This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

template memory analysis project


Here's a project that I wanted to spend a little time on, but I have
to shift my attention to other things.  The question is why
the following program requires more than 15 times as much virtual
memory to compile with

gcc -O -c tdeque_d.cxx

as it does with

gcc -O -c -DNO_TEMPLATE tdeque_d.cxx

(about 30M vs about 495M on sparc)

when in principle the processing is mostly identical.  Extra memory
to keep templates around can't be *THAT* huge, there must be a serious
bug somewhere.  If we can't fix this bug, I'm afraid we don't have
a template system that can be used in large projects.

What I was going to do (before I realized how far behind I am in other
work): compare the memory allocation in detail between the compilation
of the two programs, and figure out where they are different.

The rest is tdeque_d.cxx
----------------------------------------------------------------------
#include <string>
#include <deque>
#include <utility>
#include <iostream.h>
#include <algorithm>

inline const char* yn(bool ans) { return ans ? "yes" : "no";}

template<class T>
void show_d(const char* label, const deque<T>& data)
{
    cout << label;
    copy(data.begin(), data.end(), ostream_iterator<T>(cout, " "));
    cout << endl;
}

// val1, val2, val3 should be three distinct values, all different from the
// default constructor value T().

#ifdef NO_TEMPLATE
typedef double T;
#else
template<class T>
#endif
void deque_test(const T& val1, const T& val2, const T& val3)
{
    cout << "val1 = " << val1 << ", val2 = " << val2 << ", val3 = "
	 << val3 << endl;
    // test basic contructors
    deque<T> dq1;
    show_d("empty deque: ", dq1);
    cout << "empty? " << dq1.empty() << ", size? " << dq1.size() << endl;
    deque<T> dq2(5);
    show_d("default deque of 5 elements: ", dq2);
    cout << "empty? " << dq2.empty() << ", size? " << dq2.size() << endl;
    deque<T> dq3(4, val1);
    show_d("four elements of val1: ", dq3);
    deque<T> dq4(dq3);
    show_d("copy ctor: ", dq4);
    // test push_back and push_front
    dq1.push_back(val1);
    dq1.push_back(val2);
    dq1.push_front(val3);
    cout << "push back: " << val1 << endl;
    cout << "push back: " << val2 << endl;
    cout << "push front: " << val3 << endl;
    show_d("dq1 with the three values: ", dq1);

    // test compares:
    cout << "dq1 == dq3? " << yn(dq1 == dq3) << endl;
    cout << "dq1 < dq3? " << yn(dq1 < dq3) << endl;
    cout << "dq3 < dq1? " << yn(dq3 < dq1) << endl;
    cout << "dq1 == dq1? " << yn(dq1 == dq1) << endl;
    cout << "dq1 < dq1? " << yn(dq1 < dq1) << endl;

    // test member template ctor and reverse iterators
    deque<T> dq5(dq1.rbegin(), dq1.rend());
    show_d("dq5: should reverse dq1: ", dq5);
    // save value
    deque<T> dq6(dq5);
    // use indexing.
    for (unsigned i = 0; i < dq1.size(); i++)
	dq5[i] = dq1[i];
    show_d("dq5 after index copy: ", dq5);
    // restore value. (test assignment op)
    dq5 = dq6;
    show_d("dq5 restored: ", dq5);
    // swap
    dq5.swap(dq3);
    show_d("dq5 after swap with dq3: ", dq5);
    show_d("dq3 after swap with dq5: ", dq3);
    cout << "dq3 front: " << dq3.front() << ", dq3 back: " << dq3.back() << endl;
    // insertion tests.
    deque<T> dq7;
    dq7.insert(dq7.begin(), val1);
    dq7.insert(dq7.begin(), 3, val2);
    dq7.insert(dq7.end(), val3);
    show_d("dq7: ", dq7);
    typename deque<T>::iterator iter1 = find(dq7.begin(), dq7.end(), val1);
    dq7.erase(iter1);
    show_d("dq7 after erasing first val1 value: ", dq7);
    dq7.pop_back();
    show_d("dq7 after pop_back: ", dq7);
    dq7.pop_front();
    show_d("dq7 after pop_front: ", dq7);
    copy(dq5.begin(), dq5.end(), back_inserter(dq7));
    show_d("dq7 with dq5 appended: ", dq7);
    copy(dq5.begin(), dq5.end(), front_inserter(dq6));
    show_d("dq6 with dq5 prepended: ", dq6);
    sort(dq7.begin(), dq7.end());
    show_d("sorted dq7: ", dq7);
    dq7.resize(dq7.size() + 2);
    show_d("dq7 extended by 2: ", dq7);
    dq7.resize(dq7.size() + 2, val3);
    show_d("dq7 extended and filled with val3: ", dq7);
    dq7.clear();
    show_d("dq7 after clear: ", dq7);
}

int main() {
    deque_test(1.2, 2.3, 3.5);
    return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]