Skip to content

Commit

Permalink
Save ~2ns from GLIBCXX exception_ptr_get_type and `exception_ptr_ge…
Browse files Browse the repository at this point in the history
…t_object`

Summary:
I noticed `get_type` was unexpectedly slow at ~2.5ns. yfeldblum pointed out that `ptr.__cxa_exception_type` probably incurs a PLT lookup, and that we can safely elide this.

The new `exception_ptr_get_type_` implementation is exactly the same as the FreeBSD version.

I also visually checked the implementation against:
 - https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/eh_ptr.cc#L138
 - https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/unwind-cxx.h

Reviewed By: yfeldblum

Differential Revision: D57792972

fbshipit-source-id: 1658ac7ef1f1f33cead11bf68747948cd522de33
  • Loading branch information
Alexey Spiridonov authored and facebook-github-bot committed May 25, 2024
1 parent 53e93dc commit 2e2e7fe
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions folly/lang/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ bool exception_ptr_access_rt_v_() noexcept {

std::type_info const* exception_ptr_get_type_(
std::exception_ptr const& ptr) noexcept {
return !ptr ? nullptr : ptr.__cxa_exception_type();
if (!ptr) {
return nullptr;
}
auto object = reinterpret_cast<void* const&>(ptr);
auto exception = static_cast<abi::__cxa_exception*>(object) - 1;
return exception->exceptionType;
}

void* exception_ptr_get_object_(
Expand All @@ -253,7 +258,7 @@ void* exception_ptr_get_object_(
return nullptr;
}
auto object = reinterpret_cast<void* const&>(ptr);
auto type = ptr.__cxa_exception_type();
auto type = exception_ptr_get_type_(ptr);
return !target || target->__do_catch(type, &object, 1) ? object : nullptr;
}

Expand Down

0 comments on commit 2e2e7fe

Please sign in to comment.