public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cuzdav at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/106275] New: unordered_map with std::string key, std::hash<std::string>, and custom equality predicate weirdness
Date: Tue, 12 Jul 2022 22:18:04 +0000	[thread overview]
Message-ID: <bug-106275-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106275

            Bug ID: 106275
           Summary: unordered_map with std::string key,
                    std::hash<std::string>, and custom equality predicate
                    weirdness
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cuzdav at gmail dot com
  Target Milestone: ---

g++12.1 (on linux), Does not occur on GCC 11 or earlier.

std::unordered_map<std::string, std::hash<std::string>, CustomPred> acts
strangely such that finds do not seem to use the hasher properly, and seem to
use a linear search, invoking the equality predicate against every key.  

A custom hasher implemented in terms of std::hash<string> fixes it, as does
using the default equality predicate.  It also does not happen with key type
of, say, "int".  I've only seen it for std::string (in my limited
experimentation.)

I added output to the predicate to indicate when it's called, and it shows
excessive calls printed when "SHOWBUG" macro is defined.


////////////////////////////////////////
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>

struct EqualToWrapper{
    bool operator()(const std::string& key1, const std::string& key2) const {
        std::cout << "equal_to(key1=" << key1 << ", key2=" << key2 << ")\n";
        return std::equal_to<>{}(key1, key2);
    }
};

#ifdef SHOWBUG
using UsageMap =  std::unordered_map<std::string, int, std::hash<std::string>,
EqualToWrapper>;
#else
struct MyHash : public std::hash<std::string> {};
using UsageMap =  std::unordered_map<std::string, int, MyHash, EqualToWrapper>;
#endif

int main() {
    UsageMap m;
    m.insert(std::make_pair("A", 111));
    m.insert(std::make_pair("B", 222));
    m.insert(std::make_pair("C", 333));
    m.insert(std::make_pair("D", 444));
    m.insert(std::make_pair("E", 555));
    m.insert(std::make_pair("F", 666));
    m.find("foo");
}
////////////////////////////////


With a custom equality predicate and my derived-from-std::hash hasher, output
on g++ 12 is:

  equal_to(key1=C, key2=A)   

When run with -DSHOWBUG macro defined, output is:
  equal_to(key1=B, key2=A)
  equal_to(key1=C, key2=B)
  equal_to(key1=C, key2=A)
  equal_to(key1=D, key2=B)
  equal_to(key1=D, key2=C)
  equal_to(key1=D, key2=A)
  equal_to(key1=E, key2=B)
  equal_to(key1=E, key2=D)
  equal_to(key1=E, key2=C)
  equal_to(key1=E, key2=A)
  equal_to(key1=F, key2=E)
  equal_to(key1=F, key2=B)
  equal_to(key1=F, key2=D)
  equal_to(key1=F, key2=C)
  equal_to(key1=F, key2=A)
  equal_to(key1=foo, key2=F)
  equal_to(key1=foo, key2=E)
  equal_to(key1=foo, key2=B)
  equal_to(key1=foo, key2=D)
  equal_to(key1=foo, key2=C)
  equal_to(key1=foo, key2=A)



On Godbolt:
https://godbolt.org/z/GP5dox1qs



$ g++ -v 
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/imc/gcc-12.1.0/libexec/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-12.1.0/configure --prefix=/opt/imc/gcc-12.1.0
--enable-languages=c,c++,fortran,lto --disable-multilib
--with-build-time-tools=/build/INSTALLDIR//opt/imc/gcc-12.1.0/bin
--enable-libstdcxx-time=rt
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.1.0 (GCC)

             reply	other threads:[~2022-07-12 22:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-12 22:18 cuzdav at gmail dot com [this message]
2022-07-12 22:19 ` [Bug libstdc++/106275] " cuzdav at gmail dot com
2022-07-12 22:49 ` redi at gcc dot gnu.org
2022-07-12 22:51 ` redi at gcc dot gnu.org
2022-07-12 22:53 ` redi at gcc dot gnu.org
2022-07-12 23:00 ` redi at gcc dot gnu.org
2022-07-13 16:28 ` cuzdav at gmail dot com
2022-07-13 20:14 ` redi at gcc dot gnu.org

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=bug-106275-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).