Another thorough rewrite of library(json) to better preserve the complete set of answers as much as possible and thereby write more elegant and simple code. Some performance gains too!

This commit is contained in:
panasenco
2021-04-22 23:51:34 -07:00
parent 828550687c
commit 2cbc7e4f9a
4 changed files with 137 additions and 122 deletions

View File

@@ -1,5 +1,8 @@
## Benchmarks
These are honestly super flawed (single read/write of a single file) but that can at least capture movement on the
order of 0.1 s, and that's good enough for me.
### Read
With CLP(Z):
@@ -22,6 +25,12 @@ With first argument indexing optimizations:
% CPU time: 0.310 seconds
```
After making the code more general:
```
?- test_json:test_json_read.
% CPU time: 0.217 seconds
```
### Write
Without first argument indexing optimizations:
@@ -31,8 +40,13 @@ Without first argument indexing optimizations:
```
With first argument indexing optimizations:
```
?- test_json:test_json_minify.
% CPU time: 0.015 seconds
```
After making the code more general:
```
?- test_json:test_json_minify.
% CPU time: 0.013 seconds
```

View File

@@ -1 +1 @@
["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"":23456789012000000000000000000000000000000000000000000000000000000000000000000," s p a c e d ":[1,2,3,4,5,6,7],"# -- --> *\/":" ","\/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","E":12345678900000000000000000000000000.0,"address":"50 St. James Street","alpha":"abcdefghijklmnopqrstuvwyz","array":[],"backslash":"\\","comment":"\/\/ \/* <!-- --","compact":[1,2,3,4,5,6,7],"controls":"\b\f\n\r\t","digit":"0123456789","e":0.000000000000123456789,"false":false,"hex":"ģ䕧覫\u0001췯ꯍ\u001a","integer":1234567890,"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","null":null,"object":{},"one":1,"quote":"\"","quotes":"&#34; \" %22 0x22 034 &#x22;","real":-9876.54321,"slash":"\/ & \/","space":" ","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","true":true,"url":"http:\/\/www.JSON.org\/","zero":0},0.5,98.6,99.44,1066,"rosebud"]
["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":0.000000000000123456789,"E":12345678900000000000000000000000000.0,"":23456789012000000000000000000000000000000000000000000000000000000000000000000,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"ģ䕧覫\u0001췯ꯍ\u001a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]

View File

@@ -2,6 +2,7 @@
:- use_module(library(charsio)).
:- use_module(library(dcgs)).
:- use_module(library(format)).
:- use_module(library(json)).
:- use_module(library(lists)).
:- use_module(library(os)).
@@ -18,23 +19,31 @@ name_parse(Name, Json) :-
once(phrase_from_file(json_chars(Json), Path)).
test_json_read :-
name_parse("pass_null.json", _),
name_parse("pass_alnum.json", _),
name_parse("pass_special.json", _),
name_parse("pass_mandatory_escapes.json", _),
name_parse("pass_forward_slash.json", _),
name_parse("pass_hex.json", _),
name_parse("pass_smallfloat.json", _),
name_parse("pass_bigfloat.json", _),
name_parse("pass_null.json", null),
name_parse("pass_alnum.json", string("ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwyz0123456789")),
name_parse("pass_special.json", string("`1~!@#$%^&*()_+-={':[,]}|;.</>?")),
name_parse("pass_mandatory_escapes.json", string(" \" \\ \b\f\n\r\t ")),
name_parse("pass_forward_slash.json", string("/ & /")),
name_parse("pass_hex.json", string("ģ\x4567\\x89ab\\xcdef\\xabcd\\xef4a\")),
name_parse("pass_smallfloat.json", number(0.000000000000123456789)),
name_parse("pass_bigfloat.json", number(12345678900000000000000000000000000.0)),
time(name_parse("pass_everything.json", _)).
minify_sample_json :-
name_parse("pass_everything.json", Json),
time(once(phrase(json_chars(Json), MinChars))),
test_path("pass_everything.min.json", MinPath),
open(MinPath, write, Stream),
format(Stream, "~s", [MinChars]),
close(Stream).
test_json_minify :-
test_path("pass_everything.min.json", MinPath),
open(MinPath, read, RefMin),
read_line_to_chars(RefMin, RefChars, []),
close(RefMin),
open(MinPath, read, Stream),
read_line_to_chars(Stream, RefChars, []),
close(Stream),
name_parse("pass_everything.json", Json),
time(phrase(json_chars(Json), MinChars)),
time(once(phrase(json_chars(Json), MinChars))),
RefChars = MinChars.
test_json_int_float :-