jiffy icon indicating copy to clipboard operation
jiffy copied to clipboard

Optimize encoding performance in Erlang

Open gasparch opened this issue 7 years ago • 6 comments

Right now jiffy requires keys in maps or keyword tuples to be strings to be successfully encoded.

If existing keys in map are integers - whole map should be recreated with keys converted to integers - which is huge overhead in terms of CPU/RAM usage.

This patch allows integer to be serialized into JSON as map's key as well.

May be it should be enabled only when some option is passed to encoder.

gasparch avatar Jun 29 '17 12:06 gasparch

@davisp ping :) would you consider including this optimization?

Idea is to avoid expensive operation of changing

%{
  1 => "val",
  2 => "val"
}

to

%{
  "1" => "val",
  "2" => "val"
}

before passing it to jiffy encoder.

:smiling_imp: Bad excuse is that JS JSON.stringify allows it :) :P :P

If yes - I'll rebase it on current master and also update docs.

gasparch avatar Nov 06 '17 15:11 gasparch

Python allows it as well so I'm not against it.

davisp avatar Nov 06 '17 16:11 davisp

Ok, did small fixes.

Do you want another branch + everything squashed in one commit?

gasparch avatar Nov 11 '17 20:11 gasparch

Just tried this and for me it fails for jiffy:encode(#{2 => 2, 1 => 1}).

** exception throw: {error,{invalid_object_member_key,1}} in function jiffy:encode/2 (/Users/mikael/work/infranet-erl/_build/default/lib/jiffy/src/jiffy.erl, line 99)

lixen avatar Sep 18 '18 14:09 lixen

jiffy:encode(#{2 => 2, 1 => 1}).

I'm not sure you are using version from this branch. I think it is not merged yet into main tree :(

gasparch avatar Oct 25 '18 22:10 gasparch

Would be nice to merge this

karlseguin avatar Jun 05 '21 06:06 karlseguin