增长缓冲区以避免复制数据

  • 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 dedicated realloc function. Custom container implementation with realloc is an option but has problems with non-trivially copyable types.
  • Building resize_buffer: A function resize_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 using VirtualAlloc.
  • 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 and VirtualAlloc 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.
阅读 8
0 条评论