4 ms·
> Calling calloc/free on a 1 MB buffer seems to take about 50 microseconds on my computer (with no optimizations), and I consider that an unusually large alloca
by masklinn 11y ago
> Calling calloc/free on a 1 MB buffer seems to take about 50 microseconds on my computer (with no optimizations), and I consider that an unusually large allocation.
Do you actually touch it and write to it? If you don't the allocation may be provisional and not actually happen until you try to write memory, in which case depending on the system you'll either get a COW fault (IIRC linux points the allocation to a zeroed COW page, first time you need to write it'll copy the page and let you write in) or a zero-fill fault (e.g. freebsd doesn't allocate the BSS by default, write access will cause a zero-fill fault and in response the VM will actually allocate the page — which it can do from a queue of pre-zeroed pages)
- mortehu 11y ago> Do you actually touch it and write to it? calloc itself calls memset in user space. That's what separates it from malloc. Is it so hard to believe a modern computer can memset 20 GB/s? If I run "dd if=/dev/zero of=/dev/null bs=1M count=10000", I get 21.0 GB/s. Reading from /dev/zero is basically performing a memset to dd's buffer in kernel space. Writing to /dev/null is a no-op, so it's a very similar test.
- masklinn 11y ago> calloc itself calls memset in user space. Have you considered reading my comment past the first phrase? If you don't actually touch the page, the system doesn't need to allocate it, let alone zero it. If you don't touch the page, assuming you're benching on linux you're measuring the cost of mapping a preexisting zeroed COW page, not the cost of actually allocating that memory.
- heinrich5991 11y agoCalling memset in userspace qualifies as touching the page, no?
- mortehu 11y agocalloc is a library function, as opposed to a system call, so it calling memset is no different from the caller doing so. calloc will do the write in "copy on write". Calling memset on every byte in a buffer should qualify as "touching it". This is why I said "in user space".