Skip to content

<exception>: Optimize exception_ptr operations - #6403

Open
nam tran (namtran1812) wants to merge 2 commits into
microsoft:mainfrom
namtran1812:investigate/exception-ptr-6387
Open

<exception>: Optimize exception_ptr operations#6403
nam tran (namtran1812) wants to merge 2 commits into
microsoft:mainfrom
namtran1812:investigate/exception-ptr-6387

Conversation

@namtran1812

Copy link
Copy Markdown

Summary

Optimizes several std::exception_ptr operations that currently call out-of-line CRT helpers even though the representation is already known in <exception>.

This change:

  • makes the default constructor and nullptr_t constructor rely on the existing null member initializers
  • implements operator bool() as _Data1 != nullptr
  • compares _Data1 directly in operator==
  • swaps _Data1 and _Data2 directly
  • implements assignment from nullptr_t using swap-with-empty, avoiding __ExceptionPtrAssign

The exported CRT helper functions remain unchanged for ABI compatibility.

Fixes #6387.

Rationale

exception_ptr is intentionally laid out to match shared_ptr<const _EXCEPTION_RECORD>. Its two data members correspond to the shared pointer's stored pointer and control block, and the implementation already contains a static_assert enforcing matching size and alignment.

The removed out-of-line calls currently perform operations equivalent to the inlined implementations above:

  • __ExceptionPtrCreate default-constructs an empty shared_ptr
  • __ExceptionPtrToBool converts the underlying shared_ptr to bool
  • __ExceptionPtrCompare compares the underlying shared_ptr
  • __ExceptionPtrSwap swaps the underlying shared_ptr

For operator=(nullptr_t), swapping with an empty exception_ptr preserves ownership semantics and allows the temporary's destructor to release the previous control block correctly.

Validation

The existing Dev11_0299014_exception_ptr_requirements test already exercises the affected semantics, including:

  • default and nullptr construction
  • bool conversion
  • equality between empty, aliased, and distinct exception pointers
  • assignment from nullptr
  • swapping
  • self-swap

No ABI-visible layout or exported CRT symbol is changed.

Copilot AI balanced review requested due to automatic review settings August 15, 2026 17:55
@namtran1812
nam tran (namtran1812) requested a review from a team as a code owner August 15, 2026 17:55
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Aug 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR appears to refactor exception_ptr internals by removing helper calls and directly operating on the underlying _Data* fields (constructors, swap, bool conversion, and equality).

Changes:

  • Made exception_ptr default/null constructors empty.
  • Replaced operator=(nullptr_t) implementation with swap-based reset.
  • Reimplemented swap, operator bool, and operator== using direct member access.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread stl/inc/exception
Comment on lines +241 to +243
exception_ptr() noexcept {}

exception_ptr(nullptr_t) noexcept {
__ExceptionPtrCreate(this);
}
exception_ptr(nullptr_t) noexcept {}
Comment thread stl/inc/exception Outdated
Comment on lines 264 to 268
exception_ptr& operator=(nullptr_t) noexcept {
exception_ptr _Ptr;
__ExceptionPtrAssign(this, &_Ptr);
swap(*this, _Ptr);
return *this;
}
Comment thread stl/inc/exception

explicit operator bool() const noexcept {
return __ExceptionPtrToBool(this);
return _Data1 != nullptr;
Comment thread stl/inc/exception Outdated
Comment on lines +285 to +290
void* const _Data1 = _Lhs._Data1;
void* const _Data2 = _Lhs._Data2;
_Lhs._Data1 = _Rhs._Data1;
_Lhs._Data2 = _Rhs._Data2;
_Rhs._Data1 = _Data1;
_Rhs._Data2 = _Data2;
Comment thread stl/inc/exception

_NODISCARD friend bool operator==(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
return __ExceptionPtrCompare(&_Lhs, &_Rhs);
return _Lhs._Data1 == _Rhs._Data1;
@namtran1812

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI review requested due to automatic review settings August 15, 2026 18:27
@namtran1812

nam tran (namtran1812) commented Aug 15, 2026 via email

Copy link
Copy Markdown
Author

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Suppressed comments (1)

stl/inc/exception:290

  • This change inlines swap by directly swapping _Data1/_Data2 instead of using __ExceptionPtrSwap. That increases coupling to the current internal representation and makes future representation changes (or CRT-side invariants) more risky because some operations still rely on CRT functions while others do not. Recommendation (moderate): prefer delegating to __ExceptionPtrSwap to keep all core operations consistent with the CRT’s canonical behavior.
    friend void swap(exception_ptr& _Lhs, exception_ptr& _Rhs) noexcept {
        void* const _LhsData1 = _Lhs._Data1;
        void* const _LhsData2 = _Lhs._Data2;
        _Lhs._Data1           = _Rhs._Data1;
        _Lhs._Data2           = _Rhs._Data2;
        _Rhs._Data1           = _LhsData1;
        _Rhs._Data2           = _LhsData2;
    }

Comment thread stl/inc/exception
Comment on lines +241 to +243
exception_ptr() noexcept {}

exception_ptr(nullptr_t) noexcept {}
Comment thread stl/inc/exception
Comment on lines +270 to +272
explicit operator bool() const noexcept {
return _Data1 != nullptr;
}
Comment thread stl/inc/exception
Comment on lines +293 to +295
_NODISCARD friend bool operator==(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
return _Lhs._Data1 == _Rhs._Data1;
}
@namtran1812

nam tran (namtran1812) commented Aug 15, 2026 via email

Copy link
Copy Markdown
Author

@namtran1812

nam tran (namtran1812) commented Aug 15, 2026 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

<exception>: Optimize exception_ptr a bit

2 participants