public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: "François Dumont" <frs.dumont@gmail.com>
To: libstdc++ <libstdc++@gcc.gnu.org>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH][Hashtable] Performance optimization through use of insertion hint
Date: Tue, 29 Aug 2023 21:51:56 +0200	[thread overview]
Message-ID: <8fe52f15-1acc-da0e-9353-35bdc9aa25ef@gmail.com> (raw)
In-Reply-To: <6841b337-c7a0-55d7-a513-e655c99df01a@gmail.com>

Hi

Any feedback regarding this patch ?

François

On 24/07/2023 13:02, François Dumont wrote:
> libstdc++: [_Hashtable] Make more use of insertion hint
>
>
>     When inserting an element into an empty bucket we currently insert 
> the new node
>     after the before-begin node so in first position. The drawback of 
> doing this is
>     that we are forced to update the bucket that was containing this 
> before-begin
>     node to point to the newly inserted node. To do so we need at best 
> to do a modulo
>     to find this bucket and when hash code is not cached also compute it.
>
>     To avoid this side effect it is better to insert after the last 
> node. Adding
>     a member to keep track of this last node would be an abi breaking 
> change. Still we
>     can ask the user to maintain and provide this last node as an 
> insertion hint.
>
>     Adapt range insertion methods to try to detect the last node and 
> then use it as
>     the insertion hint.
>
>     libstdc++-v3/ChangeLog:
>
>             * include/bits/hashtable_policy.h:
>             (_Insert_base::try_emplace): Adapt to use hint.
>             (_Insert_base::_M_insert_range): Try to detect container 
> last node and use it
>             as hint for insertions.
>             (_Hash_code_base::_M_hash_code(const _Hash&, const 
> _Hash_node_value<>&)): Remove.
>             (_Hash_code_base::_M_hash_code<_H2>(const _H2&, const 
> _Hash_node_value<>&)): Remove.
>             * include/bits/hashtable.h
>             (_Hashtable<>::_M_insert_bucket_begin): Add hint parameter 
> and use it when inserting
>             into an empty bucket if hint is the last container node.
>             (_Hashtable<>::_InsertInfo): New struct.
>             (_Hashtable<>::_M_get_insert_info): New, return latter.
>             (_Hashtable<>::_M_insert_multi_node): Add _InsertInfo 
> parameter.
>             (_Hashtable<>::_M_insert_unique_node): Add __node_ptr hint 
> parameter.
>             (_Hashtable<>::_M_emplace_unique(__node_ptr, _Args&&...)): 
> New.
>             (_Hashtable<>::_M_emplace_multi(__node_ptr, _Args&&...)): 
> New.
>             (_Hashtable<>::_M_emplace()): Adapt to use latters.
>             (_Hashtable<>::_M_insert_unique): Add __node_ptr parameter.
>             (_Hashtable<>::_M_insert_unique_aux): Add __node_ptr 
> parameter.
>             (_Hashtable<>::_M_insert(__node_ptr, _Arg&&, const 
> _NodeGenerator&, true_type)):
>             Use latter.
>             (_Hashtable<>::_M_reinsert_node(const_iterator, 
> node_type&&)):
>             Add hint parameter, adapt to use it.
>             (_Hashtable<>::_M_reinsert_node_multi): Use hint parameter 
> if available to extract
>             hash code.
>             (_Hashtable<>::_M_compute_hash_code(const _Hash&, 
> __node_ptr, __node_ptr,
>             const key_type&)): New.
> (_Hashtable<>::_M_compute_hash_code<_H2>(const _H2&, __node_ptr, 
> __node_ptr,
>             const key_type&)): New.
>             (_Hashtable<>::_M_merge_unique): Adapt to use latter. 
> Implement small size
>             optimization.
>             (_Hashtable<>::_M_get_insert_info(const _Hash&, 
> __node_ptr, __node_ptr,
>             const key_type&)): New.
> (_Hashtable<>::_M_get_insert_info<_H2>(const _H2&, __node_ptr, 
> __node_ptr,
>             const key_type&)): New.
>             (_Hashtable<>::_M_merge_multi): Adapt to use latter.
>             * include/bits/unordered_map.h 
> (unordered_map<>::insert(node_type&&)): Pass cend as
>             hint.
>             (unordered_map<>::insert(const_iterator, node_type&&)): 
> Adapt to use hint.
>             * include/bits/unordered_set.h 
> (unordered_set<>::insert(node_type&&)): Pass cend as
>             hint.
>             (unordered_set<>::insert(const_iterator, node_type&&)): 
> Adapt to use hint.
>             * 
> testsuite/23_containers/unordered_multimap/insert/hint.cc: Adapt 
> implementation
>             specific tests.
>
> Tested under linux x86_64.
>
> Here is the performance results of this patch, before:
>
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> insertions w/o hint      94r   94u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> insertions with perfect hint      95r   94u    0s 191999712mem 0pf
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> insertions with bad hint      94r   94u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> range insertions      88r   88u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 
> insertions w/o hint      91r   91u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 
> insertions with perfect hint      92r   93u    0s 191999712mem 0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 
> insertions with bad hint      93r   93u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 range 
> insertions      88r   88u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 
> insertions w/o hint      94r   95u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 
> insertions with perfect hint      94r   95u    0s 191999712mem 0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 
> insertions with bad hint      94r   94u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 range 
> insertions      90r   90u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 insertions 
> w/o hint      68r   68u    0s 223999264mem    0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 insertions 
> with perfect hint      67r   67u    0s 223999264mem 0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 insertions 
> with bad hint      68r   68u    0s 223999264mem    0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 range 
> insertions      62r   62u    0s 223999264mem    0pf
>
> After:
>
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> insertions w/o hint      93r   93u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> insertions with perfect hint      58r   59u    0s 191999712mem 0pf
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> insertions with bad hint      93r   94u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code NOT cached 2000000 
> range insertions      52r   51u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 
> insertions w/o hint      96r   95u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 
> insertions with perfect hint      61r   62u    0s 191999712mem 0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 
> insertions with bad hint      94r   94u    0s 191999712mem    0pf
> unordered_multiset_hint.cc-thread    hash code cached 2000000 range 
> insertions      52r   52u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 
> insertions w/o hint      96r   95u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 
> insertions with perfect hint      58r   59u    0s 191999712mem 0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 
> insertions with bad hint      94r   94u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code NOT cached 2000000 range 
> insertions      52r   52u    0s 191999712mem    0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 insertions 
> w/o hint      70r   69u    0s 223999264mem    0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 insertions 
> with perfect hint      67r   67u    0s 223999264mem 0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 insertions 
> with bad hint      67r   67u    0s 223999264mem    0pf
> unordered_set_hint.cc-thread    hash code cached 2000000 range 
> insertions      63r   62u    0s 223999264mem    0pf
>
> Ok to commit ?
>
> François

  reply	other threads:[~2023-08-29 19:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 11:02 François Dumont
2023-08-29 19:51 ` François Dumont [this message]
2023-09-01  8:59   ` Jonathan Wakely
2023-09-01  9:00     ` Jonathan Wakely
2023-09-07 16:55     ` François Dumont

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8fe52f15-1acc-da0e-9353-35bdc9aa25ef@gmail.com \
    --to=frs.dumont@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).