mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
Reduce allocations when building strings.
Builder has a new CreateByteString function that writes a null-terimnated byte slice to the buffer. This results in zero allocations for writing strings.
This commit is contained in:
@@ -247,16 +247,20 @@ func (b *Builder) EndVector(vectorNumElems int) UOffsetT {
|
||||
|
||||
// CreateString writes a null-terminated string as a vector.
|
||||
func (b *Builder) CreateString(s string) UOffsetT {
|
||||
return b.CreateByteString([]byte(s))
|
||||
}
|
||||
|
||||
// CreateByteString writes a null-terminated byteslice as a vector.
|
||||
func (b *Builder) CreateByteString(s []byte) UOffsetT {
|
||||
b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte)
|
||||
b.PlaceByte(0)
|
||||
|
||||
x := []byte(s)
|
||||
l := UOffsetT(len(x))
|
||||
l := UOffsetT(len(s))
|
||||
|
||||
b.head -= l
|
||||
copy(b.Bytes[b.head:b.head+l], x)
|
||||
copy(b.Bytes[b.head:b.head+l], s)
|
||||
|
||||
return b.EndVector(len(x))
|
||||
return b.EndVector(len(s))
|
||||
}
|
||||
|
||||
// CreateByteVector writes a ubyte vector
|
||||
|
||||
Reference in New Issue
Block a user