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

WIP: gen/FromWire: Allocate pointer fields once #476

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented May 18, 2021

In FromWire, when we decode pointer fields, our code takes the form,

name := value.GetString()  // Given (value wire.Value)
user.Name = &name

This results in an allocation for every optional field present over the
wire.

name := value.GetString()
user.Name = &name  // alloc

// ...

email := value.GetString()
user.Email = &email  // alloc

// ...

age := value.GetInt8()
user.Age = &age  // alloc

This changes how we generate FromWire for structs to instead allocate
space for all pointer fields once, and then re-use the same block.

var ptrFields struct {
    Name string
    Email string
    Age int8
}  // alloc

ptrFields.Name = value.GetString()
user.Name = &ptrFields.Name  // no alloc

ptrFields.Email = value.GetString()
user.Email = &ptrFields.Email  // no alloc

ptrFields.Age = value.GetInt8()
user.Age = &ptrFields.Age  // no alloc

Note that the pre-allocated struct also includes nested struct fields,
so we can avoid that allocation too.

user.Comment = _Comment_Read(value)
    // func _Comment_Read(value wire.Value) *Comment {
    //   var c Comment
    //   c.FromWire(value)
    //   return &c  // alloc
    // }

// Becomes,

var ptrFields struct {
    // ...
    Comment Comment
}

ptrFields.Comment.FromWire(value)
user.Comment = &ptrFields.Comment  // no alloc

This will have the following effects:

Structs with optional primitive fields, or other structs inside them
will go from N small allocations to one big allocation. For most
structs, the total amount of space allocated will remain largely the
same. However, for sparse structs with lots of optional fields where
only a handful of them are set, this will allocate more bytes and
therefore, hold onto more memory than they need.

We think that since the majority case is going to be structs with most
of their fields filled in, reducing the number of allocations takes
precedence.

Performance:

$ git co master
$ go test -run '^$' -bench ././Decode -benchmem -count 5 > before.txt
$ git co -
$ go test -run '^$' -bench ././Decode -benchmem -count 5 > after.txt
$ benchstat before.txt after.txt
name                                        old time/op    new time/op    delta
RoundTrip/PrimitiveOptionalStruct/Decode-4    2.93µs ± 5%    2.81µs ± 6%     ~     (p=0.095 n=5+5)
RoundTrip/Graph/Decode-4                      7.30µs ±17%    6.16µs ± 8%  -15.60%  (p=0.032 n=5+5)
RoundTrip/ContainersOfContainers/Decode-4     52.7µs ± 1%    57.7µs ±12%     ~     (p=0.730 n=4+5)

name                                        old alloc/op   new alloc/op   delta
RoundTrip/PrimitiveOptionalStruct/Decode-4    1.40kB ± 0%    1.41kB ± 0%   +0.43%  (p=0.008 n=5+5)
RoundTrip/Graph/Decode-4                      2.78kB ± 0%    2.78kB ± 0%     ~     (all equal)
RoundTrip/ContainersOfContainers/Decode-4     12.3kB ± 0%    12.3kB ± 0%     ~     (p=0.881 n=5+5)

name                                        old allocs/op  new allocs/op  delta
RoundTrip/PrimitiveOptionalStruct/Decode-4      14.0 ± 0%       8.0 ± 0%  -42.86%  (p=0.008 n=5+5)
RoundTrip/Graph/Decode-4                        32.0 ± 0%      29.0 ± 0%   -9.38%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Decode-4        164 ± 0%       164 ± 0%     ~     (p=0.556 n=5+4)

@codecov
Copy link

codecov bot commented May 18, 2021

Codecov Report

Merging #476 (f1e6392) into dev (14808ba) will increase coverage by 0.12%.
The diff coverage is 99.49%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #476      +/-   ##
==========================================
+ Coverage   79.02%   79.14%   +0.12%     
==========================================
  Files         129      129              
  Lines       16092    16260     +168     
==========================================
+ Hits        12716    12869     +153     
- Misses       2066     2081      +15     
  Partials     1310     1310              
Impacted Files Coverage Δ
...en/internal/tests/non_hyphenated/non_hyphenated.go 29.54% <50.00%> (+2.04%) ⬆️
gen/field.go 89.45% <100.00%> (+0.38%) ⬆️
gen/internal/tests/collision/collision.go 82.65% <100.00%> (+0.28%) ⬆️
gen/internal/tests/containers/containers.go 71.11% <100.00%> (+0.24%) ⬆️
gen/internal/tests/enum_conflict/enum_conflict.go 83.72% <100.00%> (+0.19%) ⬆️
gen/internal/tests/enums/enums.go 86.64% <100.00%> (+0.03%) ⬆️
gen/internal/tests/exceptions/exceptions.go 83.42% <100.00%> (+0.58%) ⬆️
.../internal/tests/hyphenated-file/hyphenated-file.go 84.61% <100.00%> (+0.30%) ⬆️
.../internal/tests/hyphenated_file/hyphenated_file.go 84.61% <100.00%> (+0.30%) ⬆️
gen/internal/tests/nozap/nozap.go 64.80% <100.00%> (+0.14%) ⬆️
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 14808ba...f1e6392. Read the comment docs.

In FromWire, when we decode pointer fields, our code takes the form,

```
name := value.GetString()  // Given (value wire.Value)
user.Name = &name
```

This results in an allocation for every optional field present over the
wire.

    name := value.GetString()
    user.Name = &name  // alloc

    // ...

    email := value.GetString()
    user.Email = &email  // alloc

    // ...

    age := value.GetInt8()
    user.Age = &age  // alloc

This changes how we generate FromWire for structs to instead allocate
space for all pointer fields once, and then re-use the same block.

    var ptrFields struct {
        Name string
        Email string
        Age int8
    }  // alloc

    ptrFields.Name = value.GetString()
    user.Name = &ptrFields.Name  // no alloc

    ptrFields.Email = value.GetString()
    user.Email = &ptrFields.Email  // no alloc

    ptrFields.Age = value.GetInt8()
    user.Age = &ptrFields.Age  // no alloc

Note that the pre-allocated struct also includes nested struct fields,
so we can avoid that allocation too.

    user.Comment = _Comment_Read(value)
        // func _Comment_Read(value wire.Value) *Comment {
        //   var c Comment
        //   c.FromWire(value)
        //   return &c  // alloc
        // }

    // Becomes,

    var ptrFields struct {
        // ...
        Comment Comment
    }

    ptrFields.Comment.FromWire(value)
    user.Comment = &ptrFields.Comment  // no alloc

This will have the following effects:

Structs with optional primitive fields, or other structs inside them
will go from N small allocations to one big allocation. For most
structs, the total amount of space allocated will remain largely the
same. However, for sparse structs with lots of optional fields where
only a handful of them are set, this will allocate more bytes and
therefore, hold onto more memory than they need.

We think that since the majority case is going to be structs with most
of their fields filled in, reducing the number of allocations takes
precedence.

Performance:

```
$ git co master
$ go test -run '^$' -bench ././Decode -benchmem -count 5 > before.txt
$ git co -
$ go test -run '^$' -bench ././Decode -benchmem -count 5 > after.txt
$ benchstat before.txt after.txt
name                                        old time/op    new time/op    delta
RoundTrip/PrimitiveOptionalStruct/Decode-4    2.93µs ± 5%    2.81µs ± 6%     ~     (p=0.095 n=5+5)
RoundTrip/Graph/Decode-4                      7.30µs ±17%    6.16µs ± 8%  -15.60%  (p=0.032 n=5+5)
RoundTrip/ContainersOfContainers/Decode-4     52.7µs ± 1%    57.7µs ±12%     ~     (p=0.730 n=4+5)

name                                        old alloc/op   new alloc/op   delta
RoundTrip/PrimitiveOptionalStruct/Decode-4    1.40kB ± 0%    1.41kB ± 0%   +0.43%  (p=0.008 n=5+5)
RoundTrip/Graph/Decode-4                      2.78kB ± 0%    2.78kB ± 0%     ~     (all equal)
RoundTrip/ContainersOfContainers/Decode-4     12.3kB ± 0%    12.3kB ± 0%     ~     (p=0.881 n=5+5)

name                                        old allocs/op  new allocs/op  delta
RoundTrip/PrimitiveOptionalStruct/Decode-4      14.0 ± 0%       8.0 ± 0%  -42.86%  (p=0.008 n=5+5)
RoundTrip/Graph/Decode-4                        32.0 ± 0%      29.0 ± 0%   -9.38%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Decode-4        164 ± 0%       164 ± 0%     ~     (p=0.556 n=5+4)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant