rust-memcache
rust-memcache copied to clipboard
Optimize numeric value serialization/deserialization
We call num.to_string().as_bytes()
to serialize and deserialize numbers when setting them in memcache server. Instead, we could use byteorder
crate's read_u16
, read_u64
etc. and write out the bytes to the server with a specific endianness(big/little) and use the same when deserializing.
deserialization:
https://github.com/aisk/rust-memcache/blob/faed4e117b8108dbcae1901c891844009229190e/src/value.rs#L161
serialization:
https://github.com/aisk/rust-memcache/blob/faed4e117b8108dbcae1901c891844009229190e/src/value.rs#L95
https://github.com/aisk/rust-memcache/blob/faed4e117b8108dbcae1901c891844009229190e/src/value.rs#L91
I have been doing some digging on this, and I am not sure if this is the best idea. Specifically because if we did use the byteorder
read functions, the memcached commands incr
and decr
would automatically fail because the read bytes are not the string representation of the number that is being stored (Source: https://github.com/memcached/memcached/wiki/Commands#incrdecr). This is fine on the numbers that cannot be represented within a u64, but I question if it is worth this small optimization, especially because it will have different performance behaviors depending on if a number is positive or negative.