Earlier quoted context omitted.
>Anything else is amateur hour. Such as writing the optimizing compilers that make it feasible for you to use C at all?
I don't understand your reply. How is this not a non sequitur?
Why does calloc exist?
61–70 of 141 posts
Re: Why does calloc exist?
#62Earlier quoted context omitted.
Predictability sometimes trumps optimizations. For a striking illustration of this, see timing attacks.
Exactly. I would avoid using calloc simply because I don't know what it actually does .
Re: Why does calloc exist?
#63Sorry, but this is just goofy and bad. If you depend on copy-on-write functionality, then you need to use an API that is specced to guarantee copy-on-write functionality. If that means you use an #ifdef per platform and do OS-specific stuff, then that is what you do. Anything else is amateur hour. If copy-on-write is a desirable feature, then as the API creator, your job is to expose this functionality in the cleares…
Neither copy on write nor size checking are specced as part of the calloc() definition. Here's the specification of calloc from the ISO standard: 7.22.3.2 The calloc function Synopsis #include void *calloc(size_t nmemb, size_t size); Description The calloc function allocates space for an array of nmemb objects, each of whose size is size. The space is initialized to all bits zero. Returns The calloc function returns…
That specification gives the implementation exactly two options: return NULL, or return a pointer to a block of sufficient size.
Re: Why does calloc exist?
#64I've always been surprised that memset is usually just a nonmagical for loop. I used to expect that the OS does things to magically make it faster (running lazily, etc).
Re: Why does calloc exist?
#65With malloc + memset, the OS will likely allocate that memory in huge pages, on PC that would be 2-4MB / page depending on the architecture, https://en.wikipedia.org/wiki/Page_(computer_memory)#Huge_pa...
If I calloc then write, the OS can’t give me huge pages because of that copy on write thing. Instead, the OS will gradually give me the memory in tiny 4kb pages. For large buffers you should expect TLB cache misses, therefore slowing down all operations with that memory.
Re: Why does calloc exist?
#66I you calloc some memory and then the first thing you do is write to it, can the compiler optimize away the initial write of zeros since they will just be overwritten?
On modern server/desktop/mobile CPUs, this won't make much difference anyway because the second write to the same location is essentially 'free' due to the store buffer.
And of course if you're calling calloc() in a tight loop, then the zeroing is the least of your performance concerns!
Re: Why does calloc exist?
#67Let’s see what happens after the allocation. With malloc + memset, the OS will likely allocate that memory in huge pages, on PC that would be 2-4MB / page depending on the architecture, https://en.wikipedia.org/wiki/Page_(computer_memory)#Huge_pa... If I calloc then write, the OS can’t give me huge pages because of that copy on write thing. Instead, the OS will gradually give me the memory in tiny 4kb pages. For larg…
Re: Why does calloc exist?
#68That's a nice alternative history fiction. Here's an early implementation: https://github.com/dspinellis/unix-history-repo/blob/Researc...
You haven't proven it wrong. Here's the earliest implementation in that repo (in Research UNIX V6; your link in V7): https://github.com/dspinellis/unix-history-repo/blob/Researc... calloc(n, s) { return(alloc(n*s)); } There are several interesting things we learn from poking around V6 though: - `calloc` originated not on UNIX, but as part of Mike Lesk's "iolib", which was written to make it easier to write C programs…
Re: Why does calloc exist?
#69That's a nice alternative history fiction. Here's an early implementation: https://github.com/dspinellis/unix-history-repo/blob/Researc...
You haven't proven it wrong. Here's the earliest implementation in that repo (in Research UNIX V6; your link in V7): https://github.com/dspinellis/unix-history-repo/blob/Researc... calloc(n, s) { return(alloc(n*s)); } There are several interesting things we learn from poking around V6 though: - `calloc` originated not on UNIX, but as part of Mike Lesk's "iolib", which was written to make it easier to write C programs…
https://github.com/openbsd/src/commit/c7b2af4b3f7e78424f8943...
https://github.com/bminor/glibc/commit/0950889b810736fe7ad34...
Re: Why does calloc exist?
#70 moarmem(n) // malloc(n)
moarmemslower(n) // p = malloc(n); memset(p, 0);
moarmemfaster(n) // calloc(n, 1)
evenmoarmem(p, n); // realloc()
fuggetaboutit(p) // free()