diff options
author | sfan5 <sfan5@live.de> | 2020-05-26 17:38:31 +0200 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2020-05-27 21:58:40 +0200 |
commit | 471e567657dfd75a994a1b54d7a23cf4541a6bed (patch) | |
tree | 1b909e24669141f3fba2a6d98c68e22f1288249b /src/util | |
parent | 2fd5f38c45a3b57a9ea2d566aa50f9e5c33794d2 (diff) | |
download | minetest-471e567657dfd75a994a1b54d7a23cf4541a6bed.tar.gz minetest-471e567657dfd75a994a1b54d7a23cf4541a6bed.tar.bz2 minetest-471e567657dfd75a994a1b54d7a23cf4541a6bed.zip |
Value copy / allocation optimizations mostly in server, SAO and serialize code
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/serialize.cpp | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp index f0e177d57..5b276668d 100644 --- a/src/util/serialize.cpp +++ b/src/util/serialize.cpp @@ -110,6 +110,7 @@ std::string serializeString(const std::string &plain) if (plain.size() > STRING_MAX_LEN) throw SerializationError("String too long for serializeString"); + s.reserve(2 + plain.size()); writeU16((u8 *)&buf[0], plain.size()); s.append(buf, 2); @@ -131,13 +132,11 @@ std::string deSerializeString(std::istream &is) if (s_size == 0) return s; - Buffer<char> buf2(s_size); - is.read(&buf2[0], s_size); + s.resize(s_size); + is.read(&s[0], s_size); if (is.gcount() != s_size) throw SerializationError("deSerializeString: couldn't read all chars"); - s.reserve(s_size); - s.append(&buf2[0], s_size); return s; } @@ -152,6 +151,7 @@ std::string serializeWideString(const std::wstring &plain) if (plain.size() > WIDE_STRING_MAX_LEN) throw SerializationError("String too long for serializeWideString"); + s.reserve(2 + 2 * plain.size()); writeU16((u8 *)buf, plain.size()); s.append(buf, 2); @@ -196,13 +196,14 @@ std::wstring deSerializeWideString(std::istream &is) std::string serializeLongString(const std::string &plain) { + std::string s; char buf[4]; if (plain.size() > LONG_STRING_MAX_LEN) throw SerializationError("String too long for serializeLongString"); + s.reserve(4 + plain.size()); writeU32((u8*)&buf[0], plain.size()); - std::string s; s.append(buf, 4); s.append(plain); return s; @@ -227,13 +228,11 @@ std::string deSerializeLongString(std::istream &is) "string too long: " + itos(s_size) + " bytes"); } - Buffer<char> buf2(s_size); - is.read(&buf2[0], s_size); + s.resize(s_size); + is.read(&s[0], s_size); if ((u32)is.gcount() != s_size) throw SerializationError("deSerializeLongString: couldn't read all chars"); - s.reserve(s_size); - s.append(&buf2[0], s_size); return s; } |