From 3dd54424c031ab91050760c0816f779bd4ccc76c Mon Sep 17 00:00:00 2001 From: rw Date: Sat, 9 May 2015 16:07:11 -0700 Subject: [PATCH] remove remaining allocs during build --- go/builder.go | 30 ++++++++++++++++++++++++------ tests/go_test.go | 3 ++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/go/builder.go b/go/builder.go index d8be20486..a61448d56 100644 --- a/go/builder.go +++ b/go/builder.go @@ -34,17 +34,35 @@ func NewBuilder(initialSize int) *Builder { // Reset truncates the underlying Builder buffer, facilitating alloc-free // reuse of a Builder. func (b *Builder) Reset() { - b.Bytes = b.Bytes[:0] b.head = UOffsetT(0) b.minalign = 1 - b.vtables = b.vtables[:0] + + if b.Bytes != nil { + b.Bytes = b.Bytes[:0] + } + + if b.vtables != nil { + b.vtables = b.vtables[:0] + } + + if b.vtable != nil { + b.vtable = b.vtable[:0] + } } // StartObject initializes bookkeeping for writing a new object. func (b *Builder) StartObject(numfields int) { b.notNested() // use 32-bit offsets so that arithmetic doesn't overflow. - b.vtable = make([]UOffsetT, numfields) + if cap(b.vtable) < numfields || b.vtable == nil { + b.vtable = make([]UOffsetT, numfields) + } else { + b.vtable = b.vtable[:numfields] + for i := 0 ; i < len(b.vtable) ; i++ { + b.vtable[i] = 0 + } + } + b.objectEnd = b.Offset() b.minalign = 1 } @@ -146,7 +164,7 @@ func (b *Builder) WriteVtable() (n UOffsetT) { SOffsetT(existingVtable)-SOffsetT(objectOffset)) } - b.vtable = nil + b.vtable = b.vtable[:0] return objectOffset } @@ -269,7 +287,7 @@ func (b *Builder) CreateString(s string) UOffsetT { return b.CreateByteString([]byte(s)) } -// CreateByteString writes a null-terminated byteslice as a vector. +// CreateByteString writes a byte slice as a string (null-terminated). func (b *Builder) CreateByteString(s []byte) UOffsetT { b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte) b.PlaceByte(0) @@ -297,7 +315,7 @@ func (b *Builder) CreateByteVector(v []byte) UOffsetT { func (b *Builder) notNested() { // Check that no other objects are being built while making this // object. If not, panic: - if b.vtable != nil { + if len(b.vtable) > 0 { panic("non-inline data write inside of object") } } diff --git a/tests/go_test.go b/tests/go_test.go index df887053d..36c30cbe2 100644 --- a/tests/go_test.go +++ b/tests/go_test.go @@ -1254,7 +1254,8 @@ func BenchmarkBuildGold(b *testing.B) { reuse_fred := []byte("Fred") b.SetBytes(bytes_length) - bldr := flatbuffers.NewBuilder(512) + bldr := flatbuffers.NewBuilder(0) + b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { bldr.Reset()