Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Up to 51 times faster than malloc !

Name: Anonymous 2012-11-06 15:22

Hello people.
I made an allocation system.
It must be initialized and can only allocate a fixed-number of same-sized memory blocks (of at least sizeof(void*) bytes).
It allows freeing memory blocs, which can be reused later.
(it also displays how ugly pointer syntax can be)
I made a small benchmark to allocate and free 10000 times 10000 blocks of 1024 bytes. caralloc and carafree are more than 51 times faster than malloc and free here.


#define PTRSIZE sizeof(void*)

char* caralloc_main_pointer;
char* caralloc_memory;

void init_caralloc(size_t cell_size, unsigned int cells)
{
    int i;
    if(cell_size < PTRSIZE)
        fprintf(stderr, "caralloc error: cell_size is of %zd,  it should be at least %ld\n", cell_size, PTRSIZE);
    else
    {
        caralloc_memory = calloc(cells, cell_size);
        for(i = 0; i < cells-1; i++)
            *((void**)(((char*)caralloc_memory) + i*cell_size)) = ((void*)caralloc_memory) + (i+1) * cell_size;
        *((void**)(((char*)caralloc_memory) + (cells-1) * cell_size)) = NULL;
       
        caralloc_main_pointer = caralloc_memory;
    }
}

void quit_caralloc()
{
    free(caralloc_memory);
}

void* caralloc()
{
    void** main_pointer = (void**) caralloc_main_pointer;
    caralloc_main_pointer = *main_pointer;
    return (void*) main_pointer;
}

void carafree(void* ptr)
{
    memcpy(caralloc_main_pointer, ptr, PTRSIZE);
    caralloc_main_pointer = ptr;
}

#define N 10000
#define K 10000
#define S 1024
int main()
{
    void* ptrs[N];
    long clocks[2];
    clock_t start, end;
    int i, j;
    size_t size = S;
   
    init_caralloc(size, N+1);
   
    start = clock();
    for(j = 0; j < K; j++)
    {
        for(i = 0; i < N; i++) ptrs[i] = malloc(size);
        for(i = 0; i < N; i++) free(ptrs[i]);
    }
    end = clock();
    clocks[0] = end-start;
    printf("  malloc & free    : %ld clocks\n", clocks[0]);
   
    start = clock();
    for(j = 0; j < K; j++)
    {
        for(i = 0; i < N; i++) ptrs[i] = caralloc();
        for(i = 0; i < N; i++) carafree(ptrs[i]);
    }
    end = clock();
    clocks[1] = end-start;
   
    printf("caralloc & carafree: %ld clocks\n", clocks[1]);
   
    printf("caralloc is %f times faster than malloc\n", ((double)clocks[0]) / ((double)clocks[1]));
   
    quit_caralloc();
}


What do you guys think ?

Name: Anonymous 2012-11-06 15:26

How does it perform for different block sizes?

Name: Anonymous 2012-11-06 15:39

Pretty good.

Name: Anonymous 2012-11-06 15:41

gcc: alloc: No such file or directory
alloc.c:6:27: error: expected ‘)’ before ‘cell_size’
alloc.c: In function ‘quit_caralloc’:
alloc.c:24:5: warning: incompatible implicit declaration of built-in function ‘free’
alloc.c: In function ‘carafree’:
alloc.c:36:5: warning: incompatible implicit declaration of built-in function ‘memcpy’
alloc.c: In function ‘main’:
alloc.c:47:5: error: ‘clock_t’ undeclared (first use in this function)
alloc.c:47:5: note: each undeclared identifier is reported only once for each function it appears in
alloc.c:47:13: error: expected ‘;’ before ‘start’
alloc.c:49:5: error: ‘size_t’ undeclared (first use in this function)
alloc.c:49:12: error: expected ‘;’ before ‘size’
alloc.c:51:19: error: ‘size’ undeclared (first use in this function)
alloc.c:53:5: error: ‘start’ undeclared (first use in this function)
alloc.c:56:42: warning: incompatible implicit declaration of built-in function ‘malloc’
alloc.c:57:32: warning: incompatible implicit declaration of built-in function ‘free’
alloc.c:59:5: error: ‘end’ undeclared (first use in this function)
alloc.c:61:5: warning: incompatible implicit declaration of built-in function ‘printf’

Name: Anonymous 2012-11-06 16:23

>>4

#include <stdlib.h>
#include <stdio.h>

Name: Anonymous 2012-11-06 16:31

>>5
Oh.

Name: Anonymous 2012-11-06 16:56

Up to 51 times faster than malloc !
        caralloc_memory = calloc(cells, cell_size);

Name: Anonymous 2012-11-06 17:15

>>7
Looks like he's allocating a chunk of memory to initialize the ``caralloc system''.

I don't see what's wrong with that.

Name: Anonymous 2012-11-06 17:25

>>8
It uses malloc, so it's not faster than malloc. It's just faster than using malloc incorrectly, which is apparenlty what >>1 usually does.

Name: Anonymous 2012-11-06 17:32

>>8
mmap faggot

Name: Anonymous 2012-11-06 17:39

It uses malloc once and fucks with that chunk multiple times.

Which is obviously faster than doing 10000 individual mallocs.

Name: Anonymous 2012-11-06 17:42

FAST AS FUCK

Name: Anonymous 2012-11-06 17:54

Fragments memory 51 times faster!

Name: Anonymous 2012-11-06 18:02

So it's just a fixed-size region-based free-list allocator.

Name: Anonymous 2012-11-06 18:09

What do you guys think?
That you are a fucking retard.

Seriously this isn't even thread or signal safe, I seriously hope you never try use this flimsy piece of shit for any real task.

Name: Anonymous 2012-11-06 18:09

>>14
But he did it already for me.

Name: Anonymous 2012-11-06 18:32

>>1
The initialization step is slow. Just increment the pointer by the size of the allocation each time. Zero initialization time, and it works for different size allocations.

It's called Arena or Region allocation, and it's what everyone uses.

http://en.wikipedia.org/wiki/Region-based_memory_management
http://dice.se/wp-content/uploads/scopestacks_public.pdf

Name: Anonymous 2012-11-06 18:58

>>17
You can do the Worse is Better version (also a WiB GC) by saving a process's state to disk and restarting it with the save file when it runs out of memory.

Name: Anonymous 2012-11-06 20:23

OPTIMIZED

Name: Anonymous 2012-11-06 22:52

Wow, somebody on /prog/ produced something useful.

Name: Anonymous 2012-11-06 23:08

>>20
You call a low quality implementation of a less-than-average efficient memory pool useful?

Name: Anonymous 2012-11-07 0:18

>>21
It is more useful than >>21

Name: Anonymous 2012-11-07 0:37

Now make gcalloc()

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-11-07 0:41

Is it "up to" or "more than" 51 times faster?

Either way this technique has been known and used in embedded systems for years now.

Name: Anonymous 2012-11-07 1:13

Is malloc() really a big enough bottle neck to justify such poorly planned copies?

I've never run the numbers, but malloc() is probably only like 0.001% of the used time.

Name: Anonymous 2012-11-07 2:59

>>25
I've never run the numbers but ... probably only like 0.001%

It's called computer science, not computer speculation.

Name: Anonymous 2012-11-07 5:00

>>25
Depends how often you use it in a program.
The more often per useful instruction, the worse your program, probably.

Don't change these.
Name: Email:
Entire Thread Thread List