gno icon indicating copy to clipboard operation
gno copied to clipboard

feat(p/json): optimize json

Open notJoon opened this issue 4 months ago • 4 comments

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

notJoon avatar Oct 11 '24 08:10 notJoon