Code Monkey home page Code Monkey logo

eternal's Introduction

eternal.hpp is a header-only C++ implementation of constexpr/compile-time maps and hash maps. It provides an API that is somewhat compatible with std::map/std::unordered_map, but doesn't support insertion, or other modifications. It's main focus is in binary size: it generates minimal code and doesn't incur any static initialization overhead.

Why is this useful?

  • Lookup tables

Tested with these compilers/platforms:

  • Linux GCC 4.9.4 (runtime only, since constexpr support is broken in this version)
  • Linux GCC 5.5
  • Linux GCC 6.5
  • Linux GCC 7.3
  • Linux GCC 8.1
  • Linux Clang 3.9.1
  • Linux Clang 4
  • Linux Clang 5
  • Linux Clang 6
  • Linux Clang 7
  • macOS Xcode 10.1
  • Android NDK r17+

Usage

MAPBOX_ETERNAL_CONSTEXPR const auto colors = mapbox::eternal::map<mapbox::eternal::string, Color>({
    { "red", { 255, 0, 0, 1 } },
    { "green", { 0, 128, 0, 1 } },
    { "yellow", { 255, 255, 0, 1 } },
    { "white", { 255, 255, 255, 1 } },
    { "black", { 0, 0, 0, 1 } }
});
  • mapbox::eternal::map<key, value>() is a factory function that produces a constexpr map from the std::pair<key, value>s passed to it.
  • Alternatively, use mapbox::eternal::hash_map<key, value>() to construct a hash map. The key needs a specialization of std::hash.
  • Use mapbox::eternal::string for constexpr strings.
  • If you need to support GCC 4.9, use MAPBOX_ETERNAL_CONSTEXPR instead of constexpr in the variable definition.
  • You can pass the elements in arbitrary order; they will be sorted at compile time (except for GCC 4.9). To speed up compilation, list the elements in sorted order. To determine the sort order for hash_map, iterate over the map and print the result. Note that hashes may be architecture-specific.
  • Both map() and hash_map() support multiple values for the same key. To ensure that keys are unique, run static_assert(map.unique()); (or equivalent for GCC 4.9)
  • The preprocessor variable MAPBOX_ETERNAL_IS_CONSTEXPR is set to 1 if constexpr construction and lookups are supported.

Performance

Uses a list of 148 unique CSS color names

Run on (8 X 2600 MHz CPU s)
CPU Caches:
  L1 Data 32K (x4)
  L1 Instruction 32K (x4)
  L2 Unified 262K (x4)
  L3 Unified 6291K (x1)
-----------------------------------------------------------------------------
Benchmark                                      Time           CPU Iterations
-----------------------------------------------------------------------------
EternalMap_ConstexprLookup                     2 ns          2 ns  424582090
EternalMap_Lookup                             48 ns         48 ns   14494194
EternalMap_LookupMissing                      35 ns         35 ns   20442492
EternalMap_LookupEqualRange                   78 ns         78 ns    8862891

EternalHashMap_ConstexprLookup                 2 ns          2 ns  429076688
EternalHashMap_Lookup                         30 ns         30 ns   22685952
EternalHashMap_LookupMissing                  17 ns         17 ns   39521898
EternalHashMap_LookupEqualRange               43 ns         43 ns   16696442

StdMap_Lookup                                 51 ns         50 ns   13580630
StdMap_LookupMissing                          58 ns         58 ns   11868028
StdMap_LookupEqualRange                       89 ns         89 ns    7766129

StdMultimap_Lookup                            50 ns         50 ns   14262312
StdMultimap_LookupMissing                     60 ns         59 ns   11555922
StdMultimap_LookupEqualRange                 103 ns        103 ns    6783077

StdUnorderedMap_Lookup                        43 ns         43 ns   16175696
StdUnorderedMap_LookupMissing                 50 ns         50 ns   10000000
StdUnorderedMap_LookupEqualRange              57 ns         57 ns   12256189

StdUnorderedMultimap_Lookup                   43 ns         43 ns   16011528
StdUnorderedMultimap_LookupMissing            52 ns         51 ns   10000000
StdUnorderedMultimap_LookupEqualRange         61 ns         60 ns   11519221

eternal's People

Contributors

alexshalamov avatar ivovandongen avatar kkaefer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eternal's Issues

Iterator is not compatible with std algorithms

This can be fixed by adding

namespace std
{
template<typename Element>
struct iterator_traits<::mapbox::eternal::iterator<Element>>
{
    using difference_type = std::size_t;
    using value_type = Element;
    using pointer = value_type*;
    using reference = value_type&;
    using iterator_category = std::bidirectional_iterator_tag;
};
} // namespace std

or by adding the typedefs to the type directly.

Note that difference_type should probably be std::ptrdiff_t and not std::size_t, and thus the return type of operator-() should be adapted.

allow Hasher as a type parameter to hash_map

Thanks for this library - this is great stuff - very useful to me!

I'm using hash_map where I want the key of the hash to be an int, and that's colliding with a predefined non-constexpr std::hash type, causing a compile time error

constexpr auto TemplateFieldRecs_forms_hash = mapbox::eternal::hash_map<int, v<TemplateFieldRecs_>>({ {1, TemplateFieldRecs_form1_array }, {2, TemplateFieldRecs_form2_array }, });

I was able to workaround this with a hash_map overload like this, where I could pass Hasher as a template parameter

`
namespace mapbox {
namespace eternal {
template <typename Key, typename Value, typename Hasher, std::size_t N>
static constexpr auto hash_map(const std::pair<const Key, const Value>(&items)[N]) noexcept {
return impl::map<impl::element_hash<Key, Value, Hasher>, N>(items);
}
}
}

constexpr auto TemplateFieldRecs_forms_hash = mapbox::eternal::hash_map<int, v<TemplateFieldRecs_>, ConstexprIntHasher>({
{1, TemplateFieldRecs_form1_array },
{2, TemplateFieldRecs_form2_array },
});
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.