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
c127cf78
Commit
c127cf78
authored
May 20, 2015
by
rw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Go: CreateString now needs zero allocs.
Big speed boost for the typical use case of building with strings.
parent
6fffa2a1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
10 deletions
+25
-10
builder.go
go/builder.go
+9
-1
go_test.go
tests/go_test.go
+16
-9
No files found.
go/builder.go
View file @
c127cf78
...
...
@@ -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 @
c127cf78
...
...
@@ -533,7 +533,14 @@ 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
)
b
.
CreateString
(
"日本語"
)
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 +1270,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 +1282,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