Go: CreateString now needs zero allocs.

Big speed boost for the typical use case of building with strings.
This commit is contained in:
rw
2015-05-20 12:00:44 -07:00
parent 6fffa2a14d
commit c127cf78c2
2 changed files with 25 additions and 10 deletions

View File

@@ -281,7 +281,15 @@ 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))
b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte)
b.PlaceByte(0)
l := UOffsetT(len(s))
b.head -= l
copy(b.Bytes[b.head:b.head+l], s)
return b.EndVector(len(s))
}
// CreateByteString writes a byte slice as a string (null-terminated).