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

feat(p/json): optimize json #2939

Merged
merged 11 commits into from
Oct 30, 2024
Merged

feat(p/json): optimize json #2939

merged 11 commits into from
Oct 30, 2024

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Oct 11, 2024

Description

Optimized the JSON package and simplified JSON node creation using the builder pattern.

  • in buffer.gno and escape.gno files are modified the use of map for lookup tables to use slice array instead.
  • refactor the Unquote function in escape.gno file
  • modified the existing functions that parsed numbers to use strconv package, and deleted related files and functions
    • especially, the eisel_lemire and ryu packages were deleted since they were files that had been added to handle ParseUint and ParseFloat in strconv package.

JSON Generate Example

Plain JSON

node := Builder().
	WithString("name", "Alice").
	WithNumber("age", 30).
	WithBool("is_student", false).
	Node()

value, err := Marshal(node)
if err != nil {
	t.Errorf("unexpected error: %s", err)
}

Output:
{"name":"Alice","age":30,"is_student":false}

Nested Structure

node := Builder().
	WriteString("name", "Alice").
	WriteObject("address", func(b *NodeBuilder) {
		b.WriteString("city", "New York").
		WriteNumber("zipcode", 10001)
	}).
	Node()
// ...

Output:
{"name":"Alice","address":{"city":"New York","zipcode":10001}}

Benchmark Result for Unquote

Before

BenchmarkUnquote-8              	12433488	        98.06 ns/op	     144 B/op	       2 allocs/op
BenchmarkUnquoteWorstCase-8     	24727736	        50.46 ns/op	      48 B/op	       1 allocs/op
BenchmarkUnquoteBestCase-8      	22542354	        52.69 ns/op	      48 B/op	       1 allocs/op
BenchmarkUnquoteEmptyString-8   	394868628	         3.067 ns/op	       0 B/op	       0 allocs/op

After

BenchmarkUnquote-8              	12464704	        96.61 ns/op	     144 B/op	       2 allocs/op
BenchmarkUnquoteWorstCase-8     	25084070	        48.02 ns/op	      48 B/op	       1 allocs/op
BenchmarkUnquoteBestCase-8      	23383227	        52.66 ns/op	      48 B/op	       1 allocs/op
BenchmarkUnquoteEmptyString-8   	400496838	         2.968 ns/op	       0 B/op	       0 allocs/op

@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Oct 11, 2024
Copy link

codecov bot commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 62.93%. Comparing base (93ece90) to head (f09e339).
Report is 40 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2939      +/-   ##
==========================================
- Coverage   63.09%   62.93%   -0.16%     
==========================================
  Files         563      568       +5     
  Lines       79273    82371    +3098     
==========================================
+ Hits        50015    51843    +1828     
- Misses      25893    27081    +1188     
- Partials     3365     3447      +82     
Flag Coverage Δ
contribs/gnodev 60.57% <ø> (-0.05%) ⬇️
contribs/gnofaucet 15.77% <ø> (+0.94%) ⬆️
gno.land 67.37% <ø> (-0.09%) ⬇️
gnovm 67.43% <ø> (+0.18%) ⬆️
misc/genstd 79.72% <ø> (ø)
tm2 62.43% <ø> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@notJoon notJoon marked this pull request as ready for review October 14, 2024 07:24
@notJoon notJoon requested review from a team as code owners October 14, 2024 07:24
@notJoon notJoon requested review from sw360cab and thehowl and removed request for a team October 14, 2024 07:24
@notJoon notJoon added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 14, 2024
@jefft0
Copy link
Contributor

jefft0 commented Oct 21, 2024

Hi @notJoon. Maybe it's obvious to people familiar with the code, but in the description can you put a short explanation why you removed the eisel_lemire and ryu folders?

@jefft0 jefft0 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 21, 2024
Copy link
Contributor

@jefft0 jefft0 left a comment

Choose a reason for hiding this comment

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

This is follow-on PR by the same author of the original package in #1415 . On my macOS, all tests pass with make test.components . Removed the review/triage-pending label. Ready for review by core devs.

@notJoon
Copy link
Member Author

notJoon commented Oct 22, 2024

Hi @notJoon. Maybe it's obvious to people familiar with the code, but in the description can you put a short explanation why you removed the eisel_lemire and ryu folders?

description updated. thanks!

examples/gno.land/p/demo/json/buffer.gno Outdated Show resolved Hide resolved
@thehowl
Copy link
Member

thehowl commented Oct 22, 2024

BTW the benchmarks are in go and are likely to mean very little in Gno ;)

At most, use -print-runtime-metrics for a comparison, even though it's still going to not be terribly useful to understand real-world CPU usage (yet).

@thehowl thehowl merged commit 494976d into gnolang:master Oct 30, 2024
119 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants