summaryrefslogtreecommitdiff
path: root/go
diff options
context:
space:
mode:
authorrw <me@rwinslow.com>2016-04-23 12:31:24 -0700
committerrw <me@rwinslow.com>2016-04-23 12:32:43 -0700
commite8ac0f293e5e8a957e95c5ae72eefb0bf4d1be0e (patch)
treee1f84e6c806e3ef1e1a8a8db5ce5fbd7b0f8200b /go
parenta0d1161feb49e6db4027066134b212b7446fdc66 (diff)
downloadflatbuffers-e8ac0f293e5e8a957e95c5ae72eefb0bf4d1be0e.tar.gz
flatbuffers-e8ac0f293e5e8a957e95c5ae72eefb0bf4d1be0e.tar.bz2
flatbuffers-e8ac0f293e5e8a957e95c5ae72eefb0bf4d1be0e.zip
Fix heap allocation when reading a string.
Diffstat (limited to 'go')
-rw-r--r--go/table.go3
-rw-r--r--go/unsafe.go5
2 files changed, 7 insertions, 1 deletions
diff --git a/go/table.go b/go/table.go
index 695b92da..976a7dba 100644
--- a/go/table.go
+++ b/go/table.go
@@ -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.
diff --git a/go/unsafe.go b/go/unsafe.go
index e90ccb09..566e8b0b 100644
--- a/go/unsafe.go
+++ b/go/unsafe.go
@@ -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))
+}