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 E575270C99; Thu, 2 Sep 2021 18:48:17 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E575270C99 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1630597698; bh=bSC61JMLh8bMVXDREMjguD7Fu7OX4rAaru9enNtm+cU=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=NzMaq5Z3nmEWM6LRfA2kPvyqdUs6Yzp47nOKKuIhToSSB40MaCjfn3lrfbH2+yZ5z 3VUN3z5/EOBQHZz+dqWjww1vePblbUBOkJj8GfJTvtu8DjGBWR0g0KTLJ1vEHcF43E fHgIZpkefuk08ufoPbyCsquPMkbp4WpoKHpeTu9Y= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (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 EF91F70C99 for ; Thu, 2 Sep 2021 18:48:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EF91F70C99 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mLowi-000207-Rd; Thu, 02 Sep 2021 18:48:10 +0300 Date: Thu, 2 Sep 2021 18:22:55 +0300 To: olegrok@tarantool.org Cc: v.shpilevoy@tarantool.org, tarantool-patches@dev.tarantool.org Message-ID: <20210902152255.GT5743@tarantool.org> References: <3453bc40af586cf4242863f1307f72fe5c5a408a.1628713520.git.babinoleg@mail.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <3453bc40af586cf4242863f1307f72fe5c5a408a.1628713520.git.babinoleg@mail.ru> X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.10.1 (2018-07-13) X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9D96C1EA41D18F4D57B705481ADDDE748772430255B74E284182A05F53808504074A8E873339C924DFF646A40A2B76AEB7BA1DC41761B349BE8D055C315E1396E X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7922D113DFDC6D5A3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375045C080FAAE96148638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80DED1B8815046D87A546589169FF0320117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC3A703B70628EAD7BA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18F04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6BF3059D42242344A089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A52C357F78A94008C09F43C33005052BAF9BA34FA4E61DF2D2D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA752546FE575EB473F1410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F1ADD4D8CD3C81CEFD36996949F1B84CF1601DF92C31C0A4B009E516BF159E5051659077DDAC436B1D7E09C32AA3244C235DFE8FD8FFA5BEDF4A8ED69160A9AED9ADFF0C0BDB8D1F927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojK/h3ZnmlPjElVDEJ4GBk1g== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D20E2C0DF6DE752FC6A881FBE515615F6A7C8D0F45F857DBFE9F1EFEE2F478337FB559BB5D741EB964C8C2C849690F8E70A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 3/4] 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: Igor Munkin via Tarantool-patches Reply-To: Igor Munkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Oleg, Thanks for the patch! Additionally thank you for purging this memoize hell: now everything is much clearer and the platform performance becomes better. The changes are OK in general, but please consider my comments below. On 11.08.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.h | 5 ++ > src/lua/fiber.c | 110 +++++++++++++++---------------------------- > 2 files changed, 42 insertions(+), 73 deletions(-) > > diff --git a/src/lib/core/fiber.h b/src/lib/core/fiber.h > index 593847df7..3e9663a1a 100644 > --- a/src/lib/core/fiber.h > +++ b/src/lib/core/fiber.h > @@ -623,6 +623,11 @@ struct fiber { > * Should not be used in other fibers. > */ > struct lua_State *stack; > + /** > + * Optional reference to userdata > + * represented current fiber in Lua. > + */ > + int ref; I see you didn't draw conclusions from your first patch :) I propose to name this field , so one clearly understand what is stored in that userdata, and no additional patches with rename are necessary in future. > /** > * Optional fiber.storage Lua reference. > */ > diff --git a/src/lua/fiber.c b/src/lua/fiber.c > index 5575f2079..268ddf9cc 100644 > --- a/src/lua/fiber.c > +++ b/src/lua/fiber.c > @@ -87,27 +87,31 @@ luaL_testcancel(struct lua_State *L) > static const char *fiberlib_name = "fiber"; > > +static int > +lbox_fiber_on_stop(struct trigger *trigger, void *event) > { > + struct fiber *f = (struct fiber *) event; > + int storage_ref = f->storage.lua.storage_ref; > + if (storage_ref > 0) { I don't like this. At first, handle this case underneat, so if there is invalid reference or LUA_REFNIL / LUA_NOREF, everything works fine. But the worst part is not the redundancy, but comparing with some magic numbers. Lua provides no guarantees LUA_REFNIL / LUA_NOREF values are not changed between the particular versions. Do not rely on the internal constants outside of LuaJIT sources. Please drop both assertions and this particular condition. > + luaL_unref(tarantool_L, LUA_REGISTRYINDEX, storage_ref); > + f->storage.lua.storage_ref = LUA_NOREF; > + } > + int ref = f->storage.lua.ref; > + assert(ref > 0); > + luaL_unref(tarantool_L, LUA_REGISTRYINDEX, ref); > + f->storage.lua.ref = LUA_NOREF; > + trigger_clear(trigger); > + free(trigger); > + return 0; > } > > /** > @@ -116,42 +120,26 @@ lbox_create_weak_table(struct lua_State *L, const char *name) > static void > lbox_pushfiber(struct lua_State *L, struct fiber *f) > { > + int ref = f->storage.lua.ref; > + if (ref <= 0) { Again, do not rely on the internal values. Just compare ref with LUA_NOREF (comparison with LUA_REFNIL is not required, since this is particular value when nil slot is used in ). > + struct trigger *t = (struct trigger *)malloc(sizeof(*t)); > + if (t == NULL) { > + diag_set(OutOfMemory, sizeof(*t), "malloc", "t"); > + luaT_error(L); > + } > + trigger_create(t, lbox_fiber_on_stop, NULL, (trigger_f0) free); > + trigger_add(&f->on_stop, t); > + > + uint64_t fid = f->fid; > /* create a new userdata */ > uint64_t *ptr = lua_newuserdata(L, sizeof(*ptr)); > *ptr = fid; > luaL_getmetatable(L, fiberlib_name); > lua_setmetatable(L, -2); > - /* memoize it */ > - lua_settable(L, -3); > - luaL_pushuint64(L, fid); > - /* get it back */ > - lua_gettable(L, -2); > + ref = luaL_ref(L, LUA_REGISTRYINDEX); > + f->storage.lua.ref = ref; > } > + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); > } > > static struct fiber * > -- > 2.32.0 > -- Best regards, IM