Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Oleg Babin <olegrok@tarantool.org>,
	tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org
Subject: Re: [Tarantool-patches] [PATCH 1/1] json: use cord_ibuf for encoding and decoding
Date: Mon, 24 May 2021 17:49:45 +0200	[thread overview]
Message-ID: <06d4c252-3f41-d91b-6943-ec4cca520a99@tarantool.org> (raw)
In-Reply-To: <6e775936-55ac-066e-c68f-743890f49a07@tarantool.org>

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) {
====================

  reply	other threads:[~2021-05-24 15:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-23 14:06 Vladislav Shpilevoy via Tarantool-patches
2021-05-24 10:04 ` Oleg Babin via Tarantool-patches
2021-05-24 15:49   ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-05-24 16:00     ` Oleg Babin via Tarantool-patches
2021-05-24 13:01 ` Serge Petrenko via Tarantool-patches
2021-05-24 13:05   ` Serge Petrenko via Tarantool-patches
2021-05-24 15:47     ` Vladislav Shpilevoy via Tarantool-patches
2021-05-24 15:47   ` Vladislav Shpilevoy via Tarantool-patches
2021-05-24 16:17     ` Serge Petrenko via Tarantool-patches
2021-05-25 21:20 ` Vladislav Shpilevoy via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=06d4c252-3f41-d91b-6943-ec4cca520a99@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=olegrok@tarantool.org \
    --cc=sergepetrenko@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 1/1] json: use cord_ibuf for encoding and decoding' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox