C++ Logo

std-proposals

Advanced search

[std-proposals] Proposal for an Extended Memory Management (EMM) Library for the C++Standard

From: 1one1 <ahshabazz_at_[hidden]>
Date: Fri, 7 Jul 2023 18:03:07 +0000 (UTC)
**Document number**: PXXXXR0 **Date**: 2023-07-07 **Project**: Programming Language C++ **Reply to**: ahshabazz
# Proposal for an Extended Memory Management (EMM) Library for the C++26 Standard
## Introduction
This paper proposes the addition of an extended memory management library to the C++ Standard Library. This library will provide developers with a standard way to give hints about memory access patterns and prefetching, allowing for potential improvements in cache efficiency and overall performance.
## Motivation and Scope
Modern processors heavily rely on cache for performance. A processor's cache is used to store data that is likely to be used in the near future, with the aim of reducing memory latency. The effectiveness of cache relies on the locality of memory accesses, both in time (temporal locality) and in space (spatial locality). Thus, the way a program accesses memory can significantly impact its performance.
However, currently there is no standard way in C++ to give hints to the compiler or the system about a program's memory access patterns. Developers must rely on non-standard compiler features or write platform-specific code, leading to a lack of portability.
By introducing an extended memory management library, we can provide a standard, platform-independent way for developers to optimize their memory access patterns, potentially improving cache efficiency and performance.
## Impact on the Standard
This proposal is purely additive and does not modify any existing parts of the C++ Standard. It does not require any changes to the core language.
## Design Decisions
We propose the addition of two components:
1. **Data Locality Optimizations API:** This API allows developers to give hints about memory access patterns. These hints can potentially be used by the system to optimize the layout of data structures in memory.
```#include <cache_control.h> // Hypothetical API header // std::emm::data_locality_hint::sequential_access myArray[1000];std::vector<int, std::emm::data_locality_hint::random_access> myVector;```
2. **Prefetching API:** This API allows developers to give hints about future memory accesses, potentially enabling the system to prefetch data into cache ahead of time, similar to the compiler specific __builtin_prefetch in GCC, however now conducted in a high level platform-independent manner.
```for (int i = 0; i < data.size(); ++i) { if (i + 1 < data.size()) { std::emm::data_access_hint::prefetch(&data[i + 1]); }
    // Process data[i] //}```
## Future Directions
If accepted, future extensions to this library could include additional memory management features, such as control over memory alignment, page size, and more.
## Acknowledgments
Thanks to the members of the C++ community for their valuable insights and discussions that helped shape this proposal.
## References
[To be added]



Received on 2023-07-07 18:05:20