[Serialization] [Serialization of stl map with pointer to POD type]

I have following class with stl container:
class storage : {
private:
map

Anytime you're serialization a "const" value its a red flag that you might really want to do that. "const" values are initialized when the object is created and should never subsequently change. anyway, try: save .. ar << data.count();. for (iter = data_.begin(); iter != data_.end(); iter++) { std::string key(iter->first); ar << key; ar << iter->second; } load ... _data.clear() ar >> count; while(0 < count){ std::string key; int i; ar >> key; ar >> i; data_.insert( key.cstr(), // of course this won't work - but you get the idea i ); } Robert Ramey Denis Gabaidulin wrote:
I have following class with stl container:
class storage : { private: map
data_; friend class boost::serialization::access;
template<class Archive> void save(Archive & ar, const unsigned int version) const { map
local; constDataIter iter; for (iter = data_.begin(); iter != data_.end(); iter++) { local[iter->first] = iter->second; }
ar & local; } }
This code works, but it slowly(full copy) and consume too much memory (x2) :-(
What is the best serialization strategy with such stl containers or pointer to pod type?
I cannot change const char* to string, dont ask me why :-))
Thanks you for any advice.

even simpler
ar & (map
Anytime you're serialization a "const" value its a red flag that you might really want to do that. "const" values are initialized when the object is created and should never subsequently change.
anyway, try:
save .. ar << data.count();. for (iter = data_.begin(); iter != data_.end(); iter++) { std::string key(iter->first); ar << key; ar << iter->second; }
load ... _data.clear() ar >> count; while(0 < count){ std::string key; int i; ar >> key; ar >> i; data_.insert( key.cstr(), // of course this won't work - but you get the idea i ); }
Robert Ramey
Denis Gabaidulin wrote:
I have following class with stl container:
class storage : { private: map
data_; friend class boost::serialization::access;
template<class Archive> void save(Archive & ar, const unsigned int version) const { map
local; constDataIter iter; for (iter = data_.begin(); iter != data_.end(); iter++) { local[iter->first] = iter->second; }
ar & local; } }
This code works, but it slowly(full copy) and consume too much memory (x2) :-(
What is the best serialization strategy with such stl containers or pointer to pod type?
I cannot change const char* to string, dont ask me why :-))
Thanks you for any advice.

Thank u vеry much.
In regard to const char * type, i use it just a stupid c-strings, caz
i need maximum performance.
p.s. In my app keys are immutable.
On Wed, Dec 31, 2008 at 05:02, Robert Ramey
Anytime you're serialization a "const" value its a red flag that you might really want to do that. "const" values are initialized when the object is created and should never subsequently change.
anyway, try:
save .. ar << data.count();. for (iter = data_.begin(); iter != data_.end(); iter++) { std::string key(iter->first); ar << key; ar << iter->second; }
load ... _data.clear() ar >> count; while(0 < count){ std::string key; int i; ar >> key; ar >> i; data_.insert( key.cstr(), // of course this won't work - but you get the idea i ); }
Robert Ramey
Denis Gabaidulin wrote:
I have following class with stl container:
class storage : { private: map
data_; friend class boost::serialization::access;
template<class Archive> void save(Archive & ar, const unsigned int version) const { map
local; constDataIter iter; for (iter = data_.begin(); iter != data_.end(); iter++) { local[iter->first] = iter->second; }
ar & local; } }
This code works, but it slowly(full copy) and consume too much memory (x2) :-(
What is the best serialization strategy with such stl containers or pointer to pod type?
I cannot change const char* to string, dont ask me why :-))
Thanks you for any advice.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Denis Gabaidulin wrote:
Thank u vÅry much.
In regard to const char * type, i use it just a stupid c-strings, caz i need maximum performance. p.s. In my app keys are immutable.
Then why are you storing them in a file? That's the red flag. Robert Ramey
On Wed, Dec 31, 2008 at 05:02, Robert Ramey
wrote: Anytime you're serialization a "const" value its a red flag that you might really want to do that. "const" values are initialized when the object is created and should never subsequently change.

On Wed, Dec 31, 2008 at 19:41, Robert Ramey
Thank u vÅry much.
In regard to const char * type, i use it just a stupid c-strings, caz i need maximum performance. p.s. In my app keys are immutable.
Then why are you storing them in a file? That's the red flag.
My app is something like memcached daemon, but with addiotional commands flush/load for dumping current state to persistent storage(disk).

Denis Gabaidulin wrote:
On Wed, Dec 31, 2008 at 19:41, Robert Ramey
wrote: Thank u vÅry much.
In regard to const char * type, i use it just a stupid c-strings, caz i need maximum performance. p.s. In my app keys are immutable.
Then why are you storing them in a file? That's the red flag.
My app is something like memcached daemon, but with addiotional commands flush/load for dumping current state to persistent storage(disk).
Since you're concerned about performance, why are you storing immutable data? Robert Ramey

On Wed, Dec 31, 2008 at 20:53, Robert Ramey
Thank u vÅry much.
In regard to const char * type, i use it just a stupid c-strings, caz i need maximum performance. p.s. In my app keys are immutable.
Then why are you storing them in a file? That's the red flag.
My app is something like memcached daemon, but with addiotional commands flush/load for dumping current state to persistent storage(disk).
Since you're concerned about performance, why are you storing immutable data?
I store current state of storage (just serialize all elements of map) for backup. If daemon segfaults of even server reboot, daemon lose only data since last flush commad was. So, typical usage of daemon: E.g. Another app(adv. rotation system) have some counters(impressions today, impressions total, clicks today, etc). Many process set/get/inc/dec many counters. Each process communicate with daemon using simple textual protocol(i know that binary faster, but textual more useful for my purposes). map[immutable key] = value; Key never change, but value can.

Denis Gabaidulin wrote:
Key never change, but value can.
so why store the key? eg const char * keytable[] { "key1", // store keys at compile time in order "key2", ... } unsigned int values [sizeof(keytable) / sizeof(const char *)]; unsigned int get_key_index(const char * k){ // look up key with binary search } Then just serialize the values array. My real point is that if you find yourself wanting to serialze a const value, your likely missing out on making a much faster implementation. Any time you feel the need to change a "const" value it's a red flag that something can be done better. Robert Ramey

so why store the key?
eg
const char * keytable[] { "key1", // store keys at compile time in order "key2", ... }
I have no data in compile time. Data storage fill at runtime. Some keys could be inserted or deleted, *values* could be updated(increment/decrement/set), but there is no way one can any key could be changed, therefore key is immutable. Its very similar to memcached daemon.
participants (2)
-
Denis Gabaidulin
-
Robert Ramey