perf(util/serializer): minor performance improvement

This commit is contained in:
Leonardo de Moura 2015-08-14 18:13:08 -07:00
parent cc8b5d2d6e
commit d3d1b58fb4
2 changed files with 13 additions and 17 deletions

View file

@ -75,18 +75,14 @@ std::string deserializer_core::read_string() {
return r;
}
unsigned deserializer_core::read_unsigned() {
unsigned r = static_cast<unsigned>(m_in.get());
unsigned deserializer_core::read_unsigned_ext() {
unsigned r;
static_assert(sizeof(r) == 4, "unexpected unsigned size");
if (r < 255) {
return r;
} else {
r = static_cast<unsigned>(m_in.get()) << 24;
r |= static_cast<unsigned>(m_in.get()) << 16;
r |= static_cast<unsigned>(m_in.get()) << 8;
r |= static_cast<unsigned>(m_in.get());
return r;
}
r = static_cast<unsigned>(m_in.get()) << 24;
r |= static_cast<unsigned>(m_in.get()) << 16;
r |= static_cast<unsigned>(m_in.get()) << 8;
r |= static_cast<unsigned>(m_in.get());
return r;
}
uint64 deserializer_core::read_uint64() {
@ -97,10 +93,6 @@ uint64 deserializer_core::read_uint64() {
return r;
}
int deserializer_core::read_int() {
return read_unsigned();
}
double deserializer_core::read_double() {
// TODO(Leo): use std::hexfloat as soon as it is supported by g++
std::istringstream in(read_string());

View file

@ -51,12 +51,16 @@ inline serializer & operator<<(serializer & s, double b) { s.write_double(b); re
*/
class deserializer_core {
std::istream & m_in;
unsigned read_unsigned_ext();
public:
deserializer_core(std::istream & in):m_in(in) {}
std::string read_string();
unsigned read_unsigned();
unsigned read_unsigned() {
unsigned r = static_cast<unsigned>(m_in.get());
return r < 255 ? r : read_unsigned_ext();
}
uint64 read_uint64();
int read_int();
int read_int() { return read_unsigned(); }
char read_char() { return m_in.get(); }
bool read_bool() { return m_in.get() != 0; }
double read_double();