Boost.MultiIndex helps the programmer to avoid the manual construction of cumbersome
compositions of containers when multi-indexing capabilities are needed. Furthermore,
it does so in an efficient manner, both in terms of space and time consumption. The
space savings stem from the compact representation of the underlying data structures,
requiring a single node per element. As for time efficiency, Boost.MultiIndex
intensively uses metaprogramming techniques producing very tight implementations
of member functions which take care of the elementary operations for each index:
for multi_index_containers with two or more indices, the running time
can be reduced to half as long as with manual simulations involving several
STL containers.

The section on emulation
of standard containers with multi_index_container shows the equivalence
between single-index multi_index_containers and some STL containers. Let us now
concentrate on the problem of simulating a multi_index_container with two
or more indices with a suitable combination of standard containers.

indexed_t maintains two internal indices on elements of type
int. In order to simulate this data structure resorting only to
standard STL containers, one can use on a first approach the following types:

where manual_t1 is the "base" container that holds
the actual elements, and manual_t2 stores pointers to
elements of manual_t1. This scheme turns out to be quite
inefficient, though: while insertion into the data structure is simple enough:

The construction can be extended in a straightforward manner to
handle more than two indices. In what follows, we will compare
instantiations of multi_index_container against this sort of
manual simulations.

The gain in space consumption of multi_index_container with
respect to its manual simulations is amenable to a very simple
theoretical analysis. For simplicity, we will ignore alignment
issues (which in general play in favor of multi_index_container.)

Nodes of a multi_index_container with N indices hold the value
of the element plus N headers containing linking information for
each index. Thus the node size is

On the other hand, the manual simulation allocates N nodes per
element, the first holding the elements themselves and the rest
storing iterators to the "base" container. In practice, an iterator
merely holds a raw pointer to the node it is associated to, so its size
is independent of the type of the elements. Summing all contributions,
the space allocated per element in a manual simulation is

The relative amount of memory taken up by multi_index_container
with respect to its manual simulation is just
SI / SM, which can be expressed
then as:

SI / SM =
SI / (SI + (N-1)p).

The formula shows that multi_index_container is more efficient
with regard to memory consumption as the number of indices grow. An implicit
assumption has been made that headers of multi_index_container
index nodes are the same size that their analogues in STL containers; but there
is a particular case in which this is often not the case: ordered indices use a
spatial optimization
technique which is not present in many implementations of
std::set, giving an additional advantage to
multi_index_containers of one system word per ordered index.
Taking this fact into account, the former formula can be adjusted to:

SI / SM =
SI / (SI + (N-1)p + Ow),

where O is the number of ordered indices of the container, and w
is the system word size (typically 4 bytes on 32-bit architectures.)

These considerations have overlooked an aspect of the greatest practical
importance: the fact that multi_index_container allocates a single
node per element, compared to the many nodes of different sizes
built by manual simulations, diminishes memory fragmentation, which
can show up in more usable memory available and better performance.

From the point of view of computational complexity (i.e. big-O
characterization), multi_index_container and its corresponding manual
simulations are equivalent: inserting an element into
a multi_index_container reduces to a simple combination of
elementary insertion operations on each of the indices, and
similarly for deletion. Hence, the most we can expect is a reduction
(or increase) of execution time by a roughly constant factor. As we
will see later, the reduction can be very significative for
multi_index_containers with two or more indices.

In the special case of multi_index_containers with only one index,
resulting performance will roughly match that of the STL equivalent containers:
tests show that there is at most a negligible degradation with respect to STL,
and even in some cases a small improvement.

has been measured for different instantiations of multi_index_container
at values of n 1,000, 10,000 and 100,000,
and its execution time compared with that of the equivalent algorithm
for the corresponding manual simulation of the data structure based on
STL containers. The table below describes the test environments used.

Tests environments.

Compiler

Settings

OS and CPU

GCC 3.4.5 (mingw special)

-O3

Windows 2000 Pro on P4 1.5 GHz, 256 MB RAM

Intel C++ 7.1

default release settings

Windows 2000 Pro on P4 1.5 GHz, 256 MB RAM

Microsoft Visual C++ 8.0

default release settings, _SECURE_SCL=0

Windows XP on P4 Xeon 3.2 GHz, 1 GB RAM

The relative memory consumption (i.e. the amount of memory allocated
by a multi_index_container with respect to its manual simulation)
is determined by dividing the size of a multi_index_container node
by the sum of node sizes of all the containers integrating the
simulating data structure.

Somewhat surprisingly, multi_index_container performs slightly
better than std::set. A very likely explanation for this behavior
is that the lower memory consumption of multi_index_container
results in a higher processor cache hit rate.
The improvement is smallest for GCC, presumably because the worse quality of
this compiler's optimizer masks the cache-related benefits.

multi_index_container does not attain the performance
of its STL counterpart, although the figures are close. Again, the worst results
are those of GCC, with a degradation of up to 7%, while ICC and MSVC do not
exceed a mere 5%.

The experimental results confirm our hypothesis that multi_index_container
provides an improvement on execution time by an approximately constant factor,
which in this case lies around 60%. There is no obvious explanation for the
increased advantage of multi_index_container in MSVC for
n=105.

For n=103 and n=104, the results
are in agreement with our theoretical analysis, showing a constant factor
improvement of 50-65% with respect to the STL-based manual simulation.
Curiously enough, this speedup gets even higher when
n=105 for two of the compilers, namely GCC and ICC.
In order to rule out spurious results, the tests
have been run many times, yielding similar outcomes. Both test environments
are deployed on the same machine, which points to some OS-related reason for
this phenomenon.

We have shown that multi_index_container outperforms, both in space and
time efficiency, equivalent data structures obtained from the manual
combination of STL containers. This improvement gets larger when the number
of indices increase.

In the special case of replacing standard containers with single-indexed
multi_index_containers, the performance of Boost.MultiIndex
is comparable with that of the tested STL implementations, and can even yield
some improvements both in space consumption and execution time.