C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: extremely long compile time with large number of string literals
From: Ville Voutilainen (ville.voutilainen_at_[hidden])
Date: 2020-07-09 17:55:22


On Fri, 10 Jul 2020 at 01:44, Mandeep Sandhu
<mandeepsandhu.chd_at_[hidden]> wrote:
>
> > > $ g++ -std=c++11 -o string_udl_test string_udl_test.cc
> > > string_udl_test.cc:7:38: error: unable to find string literal operator
> > > ‘operator""s’ with ‘const char [2]’, ‘long unsigned int’ arguments
> > > 7 | const unordered_set<string> myset ({ "a"s, "b"s, "c"s, });
> >
> > The literals require C++14 to work.
>
> Oh, I might've confused it with
> https://en.cppreference.com/w/cpp/language/user_literal
>
> And indeed, using string literal greatly reduces the compilation time!
>
> The set with 50K strings, now compiles in7 secs! Unfortunately, I'm
> not sure if I can use c++14 in our project yet.

Okay. Now, instead of writing const unordered_set<string> myset ({
"a"s, "b"s, "c"s, ...});
you can write const unordered_set<string> myset ({ string("a"),
string("b"), string("c"), ...});
and you should hopefully see the same decrease in compilation time. In
other words,
since it looks like the string constructor overloads for the
initializer_list are a cause of slowdown,
make sure the initializer_list contains strings, instead of having it
convert strings. The literal is just
one way to do that, explicit string initializers are another.

> I did that initially just to see how it affected compilation speed.
> While, its much faster with "const char*" (I think it was. < secs for
> compiling 50K literals), its not usable since I don't want to compare
> the pointers but the pointed to data:
>
> const unordered_set<const char*> myset ({ "a", "b", "c", });
>
> string key {"a"};
> myset.find(key.c_str()); // does not find "a"
>
> This is why I was trying to use string instead.

You can use an unordered_set with a custom comparator. That way you
can get the comparison
to compare the data, not the pointers.

I'm not quite convinced this is on-topic any more. :) This is not a
"how to program correctly in C++" forum.


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups