- Johnny’s Software Lab LLC Expertise: Experts in performance. If performance is a concern in software projects, contact them at https://johnnysswlab.com/contact/.
- Avoiding Data Copying: Copying data can be expensive. In C++, mechanisms like avoiding data copying in C++ have been discussed earlier. This post focuses on what the operating system can offer.
- C Library Memory Allocation: Among C library memory allocation functions,
realloc
allows growing or shrinking buffers. In C++, things are more complicated as it has no dedicatedrealloc
function. Custom container implementation withrealloc
is an option but has problems with non-trivially copyable types. - Building
resize_buffer
: A functionresize_buffer
is needed to resize buffers in-place without copying. On different systems (Linux and Windows), different approaches are used. On Linux,mmap
is used with specific parameters. On Windows, similar functionality is achieved usingVirtualAlloc
. - Experiment: A simple
jsl::vector
implementation was written to test avoiding copies. Different allocation configurations were tested on Linux and Windows. Avoiding copies saves time, but there are differences in performance between systems. - Problems with Buffer Growing: There are several problems with buffer growing, such as different behaviors on different systems, silent failures, virtual space fragmentation,
mmap
andVirtualAlloc
quirks, lack of scalability, and non-standardization. - Conclusion: Buffer growth without copying is possible and worth it in some cases but requires careful implementation and testing to avoid problems. The next post will explore moving data to a new address instead of copying.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。