Frozen library - 0-cost initialisation, immutable, fixed-size containers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

With [1] I have added header-only Frozen library [2] (version 1.1.1)
to LibreOffice. Frozen is a library that provides 0-cost init, fixed
size and immutable containers (unordered_map, map, unordered_set,
set). See [2] for a better explanation.

This means that we can now use in the code a "constexpr
froze::unordered_map" for cases where we have compile-time static data
(for example strings that map to enum values or the other way around)
that will not take any cost to initialize (unlike populating an static
std::unorderd_map) and should be faster to look up because of perfect
hashes (just like gperf). For example see [4] [5] where I converted
some code to use frozen::unordered_map and frozen::unordered_set.
I haven't done much performance measurements to see if we are gaining
some (start-up) speed  as just this couple of cases in [4] and [5]
don't make much difference overall.

You can use frozen containers with most basic int/float types as "key"
and for string you have to use either std::string_view or
std::u16string_view.

To use a frozen container you have to include:
#include <frozen/bits/defines.h> (sets up defines that are used later
in the container to decide what impl. to use)
#include <frozen/bits/elsa_std.h> (needed for {u16}string_view support)
#include <frozen/unordered_map.h> (for unorderred_map support - can
also use frozen/unordered_set.h, frozen/map.h, frozen/set.h)

Maybe we can do something to make this easier.

After this you can just declare:

constexpr frozen::unordere_map<std::u16string_view, sal_Int32, 3> constData {
   { u"one" , 1 },
   { u"two", 2 },
   { u"three", 3 }
};
or alternatively (didn't try it) with no need to state the number of elements:
constexpr auto constData =
frozen::make_unordered_map<std::u16string_view, sal_Int32, 3>({
   ...
});


Lookup is the same as in std::unordered_map:
auto iterator = constData.find(u"one");
if (iterator != constData.end())
   ... found
   type value = iterator->second;
else
  ... not found

OR

auto element = constData.at(u"one");

However I think constData[u"one"] doesn't work

Any thoughts appreciated. I hope this will not cause problems and make
the code in the long run better readable, and also faster.

[1] https://gerrit.libreoffice.org/c/core/+/153173/3
[2] https://github.com/serge-sans-paille/frozen
[3] https://blog.quarkslab.com/frozen-an-header-only-constexpr-alternative-to-gperf-for-c14-users.html
[4] https://gerrit.libreoffice.org/c/core/+/153174/6
[5] https://gerrit.libreoffice.org/c/core/+/153175/10

Best Regards, Tomaž




[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux