Compute a tighter upper bound on the buffer needed for packed encoding

This commit is contained in:
Drew Fisher committed 2016-02-18 15:48:41 -08:00
1 parent 09c79de07d
commit ccdb72c931
1 file changed
+3 -1
+3 -1
View File
@@ -1778,7 +1778,9 @@ v8::Handle<v8::Value> toBytesPacked(const v8::Arguments& args) {
return liftKj([&]() -> v8::Handle<v8::Value> {
auto unpackedSize = computeSerializedSizeInWords(builder.message) * sizeof(capnp::word);
auto packedSizeUpperBound = unpackedSize * 258 / 256;
// Packed encoding can add 2 bytes of overhead per 2k segment reached.
auto packedOverhead = (((unpackedSize - 1) / 2048) + 1) * 2;
auto packedSizeUpperBound = unpackedSize + packedOverhead;
auto oversizedBuf = kj::heapArray<byte>(packedSizeUpperBound);
kj::ArrayOutputStream outputStream(oversizedBuf);
capnp::writePackedMessage(outputStream, builder.message);