Boost logo

Boost :

From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2006-06-28 12:17:35


During some conversations with a colleague I came up with a very
sketchy realization of what could be converted, if there is interest,
in a flyweight library. Please see flyweight.hpp at Vault/Patterns:

  http://tinyurl.com/l6lt4

The so-called Flyweight Design Pattern, upon which this is inspired,
is much too OO for current C++ tastes IMHO, what I'm proposing
takes a more generic stance. A minimal use example follows
showing the syntax:

#include "flyweight.hpp"
#include <string>
#include <iostream>

int main()
{
 using boost::flyweight::flyweight;

 flyweight<std::string> str("hello world");
 std::cout<<str<<"\n";
}

Briefly put, a flyweight<T> is an object that can be used as a
const T (i.e. flyweight<T> provides an operator const T&) but whose
size is that of a pointer. The trick is that identical flyweight<T>
objects
share their representation globally, which drastically reduces memory
usage when there are much more objects than different values.
Additionally, comparison of flyweight objects can be made faster than
that of the underlying type T, since we it reduces to pointer comparison

--contrast that with what takes to compare the underlinyg type T when
T=std::string, for instance.

So, is there interest in having such a lib in Boost? Please note that
the
provided implementation is still a primitive one, but OTOH it does have
all the advertised functionality, so one can use it to test it locally
and
form an opinion about its suitability: if somebody, for instance, can
plug it into some scenario with massive quantities of elements, I'd
appreciate knowing about resulting memory and performance
improvements.

In case this raises interest, I can turn the thing into a more fleshed
out
proposal during the summer.

Thank you for your attention,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk