Hello!
On Sat, Dec 6, 2014 at 2:30 PM, Puneet Agarwal wrote:
> I am using lua_cjson library which is shipped with openresty.
>
> When I profile my lua code with systemtap, I see lots of CPU time
> being utilized in cjson.encode/cjson.decode.
>
> I am wondering if cjson is utilizing luajit benefits or not?
Well, lua-cjson cannot be JIT compiled due to its use of classic Lua C
API. Shuxin Yang's lua-resty-json library [1] implements a JSON
decoder with FFI, which can be JIT compiled by LuaJIT. This new JSON
decoder can be 30% ~ 40% faster than lua-cjson for nontrivial JSON
inputs according to Shuxin's benchmarks. There might also be a faster
JSON encoder in this library in the future.
Generally speaking, such serialization and deserialization cannot be
very cheap. For example, serialization often involves (expensive)
hash-table iteration in Lua tables (which cannot be JIT compiled yet
even in LuaJIT v2.1 [2]) and de-serialization often involves quite
some (expensive) dynamic allocations of GC objects like Lua strings
and Lua tables.
So it's recommended to cache the Lua data structures (like complicated
nested Lua tables) via something like lua-resty-lrucache [3] to avoid
frequent serialization and de-serialization if you store the data in
shared dict or external services. It can help *a lot*.
Regards,
-agentzh
[1] https://github.com/yangshuxin/lua-resty-json
[2] CloudFlare might sponsor Mike Pall to implement JITting this NYI
in LuaJIT v2.1 in the near future.
[3] https://github.com/openresty/lua-resty-lrucache