Merge pull request #3859 from rw/go-zero-alloc-strings

Go: Fix heap allocation when reading a string.
This commit is contained in:
Robert
2016-04-23 18:38:03 -07:00
2 changed files with 7 additions and 1 deletions

View File

@@ -26,7 +26,8 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT {
// String gets a string from data stored inside the flatbuffer.
func (t *Table) String(off UOffsetT) string {
return string(t.ByteVector(off))
b := t.ByteVector(off)
return byteSliceToString(b)
}
// ByteVector gets a byte slice from data stored inside the flatbuffer.

View File

@@ -43,3 +43,8 @@ var (
// SizeVOffsetT is the byte size of an VOffsetT.
SizeVOffsetT = int(unsafe.Sizeof(VOffsetT(0)))
)
// byteSliceToString converts a []byte to string without a heap allocation.
func byteSliceToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}