[Assorted-commits] SF.net SVN: assorted:[1236] ydb/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-26 03:17:23
|
Revision: 1236 http://assorted.svn.sourceforge.net/assorted/?rev=1236&view=rev Author: yangzhang Date: 2009-02-26 03:17:16 +0000 (Thu, 26 Feb 2009) Log Message: ----------- - use ser_array adapter for arrays to stand in as the serialization type (instead of strings) - fixed the types on the rb pb-expander methods - added ser for ser_arrays - removed pb_size; unreliable - added some more notes Modified Paths: -------------- ydb/trunk/README ydb/trunk/src/main.lzz.clamp ydb/trunk/src/ser.h Modified: ydb/trunk/README =================================================================== --- ydb/trunk/README 2009-02-26 03:15:58 UTC (rev 1235) +++ ydb/trunk/README 2009-02-26 03:17:16 UTC (rev 1236) @@ -446,7 +446,11 @@ - almost no diff - 1: 362K - 2: 350K -- TODO use arrays instead of strings for pb and avoid dyn alloc +- DONE use arrays instead of strings for pb and avoid dyn alloc + - almost no diff + - 1: 362K + - 2: 355K +- TODO make same changes for sending responses - TODO fix pb recovery - TODO refactor st_reader, etc. to be generic opportunistic buffered readers Modified: ydb/trunk/src/main.lzz.clamp =================================================================== --- ydb/trunk/src/main.lzz.clamp 2009-02-26 03:15:58 UTC (rev 1235) +++ ydb/trunk/src/main.lzz.clamp 2009-02-26 03:17:16 UTC (rev 1236) @@ -52,7 +52,6 @@ #define map_t dense_hash_map typedef pair<int, int> pii; typedef map_t<int, int> mii; -typedef string ser_t; template<typename T> void init_map(T &map) {} template<> void init_map(dense_hash_map<int, int> &map) { @@ -256,6 +255,28 @@ st_channel<pair<st_netfd_t, shared_ptr<string> > > msgs; /** + * Adapter for arrays to look like strings (for PB serialization). + */ +class ser_array +{ + commons::array<char> a_; + size_t size_; +public: + ser_array(size_t size = buf_size) : a_(size), size_(0) {} + char *data() const { return a_.get(); } + size_t size() const { return size_; } + void clear() { size_ = 0; } + void stretch(size_t size) { + if (size > a_.size()) + a_.reset(new char[size], size); + size_ = size; + } +}; + +//typedef string ser_t; +typedef ser_array ser_t; + +/** * Serialization. * * TODO: experiment with which method is the fastest: using a string as shown @@ -281,15 +302,18 @@ copy(plen, plen + sizeof len, s.begin()); } -/** - * Helper for getting the cached ByteSize of a message. - */ -template <typename T> -size_t -pb_size(const T &msg) { - // GetCachedSize returns 0 if no cached size. - size_t len = msg.GetCachedSize(); - return len == 0 ? msg.ByteSize() : len; +template<typename T> +void +ser(ser_array &s, const T &msg) +{ + int len = msg.ByteSize(); + + // Grow the array as needed. + s.stretch(len + sizeof(uint32_t)); + + // Serialize message to a buffer with four-byte length prefix. + check(msg.SerializeToArray(s.data() + sizeof(uint32_t), len)); + *reinterpret_cast<uint32_t*>(s.data()) = htonl(uint32_t(len)); } /** @@ -299,7 +323,7 @@ void ser(ostream &s, const T &msg) { - uint32_t len = htonl(uint32_t(pb_size(msg))); + uint32_t len = htonl(uint32_t(msg.ByteSize())); s.write(reinterpret_cast<const char*>(&len), sizeof len); check(msg.SerializeToOstream(&s)); } Modified: ydb/trunk/src/ser.h =================================================================== --- ydb/trunk/src/ser.h 2009-02-26 03:15:58 UTC (rev 1235) +++ ydb/trunk/src/ser.h 2009-02-26 03:17:16 UTC (rev 1236) @@ -34,11 +34,12 @@ #define EXPAND_PB \ bool AppendToString(string*) const { throw_operation_not_supported(); } \ + bool SerializeToArray(void*, int) const { throw_operation_not_supported(); } \ bool SerializeToString(string*) const { throw_operation_not_supported(); } \ bool SerializeToOstream(ostream*) const { throw_operation_not_supported(); } \ - bool ParseFromArray(void*, size_t) { throw_operation_not_supported(); } \ - size_t GetCachedSize() const { throw_operation_not_supported(); } \ - size_t ByteSize() const { throw_operation_not_supported(); } \ + bool ParseFromArray(void*, int) { throw_operation_not_supported(); } \ + int GetCachedSize() const { throw_operation_not_supported(); } \ + int ByteSize() const { throw_operation_not_supported(); } \ #define MAKE_TYPE_BATCH(name, ns, b) \ struct name##_types { \ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |