Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: User specified initial capacity for slices #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ generate: build
./tests/nothing.go \
./tests/errors.go \
./tests/html.go \
./tests/type_declaration_skip.go
./tests/type_declaration_skip.go \
./tests/init_cap.go
bin/easyjson \
./tests/nested_easy.go \
./tests/named_type.go \
Expand Down
4 changes: 3 additions & 1 deletion gen/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
fmt.Fprintln(g.out, ws+"}")

} else {

capacity := 1
if elem.Size() > 0 {
capacity = minSliceBytes / int(elem.Size())
}
if tags.initialCapacity > 0 {
capacity = tags.initialCapacity
}

fmt.Fprintln(g.out, ws+"if in.IsNull() {")
fmt.Fprintln(g.out, ws+" in.Skip()")
Expand Down
29 changes: 22 additions & 7 deletions gen/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ var primitiveStringEncoders = map[reflect.Kind]string{
type fieldTags struct {
name string

omit bool
omitEmpty bool
noOmitEmpty bool
asString bool
required bool
intern bool
noCopy bool
omit bool
omitEmpty bool
noOmitEmpty bool
asString bool
required bool
intern bool
noCopy bool
initialCapacity int
}

const defaultCapacity int = -1

// parseFieldTags parses the json field tag into a structure.
func parseFieldTags(f reflect.StructField) fieldTags {
var ret fieldTags
ret.initialCapacity = defaultCapacity

for i, s := range strings.Split(f.Tag.Get("json"), ",") {
switch {
Expand All @@ -87,6 +91,17 @@ func parseFieldTags(f reflect.StructField) fieldTags {
}
}

for i, s := range strings.Split(f.Tag.Get("initialCap"), ",") {
switch {
case i == 0:
capacity, err := strconv.Atoi(s)
if err != nil || capacity <= 0 {
capacity = defaultCapacity
}
ret.initialCapacity = capacity
}
}

return ret
}

Expand Down
5 changes: 4 additions & 1 deletion tests/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var testCases = []struct {
{&myTypeDeclaredValue, myTypeDeclaredString},
{&myTypeNotSkippedValue, myTypeNotSkippedString},
{&intern, internString},
{&noInitCap, noInitCapString},
{&initCap, initCapString},
{&invalidInitCap, invalidInitCapString},
}

func TestMarshal(t *testing.T) {
Expand Down Expand Up @@ -243,7 +246,7 @@ func TestNestedMarshaler(t *testing.T) {
t.Errorf("Can't marshal NestedMarshaler: %s", err)
}

s2 := NestedMarshaler {
s2 := NestedMarshaler{
Value: &StructWithMarshaler{},
}

Expand Down
25 changes: 25 additions & 0 deletions tests/init_cap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tests

//easyjson:json
type NoInitCap struct {
Field []string `json:"field"`
}

//easyjson:json
type InitCap struct {
Field []string `json:"field" initialCap:"10"`
}

//easyjson:json
type InvalidInitCap struct {
Field []string `json:"field" initialCap:"-3"`
}

var noInitCap = NoInitCap{Field: []string{"elem"}}
var noInitCapString = `{"field":["elem"]}`

var initCap = InitCap{Field: []string{"elem"}}
var initCapString = `{"field":["elem"]}`

var invalidInitCap = InvalidInitCap{Field: []string{"elem"}}
var invalidInitCapString = `{"field":["elem"]}`
40 changes: 40 additions & 0 deletions tests/init_cap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tests

import (
"github.com/mailru/easyjson"
"testing"
)

func TestTagInitialCapacity(t *testing.T) {
data := []byte(`{"field":["elem"]}`)

var n NoInitCap
err := easyjson.Unmarshal(data, &n)
if err != nil {
t.Error(err)
}

if cap(n.Field) != 4 {
t.Fatalf("wrong slice capacity: %d", cap(n.Field))
}

var c InitCap
err = easyjson.Unmarshal(data, &c)
if err != nil {
t.Error(err)
}

if cap(c.Field) != 10 {
t.Fatalf("wrong slice capacity: %d", cap(c.Field))
}

var i InvalidInitCap
err = easyjson.Unmarshal(data, &i)
if err != nil {
t.Error(err)
}

if cap(i.Field) != 4 {
t.Fatalf("wrong slice capacity: %d", cap(i.Field))
}
}