diff options
author | Ben Harper <rogojin@gmail.com> | 2015-03-08 21:39:01 +0200 |
---|---|---|
committer | Wouter van Oortmerssen <wvo@google.com> | 2015-03-11 17:27:39 -0700 |
commit | 6a0126340a3767cc6f4988b861f1340ce6a747ea (patch) | |
tree | a3f10c06fffcaab369cd5bbb4c0d0996efc6bfc9 /tests | |
parent | 4464405250ee9151177d3f4b88fb9959c924ad37 (diff) | |
download | flatbuffers-6a0126340a3767cc6f4988b861f1340ce6a747ea.tar.gz flatbuffers-6a0126340a3767cc6f4988b861f1340ce6a747ea.tar.bz2 flatbuffers-6a0126340a3767cc6f4988b861f1340ce6a747ea.zip |
Add CreateByteVector function to Go's builder
This function gets around the inefficiency of populating a [ubyte] vector
byte by byte. Since ubyte vectors are probably the most commonly used type
of generic byte buffer, this seems like a worthwhile thing to create a
fast path for.
Benchmarks show a 6x improvement in throughput on x64.
There is a new test verifying the functionality of the function.
Change-Id: I82e0228ae0f815dd7ea89bf168b8c1925f3ce0d7
Diffstat (limited to 'tests')
-rw-r--r-- | tests/go_test.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/go_test.go b/tests/go_test.go index a9496355..03eaddfa 100644 --- a/tests/go_test.go +++ b/tests/go_test.go @@ -90,6 +90,9 @@ func TestAll(t *testing.T) { // some sanity checks: CheckDocExample(generated, off, t.Fatalf) + // Check Builder.CreateByteVector + CheckCreateByteVector(t.Fatalf) + // If the filename of the FlatBuffers file generated by the Java test // is given, check that Go code can read it, and that Go code // generates an identical buffer when used to create the example data: @@ -1080,6 +1083,25 @@ func CheckDocExample(buf []byte, off flatbuffers.UOffsetT, fail func(string, ... _ = example.MonsterEnd(builder) } +func CheckCreateByteVector(fail func(string, ...interface{})) { + raw := [30]byte{} + for i := 0; i < len(raw); i++ { + raw[i] = byte(i) + } + + for size := 0; size < len(raw); size++ { + b1 := flatbuffers.NewBuilder(0) + b2 := flatbuffers.NewBuilder(0) + b1.StartVector(1, size, 1) + for i := size - 1; i >= 0; i-- { + b1.PrependByte(raw[i]) + } + b1.EndVector(size) + b2.CreateByteVector(raw[:size]) + CheckByteEquality(b1.Bytes, b2.Bytes, fail) + } +} + // Include simple random number generator to ensure results will be the // same cross platform. // http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator |