Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!uwvax!geowhiz!larry From: larry@geowhiz.UUCP (Larry McVoy) Newsgroups: net.unix,net.unix-wizards,net.lang.c Subject: faster malloc anyone? Message-ID: <433@geowhiz.UUCP> Date: Fri, 2-May-86 18:54:25 EDT Article-I.D.: geowhiz.433 Posted: Fri May 2 18:54:25 1986 Date-Received: Sun, 4-May-86 06:39:09 EDT Distribution: net Organization: UW Madison, Geology Dept. Lines: 76 Xref: watmath net.unix:7764 net.unix-wizards:17923 net.lang.c:8821 [ munch. Does this bug still exist???? ] I was looking at the src for [cm]alloc() and came to the (hasty) conclusion that they take to long for little memory requests. It seems that they are leftover from the days of 256K unix systems where every byte counted. With workstations typically having gigabytes of vm and 2-4 megs of phys mem, it seems that we might sacrifice some memory for speed. In particular, if you want to save strings (5-80 bytes), it seems wasteful to do a lot of work for each call to strsav(). So, I wrote the following little chunks of code and am requesting comments. Can anyone point out why these are a *bad* idea (aside from the obvious upper bound problem)? Another problem is that free() won't work on these blocks... new.h: # define BLKSIZ 8096 char* new(); utils.c: /* utils.c -- strsav(), new() */ # include "new.h" char* strsav(s) register char* s; { char* strcpy(); register char* t; t = new(strlen(s) + 1); /* strings smaller than BLKSIZ */ return strcpy(t, s); } /*------------------------------------------------------------------02/May/86-* * new(size) - fast(??) memory allocator * * Inputs -> (int) * * Returns -> (char*) * * Results -> The memory is allocated in big contiguous blocks via calloc(3). * If the requst can fit in what's left of a block, then a block * of the size requested is returned. Otherwise, the rest of the * block is discarded & a new block is allocated. * * Warning -> This would seem to work great for little stuff. Don't use it * for big blocks. Absolute largest allocatable block is BLKSIZ. * For speed NO CHECK IS PERFORMED TO SEE IF THE REQUEST IS LESS * THAN BLKSIZ. BLKSIZ is guaranteed to be 1k or bigger (usually * much bigger). * Revisions: *----------------------------------------------------------------------larry-*/ char* new(size) register unsigned size; { register char* blk; static char* block = NULL; static unsigned bytes_left = 0; if (bytes_left < size) if (!(block = calloc(1, BLKSIZ))) syserr("calloc in new"); blk = block; block += size; bytes -= size; } -- Larry McVoy ----------- Arpa: mcvoy@rsch.wisc.edu Uucp: {seismo, ihnp4}!uwvax!geowhiz!geophiz!larry "Just remember, wherever you go -- there you are." -Buckaroo Banzai