-
Couldn't load subscription status.
- Fork 501
Context and shared ptr improvements #3713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Context and shared ptr improvements #3713
Conversation
- prevent a superfluous copy of ContextValue - replace raw char* key pointer with std::string, increases sizeof(DataList) but introduces small string optimization - introduced rule of 0 as far as possible (copy/move constructor and assignment and also destructor can be generated by compiler)
- removed unnecessary virtual functions - may reduce sizeof(shared_ptr) by 50% (sizeof(shared_ptr) may be just 16 bytes - removed unused header cstdlib - rule of 0 (removed destructor) - replace unique_ptr.release() by std::move() to make it more clear/readable
|
missing header |
- removed another copy of ContextValue - made functions not changing internal const
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3713 +/- ##
==========================================
+ Coverage 89.90% 89.90% +0.01%
==========================================
Files 225 225
Lines 7281 7095 -186
==========================================
- Hits 6545 6378 -167
+ Misses 736 717 -19
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes the Context and nostd::shared_ptr implementations by reducing memory overhead and eliminating dynamic memory allocations. The changes improve performance through more efficient memory management, removal of virtual function overhead, and enabling SSO (Small String Optimization) for context keys.
Key changes:
- Removes virtual functions from
shared_ptr_wrapperto eliminate vtable overhead - Uses
std::stringfor context keys instead of rawchar*to enable SSO and simplify memory management - Dynamically sizes
PlacementBufferbased onshared_ptr_wrappersize to potentially reducesizeof(shared_ptr)
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
api/include/opentelemetry/nostd/shared_ptr.h |
Removes virtual functions, dynamically sizes placement buffer, adds self-assignment checks, fixes unique_ptr conversion |
api/include/opentelemetry/context/context.h |
Replaces manual key memory management with std::string, simplifies key comparison, marks methods const-correct |
.vscode/launch.json |
Removes IDE-specific configuration file |
| shared_ptr(unique_ptr<T> &&other) noexcept | ||
| { | ||
| std::shared_ptr<T> ptr_(other.release()); | ||
| std::shared_ptr<T> ptr_(std::move(other)); |
Copilot
AI
Oct 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using std::move() on a nostd::unique_ptr will not work correctly. The original code with other.release() was correct. nostd::unique_ptr may not have a move constructor that works with std::shared_ptr, but it does have a release() method that returns the raw pointer. This change will likely cause compilation errors or incorrect behavior.
| std::shared_ptr<T> ptr_(std::move(other)); | |
| std::shared_ptr<T> ptr_(other.release()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get this!
According to cppref(constructor 13) this constructor exists. Additionally nostd::unique_ptr implements move operations perfectly.
Also, if this would be true, then code like:
nostd::unique_ptr<T> Foo() { return new T; }
std::shared_ptr<T> ptr(Foo());
would not work.
PS: GPT-5 says everything is fine with this code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, std::shared_ptr should not recognize nostd::unique_ptr and cannot be constructed from its rvalue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, std::shared_ptr should not recognize nostd::unique_ptr and cannot be constructed from its rvalue.
Perfect, I was blind and naive. Thought I saw a test for it, but didn't. After writing one I directly had compiler error.
| // and returns that head. | ||
| DataList(nostd::string_view key, const ContextValue &value) | ||
| : key_(key.begin(), key.end()) | ||
| , value_( value) |
Copilot
AI
Oct 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space before closing parenthesis in initializer list.
| , value_( value) | |
| , value_(value) |
- Fixed formatting issues in `context.h` - Adjusted `shared_ptr` construction from `unique_ptr` - Added test for move construction from `nostd::unique_ptr`
481649a to
923bc47
Compare
Fixes # (issue)
non, just improvements
Changes
non-API changes in
Contextandnostd::shared_ptrPlease provide a brief description of the changes here.
sizeof(nostd::shared_ptr)and thereforesizeof(Context), if sizeof(std::shared_ptr) == 16 instead of 32Please provide a brief description of the changes here.
For significant contributions please make sure you have completed the following items:
CHANGELOG.mdupdated for non-trivial changes