Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
2535a3aa
Commit
2535a3aa
authored
May 20, 2015
by
Robert
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #206 from rw/go-faster-string-writing
Go: CreateString now needs zero allocs.
parents
6fffa2a1
0894c25f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
10 deletions
+29
-10
builder.go
go/builder.go
+9
-1
go_test.go
tests/go_test.go
+20
-9
No files found.
go/builder.go
View file @
2535a3aa
...
...
@@ -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).
...
...
tests/go_test.go
View file @
2535a3aa
...
...
@@ -533,7 +533,18 @@ func CheckByteLayout(fail func(string, ...interface{})) {
check
([]
byte
{
4
,
0
,
0
,
0
,
'm'
,
'o'
,
'o'
,
'p'
,
0
,
0
,
0
,
0
,
// 0-terminated, 3-byte pad
3
,
0
,
0
,
0
,
'f'
,
'o'
,
'o'
,
0
})
// test 6b: CreateByteString
// test 6b: CreateString unicode
b
=
flatbuffers
.
NewBuilder
(
0
)
// These characters are chinese from blog.golang.org/strings
// We use escape codes here so that editors without unicode support
// aren't bothered:
uni_str
:=
"
\u65e5\u672c\u8a9e
"
b
.
CreateString
(
uni_str
)
check
([]
byte
{
9
,
0
,
0
,
0
,
230
,
151
,
165
,
230
,
156
,
172
,
232
,
170
,
158
,
0
,
// null-terminated, 2-byte pad
0
,
0
})
// test 6c: CreateByteString
b
=
flatbuffers
.
NewBuilder
(
0
)
b
.
CreateByteString
([]
byte
(
"foo"
))
...
...
@@ -1263,10 +1274,10 @@ func BenchmarkBuildGold(b *testing.B) {
buf
,
offset
:=
CheckGeneratedBuild
(
b
.
Fatalf
)
bytes_length
:=
int64
(
len
(
buf
[
offset
:
]))
reuse_str
:=
[]
byte
(
"MyMonster"
)
reuse_test1
:=
[]
byte
(
"test1"
)
reuse_test2
:=
[]
byte
(
"test2"
)
reuse_fred
:=
[]
byte
(
"Fred"
)
reuse_str
:=
"MyMonster"
reuse_test1
:=
"test1"
reuse_test2
:=
"test2"
reuse_fred
:=
"Fred"
b
.
SetBytes
(
bytes_length
)
bldr
:=
flatbuffers
.
NewBuilder
(
0
)
...
...
@@ -1275,10 +1286,10 @@ func BenchmarkBuildGold(b *testing.B) {
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
bldr
.
Reset
()
str
:=
bldr
.
Create
Byte
String
(
reuse_str
)
test1
:=
bldr
.
Create
Byte
String
(
reuse_test1
)
test2
:=
bldr
.
Create
Byte
String
(
reuse_test2
)
fred
:=
bldr
.
Create
Byte
String
(
reuse_fred
)
str
:=
bldr
.
CreateString
(
reuse_str
)
test1
:=
bldr
.
CreateString
(
reuse_test1
)
test2
:=
bldr
.
CreateString
(
reuse_test2
)
fred
:=
bldr
.
CreateString
(
reuse_fred
)
example
.
MonsterStartInventoryVector
(
bldr
,
5
)
bldr
.
PrependByte
(
4
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment