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

Add code generation for all wire types for stream encoding #500

Merged
merged 1 commit into from
Jun 23, 2021

Conversation

dianale31
Copy link
Contributor

@dianale31 dianale31 commented Jun 22, 2021

Creates the code generation for all the primitive types, enums, typedefs, structs, and container types. We use the same testing utils as the reader implementation in #496 (hopefully that will reduce rebase conflicts).

@dianale31 dianale31 requested review from abhinav and witriew June 22, 2021 01:27
@dianale31 dianale31 force-pushed the dianale/writer-codegen branch from cd55aec to 655203e Compare June 22, 2021 01:32
@dianale31 dianale31 requested a review from mh-park June 22, 2021 01:33
@dianale31 dianale31 force-pushed the dianale/writer-codegen branch 2 times, most recently from 3df00da to 32faf56 Compare June 22, 2021 02:01
@codecov
Copy link

codecov bot commented Jun 22, 2021

Codecov Report

Merging #500 (cf2414a) into streamdev (d97c94a) will decrease coverage by 7.75%.
The diff coverage is 20.35%.

Impacted file tree graph

@@              Coverage Diff              @@
##           streamdev     #500      +/-   ##
=============================================
- Coverage      79.23%   71.48%   -7.76%     
=============================================
  Files            128      129       +1     
  Lines          16167    18615    +2448     
=============================================
+ Hits           12810    13307     +497     
- Misses          2053     3642    +1589     
- Partials        1304     1666     +362     
Impacted Files Coverage Δ
gen/internal/tests/collision/collision.go 68.25% <0.00%> (-14.12%) ⬇️
gen/internal/tests/enum_conflict/enum_conflict.go 73.95% <0.00%> (-9.58%) ⬇️
.../internal/tests/hyphenated-file/hyphenated-file.go 69.35% <0.00%> (-14.96%) ⬇️
.../internal/tests/hyphenated_file/hyphenated_file.go 69.35% <0.00%> (-14.96%) ⬇️
...en/internal/tests/non_hyphenated/non_hyphenated.go 23.91% <0.00%> (-3.59%) ⬇️
gen/internal/tests/nozap/nozap.go 51.74% <0.00%> (-12.91%) ⬇️
gen/internal/tests/services/services.go 66.14% <0.00%> (-12.83%) ⬇️
gen/internal/tests/uuid_conflict/uuid_conflict.go 66.34% <0.00%> (-14.84%) ⬇️
internal/envelope/exception/exception.go 84.94% <0.00%> (-5.86%) ⬇️
gen/internal/tests/structs/structs.go 63.71% <4.59%> (-14.96%) ⬇️
... and 16 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 d97c94a...cf2414a. Read the comment docs.

@dianale31 dianale31 requested a review from r-hang June 22, 2021 02:03
@dianale31 dianale31 force-pushed the dianale/writer-codegen branch 2 times, most recently from 5f3a196 to 5c79c16 Compare June 22, 2021 02:14
@dianale31 dianale31 marked this pull request as draft June 22, 2021 02:15
@dianale31 dianale31 force-pushed the dianale/writer-codegen branch 4 times, most recently from d19e892 to 181e776 Compare June 22, 2021 02:40
@dianale31 dianale31 marked this pull request as ready for review June 22, 2021 02:43
@dianale31
Copy link
Contributor Author

Benchmark tests for stream encoding show significant savings for bytes allocated as well as CPU time:

BenchmarkRoundTrip/PrimitiveOptionalStruct/Encode-8  	  337783	      2997 ns/op	     704 B/op	       1 allocs/op
BenchmarkRoundTrip/PrimitiveOptionalStruct/Stream_Encode-8         	 1000000	      1059 ns/op	       0 B/op	       0 allocs/op
BenchmarkRoundTrip/PrimitiveOptionalStruct/Decode-8                	  385268	      3520 ns/op	    1400 B/op	      14 allocs/op
BenchmarkRoundTrip/Graph/Encode-8                                  	  328562	      3665 ns/op	    1705 B/op	      11 allocs/op
BenchmarkRoundTrip/Graph/Stream_Encode-8                           	  911430	      1483 ns/op	       0 B/op	       0 allocs/op
BenchmarkRoundTrip/Graph/Decode-8                                  	  158534	      7009 ns/op	    2776 B/op	      32 allocs/op
BenchmarkRoundTrip/ContainersOfContainers/Encode-8                 	   46626	     25660 ns/op	    1305 B/op	      18 allocs/op
BenchmarkRoundTrip/ContainersOfContainers/Stream_Encode-8          	   83979	     14972 ns/op	       0 B/op	       0 allocs/op
BenchmarkRoundTrip/ContainersOfContainers/Decode-8                 	   20068	     61197 ns/op	   12315 B/op	     164 allocs/op

@dianale31 dianale31 force-pushed the dianale/writer-codegen branch from 181e776 to 5003841 Compare June 22, 2021 06:12
@dianale31 dianale31 marked this pull request as draft June 22, 2021 06:52
@dianale31 dianale31 force-pushed the dianale/writer-codegen branch 2 times, most recently from 3a018a1 to f02915b Compare June 22, 2021 07:16
@dianale31 dianale31 marked this pull request as ready for review June 22, 2021 07:19
Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM

RE: benchmark: would you mind running a before and after and comparing them with https://pkg.go.dev/golang.org/x/perf/cmd/benchstat?

Makefile Outdated
@@ -127,7 +127,7 @@ space +=

.PHONY: cover
cover:
go test -v -race -covermode=atomic -coverprofile cover.full.out -coverpkg=./... ./...
go test -timeout 100s -v -race -covermode=atomic -coverprofile cover.full.out -coverpkg=./... ./...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was running into some issues with timeouts on the testing part of the build last night. But, it looks like it was a temporary thing. Removed!

gen/benchmark_test.go Outdated Show resolved Hide resolved
gen/field.go Outdated Show resolved Hide resolved
gen/stream.go Outdated Show resolved Hide resolved
idl/internal/lex.go Outdated Show resolved Hide resolved
gen/enum.go Outdated Show resolved Hide resolved
gen/enum.go Outdated Show resolved Hide resolved
decode bool
}{
{false, false},
//{false, true},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these cases supposed to be commented out vs removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out, this will eventually be merged with #496 which will provide the other half of the test suite.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 yep - I'll be taking care of the merging.

thriftType

Encode(stream.Writer) error
//Decode(stream.Reader) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out for now until we can merge #496 and this PR together.

@dianale31 dianale31 force-pushed the dianale/writer-codegen branch from e8c56e7 to 69aac3a Compare June 22, 2021 23:24
@dianale31
Copy link
Contributor Author

dianale31 commented Jun 22, 2021

Benchmarks for before and after adding the new stream implementation:

name                                        old time/op    new time/op    delta
RoundTrip/PrimitiveOptionalStruct/Encode-8    2.03µs ± 3%    2.17µs ± 2%    +6.94%  (p=0.008 n=5+5)
RoundTrip/PrimitiveOptionalStruct/Decode-8    2.99µs ± 6%    3.33µs ± 3%   +11.58%  (p=0.008 n=5+5)
RoundTrip/PrimitiveOptionalStruct/Stream_Encode-8            1.99µs ±11%
RoundTrip/Graph/Encode-8                      3.62µs ± 7%    3.81µs ± 2%      ~     (p=0.095 n=5+5)
RoundTrip/Graph/Decode-8                      6.00µs ± 2%   11.03µs ± 2%   +83.85%  (p=0.008 n=5+5)
RoundTrip/Graph/Stream_Encode-8                              2.41µs ± 2%
RoundTrip/ContainersOfContainers/Encode-8     23.8µs ± 4%    25.7µs ± 8%    +8.11%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Decode-8     53.8µs ± 2%   147.5µs ± 5%  +173.97%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Stream_Encode-8             16.9µs ± 3%

name                                        old alloc/op   new alloc/op   delta
RoundTrip/PrimitiveOptionalStruct/Encode-8      704B ± 0%      704B ± 0%      ~     (all equal)
RoundTrip/PrimitiveOptionalStruct/Decode-8    1.40kB ± 0%    1.45kB ± 0%    +3.43%  (p=0.008 n=5+5)
RoundTrip/PrimitiveOptionalStruct/Stream_Encode-8             0.00B 
RoundTrip/Graph/Encode-8                      1.70kB ± 0%    1.71kB ± 0%    +0.06%  (p=0.008 n=5+5)
RoundTrip/Graph/Decode-8                      2.78kB ± 0%    3.56kB ± 0%   +28.25%  (p=0.008 n=5+5)
RoundTrip/Graph/Stream_Encode-8                               0.00B
RoundTrip/ContainersOfContainers/Encode-8     1.30kB ± 0%    1.30kB ± 0%    +0.08%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Decode-8     12.3kB ± 0%    28.6kB ± 0%  +132.41%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Stream_Encode-8              0.00B

name                                        old allocs/op  new allocs/op  delta
RoundTrip/PrimitiveOptionalStruct/Encode-8      1.00 ± 0%      1.00 ± 0%      ~     (all equal)
RoundTrip/PrimitiveOptionalStruct/Decode-8      14.0 ± 0%      15.0 ± 0%    +7.14%  (p=0.008 n=5+5)
RoundTrip/PrimitiveOptionalStruct/Stream_Encode-8              0.00
RoundTrip/Graph/Encode-8                        11.0 ± 0%      11.0 ± 0%      ~     (all equal)
RoundTrip/Graph/Decode-8                        32.0 ± 0%      63.0 ± 0%   +96.88%  (p=0.008 n=5+5)
RoundTrip/Graph/Stream_Encode-8                                0.00
RoundTrip/ContainersOfContainers/Encode-8       18.0 ± 0%      18.0 ± 0%      ~     (all equal)
RoundTrip/ContainersOfContainers/Decode-8        164 ± 0%       837 ± 0%  +410.37%  (p=0.008 n=5+5)
RoundTrip/ContainersOfContainers/Stream_Encode-8               0.00

@dianale31 dianale31 requested review from abhinav and r-hang June 22, 2021 23:54
gen/field.go Outdated Show resolved Hide resolved
gen/roundtrip_test.go Outdated Show resolved Hide resolved
decode bool
}{
{false, false},
//{false, true},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 yep - I'll be taking care of the merging.

@dianale31 dianale31 force-pushed the dianale/writer-codegen branch 2 times, most recently from 8d4d71a to 3092932 Compare June 23, 2021 16:08
@dianale31 dianale31 force-pushed the dianale/writer-codegen branch from 3092932 to cf2414a Compare June 23, 2021 17:16
Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@dianale31 dianale31 merged commit 8405012 into thriftrw:streamdev Jun 23, 2021
@abhinav abhinav mentioned this pull request Aug 30, 2021
abhinav added a commit that referenced this pull request Aug 30, 2021
# Commits

The following comments are included in this release. Some of these
cherry-picked and released in v1.28.0, but they appear again in the
list above.

- protocol: Add streaming interfaces (#485)
- Move Stream-based interfaces into their own package
- Make Streaming interfaces private to allow for safe experimentation (#488)
- idl: Return structured ParseError from idl.Parse() (#492)
- Add CHANGELOG entry for #492 (#494)
- Support "<" in the templating language (#499)
- idl: add a Position struct to wrap reported lines (#497)
- Add streamwriter implementation (#490)
- Add a "StreamReader" which implements "stream.Reader"
- Use the "stream.Reader" in the "binary.Reader"
- Add code generation for all wire types for stream encoding (#500)
- Generate "Decode" for "enums" that will directly decode (#495)
- Provide "decode" code generation for the streaming variants for all other types (#496)
- idl: record document positions on constant nodes (#503)
- ast: move idl.Position to the ast package (#504)
- idl: replace internal.Position with ast.Position (#505)
- Expose stream protocol method to close Writer (#506)
- idl: add column numbers to parse error positions (#507)
- idl: record full positions for constants (#508)
- Mark assertParseCases() as a test helper (#509)
- protocol/stream: Define enveloping interfaces (#511)
- protocol/stream: Declare interface for encoding envelopes (#513)
- binary/StreamWriter: Borrow => New; unexport Return (#515)
- stream: add Close method, pool binary reader (#514)
- binary/reader: Return to pool after ReadValue (#517)
- binary/reader: Skip fixed width collections faster (#518)
- binary/stream/reader: Fast-path offsetReader skips (#519)
- binary: Move Responders and Protocol into package (#516)
- benchmark: Refactor into a suite (#520)
- Upgrade to Ragel version 6.10 (from 6.9) (#523)
- Responder: Deduplicate interface (#524)
- gen/quick_test: Add missing types (#525)
- enum/json: Support rejecting unknown values (#502)
- Back to development
- Upgrade to golang.org/x/tools version 0.1.5 (#529)
- ast: add column values to the AST nodes (#522)
- stream: Implement Request and Response handling with Enveloping (#526)
- offsetReader: Implement io.Seeker
- binary/ReadRequest: Use io.Seeker if available
- StreamReader: Use Seeker instead of offsetReader
- protocol/stream: Unembed stream.Protocol from stream.RequestReader (#532)
- thrifttest: Add mocks for streaming interfaces (#527)
- streaming: Unembed iface.Private in streaming-based interfaces (#533)
- Regenerate files for tests after merging `streamdev`
- ast: formally declare CppInclude as a Node (#536)
- ast: add Annotations(Node) []*Annotations (#537)
- Preparing release v1.29.0

# API changes

I ran apidiff on all packages in v1.28.0 and compared it with this
release. Removing changes to gen/internal/tests, the result is:

```
--- go.uber.org/thriftrw/ast ---
Compatible changes:
- Annotation.Column: added
- Annotations: added
- BaseType.Column: added
- Constant.Column: added
- ConstantList.Column: added
- ConstantMap.Column: added
- ConstantMapItem.Column: added
- ConstantReference.Column: added
- CppInclude.Column: added
- DefinitionInfo.Column: added
- Enum.Column: added
- EnumItem.Column: added
- Field.Column: added
- Function.Column: added
- Include.Column: added
- ListType.Column: added
- MapType.Column: added
- Namespace.Column: added
- Position.Column: added
- Position.String: added
- Service.Column: added
- ServiceReference.Column: added
- SetType.Column: added
- Struct.Column: added
- TypeReference.Column: added
- Typedef.Column: added

--- go.uber.org/thriftrw/envelope/stream ---
NEW PACKAGE

--- go.uber.org/thriftrw/gen ---
Compatible changes:
- StreamGenerator: added

--- go.uber.org/thriftrw/internal/envelope/exception ---
Compatible changes:
- (*ExceptionType).Decode: added
- (*TApplicationException).Decode: added
- (*TApplicationException).Encode: added
- ExceptionType.Encode: added

--- go.uber.org/thriftrw/plugin/api ---
Compatible changes:
- (*Argument).Decode: added
- (*Argument).Encode: added
- (*Feature).Decode: added
- (*Function).Decode: added
- (*Function).Encode: added
- (*GenerateServiceRequest).Decode: added
- (*GenerateServiceRequest).Encode: added
- (*GenerateServiceResponse).Decode: added
- (*GenerateServiceResponse).Encode: added
- (*HandshakeRequest).Decode: added
- (*HandshakeRequest).Encode: added
- (*HandshakeResponse).Decode: added
- (*HandshakeResponse).Encode: added
- (*Module).Decode: added
- (*Module).Encode: added
- (*ModuleID).Decode: added
- (*Plugin_Goodbye_Args).Decode: added
- (*Plugin_Goodbye_Args).Encode: added
- (*Plugin_Goodbye_Result).Decode: added
- (*Plugin_Goodbye_Result).Encode: added
- (*Plugin_Handshake_Args).Decode: added
- (*Plugin_Handshake_Args).Encode: added
- (*Plugin_Handshake_Result).Decode: added
- (*Plugin_Handshake_Result).Encode: added
- (*Service).Decode: added
- (*Service).Encode: added
- (*ServiceGenerator_Generate_Args).Decode: added
- (*ServiceGenerator_Generate_Args).Encode: added
- (*ServiceGenerator_Generate_Result).Decode: added
- (*ServiceGenerator_Generate_Result).Encode: added
- (*ServiceID).Decode: added
- (*SimpleType).Decode: added
- (*Type).Decode: added
- (*Type).Encode: added
- (*TypePair).Decode: added
- (*TypePair).Encode: added
- (*TypeReference).Decode: added
- (*TypeReference).Encode: added
- Feature.Encode: added
- ModuleID.Encode: added
- ServiceID.Encode: added
- SimpleType.Encode: added

--- go.uber.org/thriftrw/protocol ---
Compatible changes:
- BinaryStreamer: added

--- go.uber.org/thriftrw/protocol/binary ---
Compatible changes:
- Default: added
- EnvelopeV0Responder: added
- EnvelopeV1Responder: added
- NewStreamReader: added
- NewStreamWriter: added
- NoEnvelopeResponder: added
- Protocol: added
- Responder: added
- StreamReader: added
- StreamWriter: added

--- go.uber.org/thriftrw/protocol/envelope ---
NEW PACKAGE

--- go.uber.org/thriftrw/protocol/stream ---
NEW PACKAGE

--- go.uber.org/thriftrw/thrifttest/streamtest ---
NEW PACKAGE

--- go.uber.org/thriftrw/version ---
Incompatible changes:
- Version: value changed from "1.28.0" to "1.29.0"
```
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.

4 participants