boostorg / boostorg/mp11

Improve compilation speed by taking advantage of memoization

Open
#77 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
291
Forks
97
PR merge metrics
No merged PRs in 30d

Description

Mp11 does not take full advantage of memoization, which increases compile time and memory usage.

For example for `mp_map_find_impl`:

```cpp
template class M, class... T, class K> struct mp_map_find_impl, K>
{
using U = mp_inherit...>;

template class L, class... U> static mp_identity> f( mp_identity>* );
static mp_identity f( ... );

using type = mpmf_unwrap< decltype( f((U*)0) ) >;
};
```

Here, the function `f` is duplicated as many times as there are instantiations of `mp_map_find_impl`. but, `f` depends only on `K`, not on `M`, nor on `T`. Moving the functions into a separate structure that takes `K` allows it not to be duplicated.

Example:

```cpp
template struct mp_map_find_key
{
template class L, class... U> static mp_identity> f( mp_identity
static mp_identity f( ... );
};

template class M, class... T, class K> struct mp_map_find_impl, K>
{
using U = mp_inherit...>;

using type = mpmf_unwrap< decltype( mp_map_find_key::f((U*)0) ) >;
};
```

Here is the before/after result (measured with `/usr/bin/time --format='%Es - %MK'` on Linux)

compiler | gcc-12 | clang-15
-- | -- | --
before | 0:00.46s - 200544K | 0:00.45s - 159492K
after | 0:00.37s - 172132K | 0:00.43s - 156836K

The test code:

```cpp
#include

using namespace boost::mp11;

using numbers = mp_iota_c<100>;
using l1 = mp_transform>;
using l2 = mp_append>>, l1>;
using l3 = mp_append>>, l1>;
using l4 = mp_append>>, l1>;
using l5 = mp_append>>, l1>;

using m1 = mp_fold, mp_map_insert>;
using m2 = mp_fold, mp_map_insert>;
using m3 = mp_fold, mp_map_insert>;
using m4 = mp_fold, mp_map_insert>;
using m5 = mp_fold, mp_map_insert>;
```

I noticed this pattern several times, but the changes are minor compared to the benefit (especially with gcc).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.