Skip to content

Conversation

@Reneg973
Copy link
Contributor

Fixes # (issue)
non, just improvements

Changes

non-API changes in Context and nostd::shared_ptr

Please provide a brief description of the changes here.

  • may reduce sizeof(nostd::shared_ptr) and therefore sizeof(Context), if sizeof(std::shared_ptr) == 16 instead of 32
  • prevent dynamic allocation in case of short keys (SSO)
  • may perform faster because of removed vtable functions

Please provide a brief description of the changes here.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

- 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
@Reneg973 Reneg973 requested a review from a team as a code owner October 24, 2025 11:23
@Reneg973
Copy link
Contributor Author

Reneg973 commented Oct 24, 2025

missing header <string> for self sufficiency in context.h, will update it later

- removed another copy of ContextValue
- made functions not changing internal const
@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.90%. Comparing base (70daba9) to head (923bc47).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
api/include/opentelemetry/nostd/shared_ptr.h 75.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
api/include/opentelemetry/context/context.h 100.00% <100.00%> (ø)
api/include/opentelemetry/nostd/shared_ptr.h 96.83% <75.00%> (-3.17%) ⬇️

... and 46 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lalitb lalitb requested a review from Copilot October 24, 2025 20:28
Copy link

Copilot AI left a 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_wrapper to eliminate vtable overhead
  • Uses std::string for context keys instead of raw char* to enable SSO and simplify memory management
  • Dynamically sizes PlacementBuffer based on shared_ptr_wrapper size to potentially reduce sizeof(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));
Copy link

Copilot AI Oct 24, 2025

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.

Suggested change
std::shared_ptr<T> ptr_(std::move(other));
std::shared_ptr<T> ptr_(other.release());

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@Reneg973 Reneg973 Oct 25, 2025

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

Copy link
Member

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.

Copy link
Contributor Author

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)
Copy link

Copilot AI Oct 24, 2025

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.

Suggested change
, value_( value)
, value_(value)

Copilot uses AI. Check for mistakes.
- Fixed formatting issues in `context.h`
- Adjusted `shared_ptr` construction from `unique_ptr`
- Added test for move construction from `nostd::unique_ptr`
@Reneg973 Reneg973 force-pushed the Context_and_shared_ptr_improvements branch from 481649a to 923bc47 Compare October 27, 2025 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants