sile / jsone

Erlang JSON library
MIT License
291 stars 71 forks source link

Optimize encoding functions for unescaped strings #60

Closed sile closed 3 years ago

sile commented 3 years ago

This PR improves the encoding speed of unescaped strings (e.g., alphabets, numbers, some symbols) in JSON texts.

Example benchmarks

Before (the master branch)

$ git clone https://github.com/devinus/poison
$ rebar3 shell
> A = jsone:decode(element(2, file:read_file("poison/bench/data/blockchain.json"))).
> timer:tc(fun () -> lists:foreach(fun (_) -> jsone:encode(A) end, lists:seq(1, 1000)) end).
{636792,ok}  % 0.636s

> B = jsone:decode(element(2, file:read_file("poison/bench/data/issue-90.json"))).
> timer:tc(fun () -> lists:foreach(fun (_) -> jsone:encode(B) end, lists:seq(1, 10)) end).
{5247205,ok}  % 5.247s

After (this PR)

$ git clone https://github.com/devinus/poison
$ rebar3 shell
> A = jsone:decode(element(2, file:read_file("poison/bench/data/blockchain.json"))).
> timer:tc(fun () -> lists:foreach(fun (_) -> jsone:encode(A) end, lists:seq(1, 1000)) end).
{265974,ok} % 0.265s

> B = jsone:decode(element(2, file:read_file("poison/bench/data/issue-90.json"))).
> timer:tc(fun () -> lists:foreach(fun (_) -> jsone:encode(B) end, lists:seq(1, 10)) end).
{985962,ok}  % 0.985s