Skip to content

Use strings.Builder for querybuilder marshal method - #5008

Merged
vito merged 3 commits into
dagger:mainfrom
matiasinsaurralde:marshal-improvement
May 4, 2023
Merged

vito merged 3 commits into
dagger:mainfrom
matiasinsaurralde:marshal-improvement

Conversation

@matiasinsaurralde

Copy link
Copy Markdown
Contributor

Refactor marshal to use an approach similar to #5007.

Given this benchmark:

func BenchmarkMarshal(b *testing.B) {
	s := struct {
		A          string   `json:"a,omitempty"`
		B          int      `json:"b"`
		C          int      `json:"c`
		D          int      `json:"d"`
		BoolField  bool     `json:"bool_field"`
		SliceField []string `json:"slice_field"`
		Sub        struct {
			X []string `json:"x"`
		} `json:"sub"`
	}{
		A:          "test",
		B:          42,
		C:          1,
		D:          3,
		BoolField:  true,
		SliceField: []string{"string1", "string2"},
	}
	s.Sub.X = []string{"1"}
	for i := 0; i < b.N; i++ {
		MarshalGQL(context.TODO(), s)
	}
}

Running with the following command:

go test -benchmem -run=^$ -bench Marshal dagger.io/dagger/internal/querybuilder  -v -benchmem -count 10

And comparing main to this branch, with benchstat:

% benchstat old new
old:4: missing iteration count
new:4: missing iteration count
goos: darwin
goarch: arm64
pkg: dagger.io/dagger/internal/querybuilder
          │     old      │                 new                 │
          │    sec/op    │   sec/op     vs base                │
Marshal-8   14.071µ ± 8%   1.677µ ± 3%  -88.08% (p=0.000 n=10)

          │     old     │                new                 │
          │    B/op     │    B/op     vs base                │
Marshal-8   3877.0 ± 0%   872.0 ± 0%  -77.51% (p=0.000 n=10)

          │     old     │                new                 │
          │  allocs/op  │ allocs/op   vs base                │
Marshal-8   105.00 ± 0%   41.00 ± 0%  -60.95% (p=0.000 n=10)

benchcmp output:

% benchcmp old new
benchcmp is deprecated in favor of benchstat: https://pkg.go.dev/golang.org/x/perf/cmd/benchstat
benchmark              old ns/op     new ns/op     delta
BenchmarkMarshal-8     14057         1669          -88.13%
BenchmarkMarshal-8     14078         1666          -88.17%
BenchmarkMarshal-8     14062         1786          -87.30%
BenchmarkMarshal-8     14063         1726          -87.73%
BenchmarkMarshal-8     14038         1684          -88.00%
BenchmarkMarshal-8     13964         1681          -87.96%
BenchmarkMarshal-8     14212         1680          -88.18%
BenchmarkMarshal-8     14854         1670          -88.76%
BenchmarkMarshal-8     15526         1673          -89.22%
BenchmarkMarshal-8     15180         1674          -88.97%

benchmark              old allocs     new allocs     delta
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%
BenchmarkMarshal-8     105            41             -60.95%

benchmark              old bytes     new bytes     delta
BenchmarkMarshal-8     3878          872           -77.51%
BenchmarkMarshal-8     3877          872           -77.51%
BenchmarkMarshal-8     3877          872           -77.51%
BenchmarkMarshal-8     3877          872           -77.51%
BenchmarkMarshal-8     3877          872           -77.51%
BenchmarkMarshal-8     3877          872           -77.51%
BenchmarkMarshal-8     3877          872           -77.51%
BenchmarkMarshal-8     3878          872           -77.51%
BenchmarkMarshal-8     3878          872           -77.51%
BenchmarkMarshal-8     3878          872           -77.51%

@cpuguy83 cpuguy83 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Really nice speedup!

It seems like this is creating a strings.Builder for cases that it is not required, though (maybe the compiler optimizes that out?).

I also wonder if MarshalGQL could pass down a *strings.Builder to marshalValue and just have marshalValue use that recursively?

Comment thread sdk/go/internal/querybuilder/marshal.go Resolved
@matiasinsaurralde

matiasinsaurralde commented Apr 24, 2023 •

Copy link
Copy Markdown
Contributor Author

Really nice speedup!

It seems like this is creating a strings.Builder for cases that it is not required, though (maybe the compiler optimizes that out?).

I also wonder if MarshalGQL could pass down a *strings.Builder to marshalValue and just have marshalValue use that recursively?

That sounds good, I've tackled the first one on 7bbabea92859a4cc73b7f5613894906ed3626754 👍

The second idea sounds interesting too, will do some exploration and perhaps do another PR.

% benchcmp old new
benchcmp is deprecated in favor of benchstat: https://pkg.go.dev/golang.org/x/perf/cmd/benchstat
benchmark              old ns/op     new ns/op     delta
BenchmarkMarshal-8     1664          1557          -6.43%
BenchmarkMarshal-8     1654          1552          -6.17%
BenchmarkMarshal-8     1662          1556          -6.38%
BenchmarkMarshal-8     1738          1556          -10.47%
BenchmarkMarshal-8     1674          1554          -7.17%

benchmark              old allocs     new allocs     delta
BenchmarkMarshal-8     41             37             -9.76%
BenchmarkMarshal-8     41             37             -9.76%
BenchmarkMarshal-8     41             37             -9.76%
BenchmarkMarshal-8     41             37             -9.76%
BenchmarkMarshal-8     41             37             -9.76%

benchmark              old bytes     new bytes     delta
BenchmarkMarshal-8     872           840           -3.67%
BenchmarkMarshal-8     872           840           -3.67%
BenchmarkMarshal-8     872           840           -3.67%
BenchmarkMarshal-8     872           840           -3.67%
BenchmarkMarshal-8     872           840           -3.67%

@matiasinsaurralde
matiasinsaurralde force-pushed the marshal-improvement branch 11 times, most recently from 31bb7a9 to a2c61d5 Compare April 25, 2023 16:56
@matiasinsaurralde

Copy link
Copy Markdown
Contributor Author

Rebased and similar to #5007 in case you want to give it a review @vito 👍

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but curious if @aluzzardi has insight into the parallel marshaling we were doing before

return v.String(), nil
}
return fmt.Sprintf("%q", v.String()), nil //nolint:gosimple,staticcheck
return strconv.Quote(v.String()), nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Huh, I didn't realize we were just quoting Go strings to pass them to GraphQL - it seems like that could fail someday if they have different escape syntax, but I haven't found a way to break it yet, so 🤷‍♂️ guess it's fine

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My fears have come true: #5069

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right -- those should be escaped. Impressed this worked for so long.

eg, gctx := errgroup.WithContext(ctx)
for i := 0; i < n; i++ {
i := i
eg.Go(func() error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Interesting that we did these in parallel before, perhaps a micro-optimization but I wonder if there were cases where it helped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, totally missed this comment.

It's because of "pass by object".

When passing a *Container type, there's a custom marshaller doing the *Container --> ContainerID conversion which issues an API call: https://github.com/dagger/dagger/blob/main/sdk/go/api.gen.go#L79

The problem is worsened since this is a recursive: passing an array with 10 elements, each containing a struct with 10 fields will result in 100 sequential API calls. On the upside, I don't think the API currently has a scenario where that is possible.

There are two layers of parallelism:

  • querybuilder.go:marshalArguments(): For a given GraphQL call, process all arguments in parallel (e.g. if chaining 10 things -- take all passed arguments and resolve them at once)
  • this code: for each argument, resolve each slice and struct elements in parallel

Combined they make sure that all *Object->ObjectID resolution happens in parallel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@aluzzardi Ah - I didn't appreciate that marshaling involves sending an API request, that makes sense. Sounds important; will revert and reopen.

@vito vito mentioned this pull request May 2, 2023
@gerhard

gerhard commented May 4, 2023

Copy link
Copy Markdown
Contributor

Any other changes required before this gets merged? Would love to see this in by the end of this week.

Thanks for another fine improvement @matiasinsaurralde 💪

@vito

vito commented May 4, 2023

Copy link
Copy Markdown
Contributor

Will go ahead and merge - can't imagine dropping the parallel marshaling would have a huge impact, and the string escaping issue already existed before.

Thanks again!

@vito
vito merged commit 7a12edb into dagger:main May 4, 2023
vito added a commit that referenced this pull request May 5, 2023
vito added a commit that referenced this pull request May 5, 2023
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.

5 participants