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

Avoid Memory Reallocations in flat_hash_map clear for Trivial Classes #205

Closed
linjiahao962889027 opened this issue Aug 11, 2023 · 5 comments

Comments

@linjiahao962889027
Copy link

Description:
When both the Key and Value types are trivial classes, I suggest modifying the behavior of clear in flat_hash_map to avoid releasing memory, similar to the behavior of std::vector. The current reallocation during clear impacts performance due to the second allocation, especially for trivial types where memory deallocation isn't necessary. This change would align the behavior with other standard containers, providing better performance for scenarios where frequent clearing is required.

@greg7mdp
Copy link
Owner

Thanks @linjiahao962889027, this is a good suggestion, I'll look into it.

@greg7mdp
Copy link
Owner

greg7mdp commented Sep 2, 2023

Hey @linjiahao962889027 , this should fix your issue, please let me know if you see any problem.

@linjiahao962889027
Copy link
Author

Hey @linjiahao962889027 , this should fix your issue, please let me know if you see any problem.
Awesome!!! It seems very great!!!

@stdpain
Copy link
Contributor

stdpain commented Dec 13, 2024

thanks it resolve my issue

       │                                                                                                                                             ▒
       │     ~__shared_count() noexcept                                                                                                              ▒
       │     {                                                                                                                                       ▒
       │     if (_M_pi != nullptr)                                                                                                                   ▒
  0.13 │       mov     0xa0(%rdi),%rdi                                                                                                               ▒
       │       test    %rdi,%rdi                                                                                                                     ▒
  0.28 │     ↓ je      23                                                                                                                            ▒
       │     _M_pi->_M_release();                                                                                                                    ▒
       │     → call    std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release                                                                 ▒
       │     phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<int, unsigned long>, starrocks::StdHash<int>, phmap::EqualTo<int>, std::allocat▒
       │     be/build_Release/src/runtime/be/src/util/phmap/phmap.h:1839                                                                             ▒
 69.11 │ 23:   cmpq    $0x0,0x70(%rbx)                                                                                                               ▒
  0.67 │     ↓ je      3f                                                                                                                            ▒
       │     be/build_Release/src/runtime/be/src/util/phmap/phmap.h:1848                                                                             ▒
  0.34 │       mov     0x58(%rbx),%rdi                                                                                                               ▒
  0.02 │       lea     phmap::priv::EmptyGroup()::empty_group,%rax                                                                                   ▒
  0.12 │       cmp     %rax,%rdi                                                                                                                     ▒
       │     ↓ je      3f      

@greg7mdp
Copy link
Owner

thanks it resolve my issue

Excellent, thanks for letting me know!

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

3 participants