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

Faster printer.Print #682

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

adelsz
Copy link

@adelsz adelsz commented Oct 25, 2023

In our project we do thousands of printer.Print calls per second, so its performance is critical for us.

As it is, printer.Print is rather slow, see issue #674 for some comparisons.
This is caused by its use of json.Marshal and json.Unmarshal as a way of converting interfaces to map[string]interface{} in the convertMap method.

This PR replaces the JSON logic with a custom, reflection based converter instead.

It also adds a benchmark to measure the performance impact.

Before the changes:

goos: darwin
goarch: arm64
pkg: github.com/graphql-go/graphql/language/printer
BenchmarkPrint
BenchmarkPrint-10    	     604	   1965835 ns/op
PASS

After the changes:

goos: darwin
goarch: arm64
pkg: github.com/graphql-go/graphql/language/printer
BenchmarkPrint
BenchmarkPrint-10    	    4900	    235592 ns/op
PASS

Roughly a 10x speedup. There might be some edge-cases I have missed, but looks like convertMap conforms to the behavior of its previous version.

Fixes #674

@coveralls
Copy link

Coverage Status

coverage: 92.051%. remained the same when pulling 732b137 on adelsz:faster-printer-print into 4ebf270 on graphql-go:master.

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.

Printing GraphQL documents is slow
2 participants