Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC_malloc/free is very slow #699

Open
ivmai opened this issue Feb 6, 2025 · 0 comments
Open

GC_malloc/free is very slow #699

ivmai opened this issue Feb 6, 2025 · 0 comments

Comments

@ivmai
Copy link
Owner

ivmai commented Feb 6, 2025

On behalf of @wareya:
At first I thought it was stuck in a deadlock because it didn't finish, but after a while it finished, it's just... 20x slower than glibc malloc. Well, I'll let you guys decide if that's worth worrying about, since it's not really a safety problem.

#include <thread>
#include <vector>
#include <atomic>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
using namespace std;

// mimalloc
//#include <mimalloc.h>
//#define malloc mi_malloc
//#define free mi_free

// glibc
//__attribute__((optnone)) void * _malloc(size_t n) { return malloc(n); }
//#define malloc _malloc

// boehm
#define GC_THREADS
#include <gc.h>
#define malloc(X) GC_malloc((X))
#define free(X) GC_free((X))

std::atomic_int tc = 0;

int * ptrs[512][8];
void * looper(void *)
{
    int unique = tc.fetch_add(1);
    for (int i = 0; i < 1000000; ++i)
    {
        for (int j = 0; j < 8; j++)
        {
            ptrs[unique][j] = (int *)(malloc(sizeof(int)));
            *ptrs[unique][j] = j+unique*1523;
        }
        for (int j = 8; j > 0; j--)
        {
            if (*ptrs[unique][j-1] != j-1+unique*1523)
                assert(((void)"memory corruption! (FILO)", 0));
            free(ptrs[unique][j-1]);
        }
        
        for (int j = 0; j < 8; j++)
        {
            ptrs[unique][j] = (int *)(malloc(sizeof(int)));
            *ptrs[unique][j] = j+unique*1523;
        }
        for (int j = 0; j < 8; j++)
        {
            if (*ptrs[unique][j] != j+unique*1523)
                assert(((void)"memory corruption! (FIFO)", 0));
            free(ptrs[unique][j]);
        }
    }
    return 0;
}

#include <pthread.h>

int main()
{
    #ifdef GC_INIT
    GC_INIT();
    #endif
    
    int threadcount = 32;
    vector<pthread_t> threads;
    
    for (int i = 0; i < threadcount; ++i)
    {
        pthread_t thread;
        pthread_create(&thread, nullptr, looper, nullptr);
        threads.emplace_back(thread);
    }
    
    for (auto & thread : threads)
        pthread_join(thread, nullptr);
    
    puts("Done!");
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant