From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id D2FEC6F3F2; Fri, 3 Sep 2021 23:49:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D2FEC6F3F2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1630702143; bh=em4eHDPgQX/jrnabo2VStjHtoyuc3vs3+yNZ3jMSXYg=; h=To:References:Date:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=irjjb2cFelGX7faRbvSyGsvYuk+QW7nTLnlPNp8mBLw6y1X8lYDDetncai4KkvfKM eM3joJHXcLt2oCIA8znNQZG6V+0iA+c/1PVIw28ce8BhgjLPelRHM9Ttmd2qSUBvBq vFah3UtdfCX0lKMWe06nYQuSrgkWls08R61MFTLE= Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [94.100.177.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id DBD596F3F2 for ; Fri, 3 Sep 2021 23:49:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DBD596F3F2 Received: by smtp44.i.mail.ru with esmtpa (envelope-from ) id 1mMG7P-0000eE-Iv; Fri, 03 Sep 2021 23:49:00 +0300 To: Igor Munkin References: <7ef7fcfec0d2c78721ea17376cbfe0d0f231798c.1630606077.git.babinoleg@mail.ru> <20210903122314.GV5743@tarantool.org> Message-ID: <6e5f684a-bf7b-3767-1e31-25ed59416c61@tarantool.org> Date: Fri, 3 Sep 2021 23:48:58 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20210903122314.GV5743@tarantool.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9D96C1EA41D18F4D502C758EA1710EDB6F5A7A0628E11ABA6182A05F5380850409F153A3B6F1457EE3905C9B2E3B35AE346F40C19FBFFB0A2431A006E52B7A92D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7B9FBA884A7C9B8BAEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AF78C68CB7D402738638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D85FA2F840EB250703DA77A01FDE35A06D117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC974A882099E279BDA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD182CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EECCD848CCB6FE560CFCE65BE3358055BDD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3FA486DC37A503D0B040F9FF01DFDA4A8C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006379FC65886B7F75608EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458F0AFF96BAACF4158235E5A14AD4A4A4625E192CAD1D9E79DB8BF922E402C092CCD815D6E1AEA3AA5 X-C1DE0DAB: 0D63561A33F958A5419DE4FDCBCEAC2B38BDECDDFB501BAF9365D03389F06201D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA752546FE575EB473F1410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D2AC226BFD4557240D8C3AF69B76963417CBBDF4379076CE862EAC31E877339BA9DC7EEC82FDA2BF1D7E09C32AA3244C6463DDB8F7E8DEF08F7DE3EE568165C360759606DA2E136A927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0HWrnICxeoqadgNNrrQSdg== X-Mailru-Sender: 583F1D7ACE8F49BD1042885CEC987B6BB899FACE6D552A1B3905C9B2E3B35AE3CA71892AC981DD5F7019711D9D5B048E1458020726E2BC9FD5ECBA0B92C0A936CDC7563AA7CEBD287402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v2 3/3] fiber: keep reference to userdata if fiber created once X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Oleg Babin via Tarantool-patches Reply-To: Oleg Babin Cc: tarantool-patches@dev.tarantool.org, v.shpilevoy@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thanks for your review. See my answers below. On 03.09.2021 15:23, Igor Munkin wrote: > Oleg, > > Thanks for the fixes! I still have some comments regarding the new > version of this patch. > > On 02.09.21, olegrok@tarantool.org wrote: >> From: Oleg Babin >> >> This patch reworks approach to fiber management in Lua. Before >> this patch each action that should return fiber led to new >> userdata creation that was quite slow and made GC suffer. This >> patch introduces new field in struct fiber to store a reference to >> userdata that was created once for a fiber. It allows speedup >> operations as fiber.self() and fiber.id(). >> Simple benchmark shows that access to fiber storage is faster in >> two times, fiber.find() - 2-3 times and fiber.new/create functions >> don't have any changes. >> --- >> src/lib/core/fiber.c | 5 ++ >> src/lib/core/fiber.h | 5 ++ >> src/lua/fiber.c | 113 +++++++++++++------------------------------ >> 3 files changed, 44 insertions(+), 79 deletions(-) >> >> diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c >> index 588b78504..39b67f940 100644 >> --- a/src/lib/core/fiber.c >> +++ b/src/lib/core/fiber.c >> @@ -32,6 +32,7 @@ >> >> #include >> #include >> +#include > Totally agree with Vlad here. I guess you have two options: > * Use the approach similar to . > * Introduce a constant in src/lib/core/fiber.h and add a static assert > in src/lua/utils.h whether this value is the same as in LuaJIT. > > Maybe there are other options Vlad can suggest. I've found that there is lua_nil_ref value in our code. For my purpose I've added lua_no_ref. Incremental diff will be below. Also I will send new series since this change was done in separate patch. > >> #include >> #include >> #include > > >> diff --git a/src/lua/fiber.c b/src/lua/fiber.c >> index 5575f2079..ab0e895eb 100644 >> --- a/src/lua/fiber.c > +++ b/src/lua/fiber.c >> @@ -87,27 +87,28 @@ luaL_testcancel(struct lua_State *L) > > >> + struct fiber *f = event; >> + int storage_ref = f->storage.lua.storage_ref; > Minor: Why do you need both and variables? After > your change they can be freely dropped. Removed > >> + luaL_unref(tarantool_L, LUA_REGISTRYINDEX, storage_ref); >> + f->storage.lua.storage_ref = LUA_NOREF; >> + int ref = f->storage.lua.fid_ref; >> + luaL_unref(tarantool_L, LUA_REGISTRYINDEX, ref); >> + f->storage.lua.fid_ref = LUA_NOREF; >> + trigger_clear(trigger); >> + free(trigger); >> + return 0; >> } >> >> /** >> @@ -116,42 +117,26 @@ lbox_create_weak_table(struct lua_State *L, const char *name) > > >> + int ref = f->storage.lua.fid_ref; > Minor: It would be nice also to rename to . Fixed. >> + if (ref == LUA_NOREF) { > > >> -- >> 2.32.0 >> diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c index 39b67f940..1e5b2fb80 100644 --- a/src/lib/core/fiber.c +++ b/src/lib/core/fiber.c @@ -32,7 +32,6 @@  #include  #include -#include  #include  #include  #include @@ -45,6 +44,7 @@  #include "errinj.h"  extern void cord_on_yield(void); +extern int luaL_no_ref;  #if ENABLE_FIBER_TOP  #include /* __rdtscp() */ @@ -896,8 +896,8 @@ fiber_recycle(struct fiber *fiber)      fiber->f = NULL;      fiber->wait_pad = NULL;      memset(&fiber->storage, 0, sizeof(fiber->storage)); -    fiber->storage.lua.storage_ref = LUA_NOREF; -    fiber->storage.lua.fid_ref = LUA_NOREF; +    fiber->storage.lua.storage_ref = luaL_no_ref; +    fiber->storage.lua.fid_ref = luaL_no_ref;      unregister_fid(fiber);      fiber->fid = 0;      region_free(&fiber->gc); @@ -1239,8 +1239,8 @@ fiber_new_ex(const char *name, const struct fiber_attr *fiber_attr,              return NULL;          }          memset(fiber, 0, sizeof(struct fiber)); -        fiber->storage.lua.storage_ref = LUA_NOREF; -        fiber->storage.lua.fid_ref = LUA_NOREF; +        fiber->storage.lua.storage_ref = luaL_no_ref; +        fiber->storage.lua.fid_ref = luaL_no_ref;          if (fiber_stack_create(fiber, &cord()->slabc,                         fiber_attr->stack_size)) { diff --git a/src/lua/fiber.c b/src/lua/fiber.c index f82f4032f..70addd8f6 100644 --- a/src/lua/fiber.c +++ b/src/lua/fiber.c @@ -100,11 +100,9 @@ static int  lbox_fiber_on_stop(struct trigger *trigger, void *event)  {      struct fiber *f = event; -    int storage_ref = f->storage.lua.storage_ref; -    luaL_unref(tarantool_L, LUA_REGISTRYINDEX, storage_ref); +    luaL_unref(tarantool_L, LUA_REGISTRYINDEX, f->storage.lua.storage_ref);      f->storage.lua.storage_ref = LUA_NOREF; -    int ref = f->storage.lua.fid_ref; -    luaL_unref(tarantool_L, LUA_REGISTRYINDEX, ref); +    luaL_unref(tarantool_L, LUA_REGISTRYINDEX, f->storage.lua.fid_ref);      f->storage.lua.fid_ref = LUA_NOREF;      trigger_clear(trigger);      free(trigger); @@ -117,8 +115,8 @@ lbox_fiber_on_stop(struct trigger *trigger, void *event)  static void  lbox_pushfiber(struct lua_State *L, struct fiber *f)  { -    int ref = f->storage.lua.fid_ref; -    if (ref == LUA_NOREF) { +    int fid_ref = f->storage.lua.fid_ref; +    if (fid_ref == LUA_NOREF) {          struct trigger *t = malloc(sizeof(*t));          if (t == NULL) {              diag_set(OutOfMemory, sizeof(*t), "malloc", "t"); @@ -133,10 +131,10 @@ lbox_pushfiber(struct lua_State *L, struct fiber *f)          *ptr = fid;          luaL_getmetatable(L, fiberlib_name);          lua_setmetatable(L, -2); -        ref = luaL_ref(L, LUA_REGISTRYINDEX); -        f->storage.lua.fid_ref = ref; +        fid_ref = luaL_ref(L, LUA_REGISTRYINDEX); +        f->storage.lua.fid_ref = fid_ref;      } -    lua_rawgeti(L, LUA_REGISTRYINDEX, ref); +    lua_rawgeti(L, LUA_REGISTRYINDEX, fid_ref);  }  static struct fiber * diff --git a/src/lua/utils.c b/src/lua/utils.c index c71cd4857..c252d7cdf 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -40,6 +40,7 @@  #include "uuid/tt_uuid.h"  int luaL_nil_ref = LUA_REFNIL; +int luaL_no_ref = LUA_NOREF;  static int luaT_newthread_ref = LUA_NOREF; diff --git a/test/unit/core_test_utils.c b/test/unit/core_test_utils.c index 23452bbfd..9b1789e75 100644 --- a/test/unit/core_test_utils.c +++ b/test/unit/core_test_utils.c @@ -35,3 +35,5 @@  void cord_on_yield(void)  {  } + +int luaL_no_ref = -2;