Commit e8ac0f29 authored by rw's avatar rw

Fix heap allocation when reading a string.

parent a0d1161f
...@@ -26,7 +26,8 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT { ...@@ -26,7 +26,8 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT {
// String gets a string from data stored inside the flatbuffer. // String gets a string from data stored inside the flatbuffer.
func (t *Table) String(off UOffsetT) string { 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. // ByteVector gets a byte slice from data stored inside the flatbuffer.
......
...@@ -43,3 +43,8 @@ var ( ...@@ -43,3 +43,8 @@ var (
// SizeVOffsetT is the byte size of an VOffsetT. // SizeVOffsetT is the byte size of an VOffsetT.
SizeVOffsetT = int(unsafe.Sizeof(VOffsetT(0))) 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))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment