summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2017-08-21 13:44:23 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2017-08-24 09:35:54 -0700
commitac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9 (patch)
treed45f46b9674d6b260a483d62eb2f9fc718935a9c /js
parent513958ea724dadbf55d502dd66d382657703e234 (diff)
downloadflatbuffers-ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9.tar.gz
flatbuffers-ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9.tar.bz2
flatbuffers-ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9.zip
Trimmed vtables of trailing zeroes.
This is something the format supports, but none of the builders were doing. Can save 10-20% on FlatBuffer binary size! Also fixed the Go tests. Change-Id: I616c56ce9bbcfcaee23aa24f0532fcb60b6a8c75 Tested: on Linux.
Diffstat (limited to 'js')
-rw-r--r--js/flatbuffers.js17
1 files changed, 11 insertions, 6 deletions
diff --git a/js/flatbuffers.js b/js/flatbuffers.js
index ccbd362a..4c2bf52d 100644
--- a/js/flatbuffers.js
+++ b/js/flatbuffers.js
@@ -604,23 +604,28 @@ flatbuffers.Builder.prototype.endObject = function() {
this.addInt32(0);
var vtableloc = this.offset();
+ // Trim trailing zeroes.
+ var i = this.vtable_in_use - 1;
+ for (; i >= 0 && this.vtable[i] == 0; i--) {}
+ var trimmed_size = i + 1;
+
// Write out the current vtable.
- for (var i = this.vtable_in_use - 1; i >= 0; i--) {
+ for (; i >= 0; i--) {
// Offset relative to the start of the table.
this.addInt16(this.vtable[i] != 0 ? vtableloc - this.vtable[i] : 0);
}
var standard_fields = 2; // The fields below:
this.addInt16(vtableloc - this.object_start);
- this.addInt16((this.vtable_in_use + standard_fields) * flatbuffers.SIZEOF_SHORT);
+ var len = (trimmed_size + standard_fields) * flatbuffers.SIZEOF_SHORT;
+ this.addInt16(len);
// Search for an existing vtable that matches the current one.
var existing_vtable = 0;
+ var vt1 = this.space;
outer_loop:
- for (var i = 0; i < this.vtables.length; i++) {
- var vt1 = this.bb.capacity() - this.vtables[i];
- var vt2 = this.space;
- var len = this.bb.readInt16(vt1);
+ for (i = 0; i < this.vtables.length; i++) {
+ var vt2 = this.bb.capacity() - this.vtables[i];
if (len == this.bb.readInt16(vt2)) {
for (var j = flatbuffers.SIZEOF_SHORT; j < len; j += flatbuffers.SIZEOF_SHORT) {
if (this.bb.readInt16(vt1 + j) != this.bb.readInt16(vt2 + j)) {