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

encoding/json: json.Decoder Token method is two times slow than Decode #58786

Closed
bigpigeon opened this issue Feb 28, 2023 · 3 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@bigpigeon
Copy link

question

I need Depth limit decode function with json data

but std json did not support, I want to use json.Decoder.Token to implement it

performance test

I write below benchmark to test it's performance

https://gist.github.com/bigpigeon/93f90fe84cf96f10356541c22db6e7dc

# go test -test.bench ".*" decode_test.go 
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
BenchmarkJsonDecode-6   	  331059	      4878 ns/op	    3984 B/op	      44 allocs/op
BenchmarkGetToken-6     	  146536	      7969 ns/op	    3064 B/op	     135 allocs/op
PASS
ok  	command-line-arguments	2.907s

the getAllToken is even slower than Decode

What version of Go are you using (go version)?

$ go version
go version go1.19.1 linux/amd64

@dmitshur dmitshur added Performance NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Feb 28, 2023
@dmitshur dmitshur added this to the Backlog milestone Feb 28, 2023
@dmitshur
Copy link
Contributor

CC @dsnet, @bradfitz, @mvdan.

@dsnet
Copy link
Member

dsnet commented Feb 28, 2023

Closing as a duplicate of #40128. A huge part of it's slowness is the fact that it allocates.

@dsnet dsnet closed this as completed Feb 28, 2023
@dsnet
Copy link
Member

dsnet commented Feb 28, 2023

On a different note, if you want to a propose a Decoder.SetDepthLimit method, that can filed separately and can be considered.

@golang golang locked and limited conversation to collaborators Feb 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

4 participants