Skip to content

Commit

Permalink
Go: add CreateUninitializedVector and CreateUninitializedByteVector
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Apr 19, 2024
1 parent 7106d86 commit e77dfef
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
35 changes: 35 additions & 0 deletions go/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,41 @@ func (b *Builder) CreateByteVector(v []byte) UOffsetT {
return b.EndVector(len(v))
}

// CreateUninitializedVector is a low-level function to create a vector with
// uninitialized data. The arguments are the same as for StartVector.
//
// Return the offset of the vector, as well as the raw bytes of the vector,
// which can be filled in after this function returns.
//
// Beware that the returned slice is only valid as long as the underlying buffer
// is not reallocated. So the data should be written to the slice before adding
// anything to the builder. Another option is to ignore the []byte return value,
// and instead finish the whole buffer and then mutate it in-place.
func (b *Builder) CreateUninitializedVector(elemSize, numElems, alignment int) (UOffsetT, []byte) {
b.StartVector(elemSize, numElems, alignment)
l := UOffsetT(elemSize * numElems)

// Skip ahead over the elements that the vector would contain
b.head -= l

vecHead := b.head
return b.EndVector(numElems), b.Bytes[vecHead : vecHead+l]
}

// CreateUninitializedByteVector is a low-level function to create a byte vector with
// uninitialized data of a given length. It avoids a copy in CreateByteVector.
//
// Return the offset of the vector, as well as the raw bytes of the vector,
// which can be filled in after this function returns.
//
// Beware that the returned slice is only valid as long as the underlying buffer
// is not reallocated. So the data should be written to the slice before adding
// anything to the builder. Another option is to ignore the []byte return value,
// and instead finish the whole buffer and then mutate it in-place.
func (b *Builder) CreateUninitializedByteVector(numElems int) (UOffsetT, []byte) {
return b.CreateUninitializedVector(SizeByte, numElems, SizeByte)
}

func (b *Builder) assertNested() {
// If you get this assert, you're in an object while trying to write
// data that belongs outside of an object.
Expand Down
31 changes: 30 additions & 1 deletion tests/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ func TestAll(t *testing.T) {
// some sanity checks:
CheckDocExample(generated, off, t.Fatalf)

// Check Builder.CreateByteVector
// Check Builder.CreateByteVector and Builder.CreateUninitializedByteVector
CheckCreateByteVector(t.Fatalf)

// Check Builder.StartVector and Builder.CreateUninitializedVector
CheckUint64Vector(t.Fatalf)

// Check a parent namespace import
CheckParentNamespace(t.Fatalf)

Expand Down Expand Up @@ -1791,12 +1794,38 @@ func CheckCreateByteVector(fail func(string, ...interface{})) {
for size := 0; size < len(raw); size++ {
b1 := flatbuffers.NewBuilder(0)
b2 := flatbuffers.NewBuilder(0)
b3 := 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])
_, uninitialized := b3.CreateUninitializedByteVector(size)
copy(uninitialized, raw[:size])
CheckByteEquality(b1.Bytes, b2.Bytes, fail)
CheckByteEquality(b1.Bytes, b3.Bytes, fail)
}
}

func CheckUint64Vector(fail func(string, ...interface{})) {
raw := [30]uint64{}
for i := 0; i < len(raw); i++ {
raw[i] = 1234567890123456789 * uint64(i)
}

for size := 0; size < len(raw); size++ {
b1 := flatbuffers.NewBuilder(0)
b2 := flatbuffers.NewBuilder(0)
b1.StartVector(8, size, 8)
for i := size - 1; i >= 0; i-- {
b1.PrependUint64(raw[i])
}
b1.EndVector(size)
_, uninitialized := b2.CreateUninitializedVector(8, size, 8)
for i := 0; i < size; i++ {
flatbuffers.WriteUint64(uninitialized[8*i:], raw[i])
}
CheckByteEquality(b1.Bytes, b2.Bytes, fail)
}
}
Expand Down

0 comments on commit e77dfef

Please sign in to comment.