https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90634

            Bug ID: 90634
           Summary: filesystem::path insane memory allocations
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 1000hz.radiowave at gmail dot com
  Target Milestone: ---

std::filesystem::path allocates insane amounts of memory, even for short paths.
for example, for a 20 bytes path it allocates 813 bytes. which makes it
practically unusable to store a lot of paths, while crawling through a
filesystem for example. 

see the small test program:

#include <cstdlib>
#include <iostream>
#include <string>
#include <filesystem>

using namespace std;
using namespace std::filesystem;

size_t total = 0;

// replace operator new and delete to log allocations
void* operator new(size_t n) {
        cout << "allocating " << n << " bytes" << endl;
        size_t *p = (size_t*) malloc(n + sizeof(size_t));
        *p++ = n;
        total += n;
        return p;
}
void operator delete(void* p) throw() {
        size_t *sp = (size_t *) p;
        cout << "freed " <<  *--sp << " bytes" << endl;
        total -= *sp;
        free(sp);
}

int main() {
        path p("/test/test/test/test");
        cout << "about to quit. total allocated " << total << " bytes" << endl;
        return 0;
}

the output is:

allocating 21 bytes
allocating 72 bytes
allocating 144 bytes
freed 72 bytes
allocating 288 bytes
allocating 72 bytes
freed 144 bytes
allocating 576 bytes
allocating 72 bytes
allocating 72 bytes
allocating 72 bytes
freed 72 bytes
freed 288 bytes
about to quit. total allocated 813 bytes

Reply via email to