10 ms·
How fast can you allocate a large block of memory in C++?
- saagarjha 7y ago> If you actually want to measure the memory allocation in C++, then you need to ask the system to give you s bytes of allocated and initialized memory. You can achieve the desired result in C++ by adding parentheses after the call to the new operator Is this an actual thing? You can force reification of overcommitted memory just by calling operator() on it?
- ajtulloch 7y agoIt's not `operator()`, it's value initialization. If you're particularly masochistic you can read more about it at https://en.cppreference.com/w/cpp/language/value_initialization https://en.cppreference.com/w/cpp/language/value_initializat....
- saagarjha 7y agoAh, that makes more sense. Thanks.
- Someone 7y agoI don’t think that guarantees the memory gets committed. The compiler/standard library combination is allowed to know how the OS behaves, so if it, say, initializes new pages with 0x00, it can use that information to skip the initialization of that memory.
- inetknght 7y agoThe example given: char *buf = new char[s](); Put into Compiler Explorer: https://gcc.godbolt.org/z/QAs9gz However, doing this is a very strong code smell. At the very least, using `new` and assigning to a raw pointer is a sign that the C++ developer is managing memory manually and is likely to hit a lot of problems including memory leaks or segmentation faults. Also many would forget that this is calling `operator new[]()`, not `operator new()` and might confuse with placement-new `operator new(...)` or `operator new[](...)`. And the developer might also forget that `new` could throw an exception... [0] The developer should instead be using, at a minimum, a `std::unique_ptr<char[]>` [1]. Or, IMO, a `std::vector<char>` which reminds the developer not only of the pointer but also of the count of bytes which have been allocated (.capacity()) and also of the valid range which has been initialized (.size()). IMO, if the developer wanted a pointer to a byte array then it's a lot easier to use `malloc()` than trying to remember all the different ways you can get screwed by `operator new`: std::unique_ptr<char, std::function<void(char*)>> m{ (char*)std::malloc(count), std::free }; [0]: https://en.cppreference.com/w/cpp/language/new https://en.cppreference.com/w/cpp/language/new [1]: https://en.cppreference.com/w/cpp/memory/unique_ptr https://en.cppreference.com/w/cpp/memory/unique_ptr
- burfog 7y agoI gave up on C++ in part because the language changes too much. I paid for lessons on it. The std:: stuff didn't exist, templates were a recent invention, and people were starting to agree that putting the overload keyword everywhere was bad style. Mostly, people were just excited about // comments and cout. I keep hearing about how C++ is supposed to be done, and it seems to change every year. I don't know what people do with the older code as it becomes unfashionable with "very strong code smell". Updating it is risky busywork, kind of like porting to Python 3, with potential for serious bugs. Leaving it in place will make developers differently unhappy. It isn't easy getting everybody on a team to agree on what subset of C++ is OK to use. People will sneak in their must-have feature. You'd have better luck getting agreement between emacs and vi.
- ncmncm 7y agoC++ makes no pretense of ever planning to be "done". "Done" is another word for dead. Even COBOL is still evolving. Failing to keep up with evolving languages is called stagnation. Everyone is free to stagnate, but I do not advise it. You use the subset of the language supported by the compiler you have. Code using newer features is better because the new features were added for sound engineering reasons, not just to be different.
- lmm 7y agoIt's the opposite of sound engineering. Every version of C++ contains a new feature that does 11% of the proper solution because the feature in the last version only did 9% of the proper solution. Despite all that churn the language is still as far behind ML as ever.
- ncmncm 7y agoI see that you have not been keeping up.
- throwaway17_17 7y ago
- paulsutter 7y agoAny C++ programmer could write code that “allocates” 500MB in a few instructions if the pages already exist. If the pages don’t already exist (as indicated by the given timings), this is a test of the OS and has little to do with the language. It’s a poorly posed question. C++ runs on many environments.
- ggggtez 7y agoCorrect me if I'm wrong, but won't the speed depends on the compiler, the operating system, the hardware... There is no particular reason you can't allocate 500GB in 1 cycle. Just get rid of all memory management. It seems silly to answer a question about C++ in operations per second, besides.
- chapium 7y agoWhat is the point in initializing memory here?
- saagarjha 7y agoIt ensures that they actually exist, because otherwise the OS will just overcommit them.
- signa11 7y agodup: https://news.ycombinator.com/item?id=22053831 https://news.ycombinator.com/item?id=22053831
- ncmncm 7y agoIf you need to allocate lots of memory, you should be using mmap(). If you need it faulted in, you use MAP_POPULATE. Try to use MAP_HUGETLB to economize on TLB cache entries; you may need to set "vm.nr_hugepages=16384" (or something) in /etc/sysctl.conf (or someplace) to reserve them. If you are allocating lots of memory and don't know about hugepages, you badly need to learn. If your program has threads, unmapping memory is generally to be avoided.
- PaulDavisThe1st 7y agommap is an operating system interface, or at the very least a part of POSIX. it isn't part of the C++ standard, and so you cannot rely on it being present on systems that don't claim extensive POSIX compatibility. As for MAP_POPULATE and MAP_HUGETLB, try using that on a POSIX-ish OS that isn't Linux and see how far you get.
- ncmncm 7y agoWhat, are there any left? Every OS nowadays has mmap, by some spelling, and they all support hugepages, likewise. A program that doesn't use its host OS doesn't, generally, do much.
- saagarjha 7y ago> Every OS nowadays has mmap, by some spelling I mean, I'm fairly sure that's the problem. (Plus a lot of systems support C++ but not virtual memory…)
- IcePic 7y agoJust to take one (small) example, the Arduino IDE is C++, and you will not mmap() a lot there.
- ncmncm 7y agoYou also won't new[] a lot there. How much can it even physically address?
- halayli 7y agomost std libs rely on malloc lib underneath. so i am not sure what's being tested here. Especially that the standard allocator is being used.
- gumby 7y agoAnonymous mmap() zeros memory so why is new wasting its cycles doing so too? I doubt any modern c++ standard library is using brk/sbrk I this day and age.
- AstralStorm 7y agoThe thing is, tested C++ standard library implementation cannot assume a specific platform or implementation of libc. It's too portable. Yes, you could add some platform and type specific speedups, it ends up a big mess. In C++20 you get uninitialized allocation functions which do not have to initialize.
- gumby 7y agoBy definition the libraries present a portable interface to underlying system resources and are full of platform specific code (I was a Cygnus founder so spent plenty of time deep in these issues at a time when there was more diversity). All posix is systems I know (e.g. Linux, BSD, Solaris, etc) return pages of zeros from anonymous mmap. I consider it a bug if they post process in this case.
- blackrock 7y agoAnyone want to take a stab at how malloc actually works? What’s under the hood? What data structures and algorithms does it use?
- jabl 7y agoIn a comment the blog author says he's using Ubuntu and glibc malloc. And glibc malloc has a threshold (128 kB or thereabouts) where it switches to using mmap(). So in practice this benchmark is a test of how the Linux kernel implements the mmap() syscall, and how the compiler implements the zeroing loop.
- Const-me 7y ago> And glibc malloc has a threshold (128 kB or thereabouts) where it switches to using mmap() Very similar on Windows. In modern CRT, malloc is a thin wrapper over HeapAlloc WinAPI. That one has a threshold (512kb or 1MB depending on 32- or 64-bit process) where it switches to VirtualAlloc.
- pjc50 7y agoK&R actually contains an example malloc()/free() implementation using a "freelist" - i.e. a list of spare memory blocks held as a linked list in the "unallocated" (or de-allocated) blocks. Memory is retrieved from the OS using the ugly sbrk() interface.
- scoutt 7y ago> the system may then decide This makes me think if the whole question is really about C++ or the OS implementation for memory allocations. This could have little to do with C++. On a bare-metal system, malloc/sbrk (and thus new) can be something as simple as: uint32_t retptr = heap; heap += size; return retptr;
- Const-me 7y agoThe OP should have added “Linux” somewhere in the title. Here on Windows, the OS doesn’t overcommit and the first new[] call actually allocates these pages (possibly in a page file). Also, if the requested size is large enough (>512kb on Win32, >1MB on Win64), the OS guarantees zero initialization despite C++ doesn’t: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc https://docs.microsoft.com/en-us/windows/win32/api/memoryapi...
- wbkang 7y agoEven on Windows the pagefile space is taken but it doesn't have to write to disk right? They can zero on first read.
- fargle 7y agoThis is a completely ridiculous article. The whole concept is flawed. New/delete like this are for smallish general purpose allocation, for example of objects, where we want to keep the code at a fairly high level in C++ and not think about low-level concepts like "bytes" or allocation mechanics. Or performance. Conversely, an app written in C++ that needs huge swaths memory for manipulating raw bytes and needs high-performance would not likely use new char[] or calloc/malloc directly at all. It would directly interface with the OS via mmap or indirectly via some domain relevant library, for example, OpenCV. We might even still exclusively use the C++ language to write the low-level portions of the code, but if you need to interface with the OS in specific controlled ways or write performance oriented code, you are not going to do it using only high-level C++ operations. You are going to call exactly what you need to interface with the system directly. If you want mmap(), you'd just call mmap() from C++. If you want sbrk(), you'd call sbrk(). If you don't like the system new/delete and malloc/free, you could use something like dlmalloc and and even remap new/delete to it, or to some custom slab allocator. Secondly, as pointed out by others, the author isn't benchmarking C++. He's benchmarking glibc malloc and the Linux mmap. We'd expect a C program using malloc/calloc to have exactly the same timings. Thirdly, fallacious reasoning about initialization. If your app needs to allocate (for some ??? reason) 32 GB of memory, you would NOT automatically zero it first, unless you actually needed it to be zero, or you wanted your app to waste a bunch of time. Unnecessary zeroing huge arrays is not required for good security. We're mostly benchmarking memset here, not even malloc/mmap. So it's completely apples/oranges to compare mmap benchmark with a zerofill benchmark. I almost expect a follow-up article that points out accessing x[i] is much faster when x is a raw array of integers than when x is a std::map of strings. Now read the footnotes. The author is misunderstanding what "idomatic C++" means. RAII is the best answer I can come up with for idiomatic - C++ doesn't have a built-in guard concept so we creatively mis-use constructors and destructors to get a similar result. Footnote 2 proves the author knows bupkis about how C++ or any part of the system actually works. He's semi-admitting as much. I'd hope for much better from a CS professor. Stick to benchmarking this: for(i=0; i<1E9; ++i);
- adrianmonk 7y agoVery minor nitpick, but IMHO, this article creates a little unnecessary confusion by insisting upon a particular meaning of the word "allocate" without being very clear it's doing that. That is, it uses "allocate" to mean "make ready for immediate use with no further (lazy) processing". Another perfectly reasonable definition would be "guarantee to be possible to use". The distinction would arise on, for example, a system which doesn't over-commit memory but also doesn't fault pages in upon allocation. On such a system, allocation might give you a rock solid guarantee that you can write to (and read from) that memory but it wouldn't give you any guarantee about how fast or slow that would happen on initial access. Personally, I prefer the second definition, but that's not really the point. The point is to be clear and avoid confusion.
- NoZZz 7y agoOn a barebones system, 0 cpu cycles.