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 323EB6EC58; Wed, 4 Aug 2021 18:35:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 323EB6EC58 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628091330; bh=oWfEbnZfiYFACMYF+sV6tWZTRDbo0v9zP9a4yVJlXbQ=; 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=RqIjubsk7TVV2C4eZRQl1lZmZbxScsw2+2ZgOqsfOcFwQB+y0+5QzuxSYI+NTIHW1 DbNVQZXoyyhDec66RrOnI1K39tE0Du7j5e9THBULn5V9XCpGZ1+VCnUCrQ7Dv+xu6t +CadsUpVtKoEI4TXFp83qW90sC4NPcH/XqODmW3o= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 D9A646EC58 for ; Wed, 4 Aug 2021 18:35:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D9A646EC58 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mBIvY-0004gz-5w; Wed, 04 Aug 2021 18:35:28 +0300 Date: Wed, 4 Aug 2021 18:35:26 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20210804153526.gr2uyri5dg52zw4b@esperanza> References: <903f6291-bab5-985e-f922-042453e4d146@tarantool.org> <20210804123001.3cwudybvkw6dds74@esperanza> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210804123001.3cwudybvkw6dds74@esperanza> X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD941C43E597735A9C3104FC76DFAAAAF7DA068FE323FAC4379182A05F538085040CF5152A2B701A733BBE4F75C76C4CBF7C1F2C74CD62358D7CAC83EDCFEADE191 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76D34FAA3D8B31588C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE73F300A97FDD4E158EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F27987EB8BCDF6CFA22027F93B0A275B67CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8D2DCF9CF1F528DBCF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BB07C9E286C61B7F975ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A51F3A7F67A63E133C28848F650B8A6EF96D755091234EFA05D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7501A9DF589746230F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D348B532EA2091F4FF64836CDB2AF1C1F4E1A86E968FBC656C4C228F820227C554B3990A2CE5BD6390A1D7E09C32AA3244CDB3888930D64D59318D94DF7D43F6C26E8FBBEFAE1C4874CFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojeDyvyeZJDJH0WbKz5dEslQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D99D0087769D8590E64B66DBC219A490E274CEFED1673C562683ABF942079399BFB559BB5D741EB966A65DFF43FF7BE03240331F90058701C67EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 15/20] net.box: rewrite request implementation in C 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: Vladimir Davydov via Tarantool-patches Reply-To: Vladimir Davydov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" On Wed, Aug 04, 2021 at 03:30:01PM +0300, Vladimir Davydov wrote: > On Mon, Aug 02, 2021 at 11:54:43PM +0200, Vladislav Shpilevoy wrote: > > > + lua_pushcclosure(L, luaT_netbox_request_iterator_next, 2); > > > > 8. Push of cfunctions, especially closures, on a regular basis might > > be expensive. Could you please try to make it a non-closure function > > and cache its reference like I showed in the proposal about request __gc? > > My test doesn't check performance of the request iterator so I need to > come up with a new test first to check this hypothesis. I'll reply to > this email with the results once they're ready. Using getref instead of pushcclosure does improve performance of the final version by about 10%: KRPS (WALL TIME) KRPS (PROC TIME) pushcclosure : 233 +- 3 : 344 +- 7 getref : 259 +- 4 : 404 +- 10 The test is here: https://gist.github.com/locker/9df4eaaf1bc889b16706640711c946f7 Amended the patch, thanks. The incremental diff is below: -- diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c index 7ef40792320e..fadd71a6813b 100644 --- a/src/box/lua/net_box.c +++ b/src/box/lua/net_box.c @@ -131,11 +131,20 @@ struct netbox_request { * the response hasn't been received yet. */ struct error *error; + /** Timeout for luaT_netbox_request_iterator_next(). */ + double iterator_timeout; }; static const char netbox_registry_typename[] = "net.box.registry"; static const char netbox_request_typename[] = "net.box.request"; +/** + * Instead of pushing luaT_netbox_request_iterator_next with lua_pushcclosure + * in luaT_netbox_request_pairs, we get it by reference, because it works + * faster. + */ +static int luaT_netbox_request_iterator_next_ref; + static void netbox_request_destroy(struct netbox_request *request) { @@ -1505,9 +1513,8 @@ luaT_netbox_request_discard(struct lua_State *L) static int luaT_netbox_request_iterator_next(struct lua_State *L) { - struct netbox_request *request = luaT_check_netbox_request( - L, lua_upvalueindex(1)); - double timeout = lua_tonumber(L, lua_upvalueindex(2)); + struct netbox_request *request = lua_touserdata(L, 1); + double timeout = request->iterator_timeout; if (luaL_isnull(L, 2)) { /* The previous call returned an error. */ goto stop; @@ -1575,16 +1582,17 @@ error: static int luaT_netbox_request_pairs(struct lua_State *L) { + struct netbox_request *request = luaT_check_netbox_request(L, 1); + double timeout = TIMEOUT_INFINITY; if (!lua_isnoneornil(L, 2)) { - if (lua_type(L, 2) != LUA_TNUMBER || lua_tonumber(L, 2) < 0) + if (lua_type(L, 2) != LUA_TNUMBER || + (timeout = lua_tonumber(L, 2)) < 0) luaL_error(L, "Usage: future:pairs(timeout)"); - } else { - if (lua_isnil(L, 2)) - lua_pop(L, 1); - lua_pushnumber(L, TIMEOUT_INFINITY); } - lua_pushcclosure(L, luaT_netbox_request_iterator_next, 2); - lua_pushnil(L); + request->iterator_timeout = timeout; + lua_rawgeti(L, LUA_REGISTRYINDEX, + luaT_netbox_request_iterator_next_ref); + lua_insert(L, 1); lua_pushinteger(L, 0); return 3; } @@ -1750,6 +1758,9 @@ netbox_dispatch_response_console(struct lua_State *L) int luaopen_net_box(struct lua_State *L) { + lua_pushcfunction(L, luaT_netbox_request_iterator_next); + luaT_netbox_request_iterator_next_ref = luaL_ref(L, LUA_REGISTRYINDEX); + static const struct luaL_Reg netbox_registry_meta[] = { { "__gc", luaT_netbox_registry_gc }, { "reset", luaT_netbox_registry_reset },