3 ms·
It's a good explanation of why calloc still exists and is useful. Otherwise it would have been dropped from the standard like cfree was.
by colanderman 10y ago
It's a good explanation of why calloc still exists and is useful. Otherwise it would have been dropped from the standard like cfree was.
- viraptor 10y agoI think there's a big difference between "why does ... exist" and "why does ... still exist". calloc() may be useful today for reasons completely different from why it existed in the first place. And the difference #2 is just an implementation-specific optimisation. There's nothing in the standard that forces calloc to use lazy allocation / virtual memory. Actually, it may be implemented on platforms which can't provide this.
- greglindahl 10y agoThank you for bringing up the implementation-specific nature of #2! If the author is running Linux, then perhaps they've never checked out overcommit vs. not overcommit.
- mikeash 10y agoYou're likely to still see a performance improvement without overcommit. The OS will try to zero free pages in the background, so there's a good chance that it'll have pre-zeroed pages to hand you when you ask for them, rather than making you wait for them to be zeroed on demand. Of course, there are plenty of systems where this doesn't happen, or there are no pages in the first place, or there's no kernel zeroing stuff for you.
- Someone 10y agoThe way malloc, calloc, and memset are implemented all are implementation specific. For example, memset, when asked to zero memory, may use cache control instructions such as https://en.wikipedia.org/wiki/Cache_control_instruction#Data_cache_block_allocate_zero https://en.wikipedia.org/wiki/Cache_control_instruction#Data.... That tells your cache "pretend that you read all zeroes into the cache line at this address, and mark it as dirty (that guarantees the zeroes will be written out, even if the caller doesn't write to the cache line) For small amounts of memory that will be written to soon, that's as good as free since it doesn't have to read from memory (the naive loop will, as it has to bring in an entire cache line before it can zero out its first byte or word)
- dagw 10y agoFor what it's worth, the numpy 'trick' he used to demonstrate that feature also works on windows.