[Tarantool-patches] [PATCH 1/1] json: use cord_ibuf for encoding and decoding
Vladislav Shpilevoy
v.shpilevoy at tarantool.org
Mon May 24 18:49:45 MSK 2021
Hi! Thanks for the review!
On 24.05.2021 12:04, Oleg Babin wrote:
> Hi! Thanks for your patch.
>
>
> I see strange effect. After a patch following script:
>
> ```
>
> for i = 1, 1e9 do pcall(json.encode, function() end) end
>
> ```
>
> produces quite strange effects with memory. After some time
>
> my system kills a process - also I see in htop that process consumes about 20% of memory.
>
> In contrast before the patch process uses 0.1% of memory and doesn't have any oscillations
>
> in "VIRT" and "RES" columns. Yes, it's a negative case but I believe such behaviour shouldn't be affected as well.
This is happening because you didn't do any yields. Cord buffer is freed
automatically when a yield happens. This is a workaround for not being
able to use a global buffer, which wouldn't need freeing at all.
This is a known issue with the cord buffer, and the only working alternative
I see is to wrap all related Lua C calls into lua_pcall(). This leads to
perf issues for the success case, because pcall does more work; because
you usually need to re-push the arguments; and because pcall is not jitted
AFAIK. For instance about arguments re-push, to use lua_pcall() in lua_cjson
in json_encode() I would need to push the Lua json.encode(...) arguments on
the stack again.
I couldn't find any good solution for the error-case so far. The same issue
exists now with all the code which used IBUF_SHARED/tarantool_ibuf and now
uses cord_ibuf_take()/put(). It does not justify the problem though.
I was thinking about using pcall anyway; about pushing a GC function on Lua
stack to free the cord buffer; about having a global buffer for normal context
and another global buffer per each level of GC recursion. The last idea is
not possible to implement due to lack of a concept of GC level in our Lua
implementation. The other ideas are going to hit the perf for the success
case. All looks bad.
Your particular example started working when I added a yield every 10k
encodes.
>> diff --git a/third_party/lua-cjson/lua_cjson.c b/third_party/lua-cjson/lua_cjson.c
>> index 38e999870..85186d6d5 100644
>> --- a/third_party/lua-cjson/lua_cjson.c
>> +++ b/third_party/lua-cjson/lua_cjson.c
>> @@ -182,9 +177,6 @@ static int json_destroy_config(lua_State *l)
>> static void json_create_tokens()
>> {
>> int i;
>> -#if DEFAULT_ENCODE_KEEP_BUFFER > 0
>> - strbuf_init(&encode_buf, 0);
>> -#endif
>> /* Decoding init */
>> @@ -444,7 +436,9 @@ static int json_encode(lua_State *l) {
>> "expected 1 or 2 arguments");
>> /* Reuse existing buffer. */
>> - strbuf_reset(&encode_buf);
>> + strbuf_t encode_buf;
>> + struct ibuf *ibuf = cord_ibuf_take();
>> + strbuf_create(&encode_buf, -1, ibuf);
>
> Maybe it's better to use "0" here. I know it has the same effect but usually 0 is default value. But up to you.
0 looks like "do not pre-allocate anything". I used the default value
explicitly now:
====================
@@ -438,7 +438,7 @@ static int json_encode(lua_State *l) {
/* Reuse existing buffer. */
strbuf_t encode_buf;
struct ibuf *ibuf = cord_ibuf_take();
- strbuf_create(&encode_buf, -1, ibuf);
+ strbuf_create(&encode_buf, STRBUF_DEFAULT_SIZE, ibuf);
struct luaL_serializer *cfg = luaL_checkserializer(l);
if (lua_gettop(l) == 2) {
====================
More information about the Tarantool-patches
mailing list