Skip to content

Conversation

@adelsz
Copy link
Contributor

@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.

@coveralls
Copy link

Coverage Status

coverage: 92.048%. remained the same
when pulling 15d7510 on adelsz:faster-printer-print
into f2b39ca on graphql-go:master.

@chris-ramon chris-ramon merged commit a546af7 into graphql-go:master Oct 12, 2024
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

3 participants