Skip to content
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

Use atomics for pmr get/set default resource #211

Closed
marksantaniello opened this issue Jan 31, 2022 · 2 comments
Closed

Use atomics for pmr get/set default resource #211

marksantaniello opened this issue Jan 31, 2022 · 2 comments

Comments

@marksantaniello
Copy link

Pmr has the concept of a global "default resource", accessed via get_default_resource/set_default_resource. In the libstdc++ implementation, this is done with std::atomic:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/experimental/memory_resource#L537-L560

In Boost container we have a global lock:
https://github.com/boostorg/container/blob/develop/src/global_resource.cpp#L85-L86

The default c'tor for polymorphic_allocator calls get_default_resource, so every default-constructed pmr container will acquire the lock, which is expensive:

polymorphic_allocator() BOOST_NOEXCEPT
: m_resource(::boost::container::pmr::get_default_resource())

It's extremely common /not/ to pass a fancy allocator to a pmr container at construction time. This is one of the key value propositions of std::pmr -- that it provides "vocabulary types" (such as pmr::vector), which are compile-time compatible and accept fancy allocators on specific instances at runtime.

@igaztanaga
Copy link
Member

Many thanks for the report!

@marksantaniello
Copy link
Author

Thanks for the fix!

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

No branches or pull requests

2 participants