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 BD7D36EC55; Thu, 29 Jul 2021 18:23:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BD7D36EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627572200; bh=5PnhGMUrBCGxf628WVFryASnGOYp7SlfwLH+qOmxgt8=; 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=uzI6on6KOb6X8zVw3nGeaO0gLzbKKBBu8ofKBXIZpDAdVm0sCa8coPAE97NTuGwU9 GF52JglNEFr3YyKPP33Bgvnvjiz3p5FQM8o8Xi0JQDLEEVzBUDyaFX2McK5FEfSy3V xUg1PZaY2a5oQ61Bh226Ls+eY/unVAHWrzbx1ywY= 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 C3D076EC55 for ; Thu, 29 Jul 2021 18:23:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C3D076EC55 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1m97sT-0008Jt-V3; Thu, 29 Jul 2021 18:23:18 +0300 Date: Thu, 29 Jul 2021 18:23:16 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20210729152316.boj2wdhfn6wr26j2@esperanza> References: <0123065e-6352-331e-2dd8-b712b9d6e26a@tarantool.org> <20210729113313.fmq2fo4vpkrdusp7@esperanza> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210729113313.fmq2fo4vpkrdusp7@esperanza> X-4EC0790: 10 X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD941C43E597735A9C34755E0A9F196FCB739C645213AB7C8E0182A05F53808504095D0350C51A1A108037553E572A83913D99BC167B0E131BDDC1E0274E9F68336 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE75C0AD7D016C066E3C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE7965AF5021CACFC748F08D7030A58E5AD1A62830130A00468AEEEE3FBA3A834EE7353EFBB5533756682A4B7DEF7839881FF3138BE4E836F275E5D77553FF38084A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE71AE4D56B06699BBC9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E478A468B35FE767117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637A7EFCB0EB5ACB161EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A5627F0FAD331FF6C0DAC58C21874FDB47A4B42D834AD9A7ADD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA753530422897FB34C3410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34AF61ACC197BF517A64357DA19DAD3DB1F3622AF12567DAA828EF65B21614DE9CBEB3BDA68627E5401D7E09C32AA3244CE6B8A3A5A0B86C4EAB280BEC24C79A556C24832127668422FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojPp/mPgZxawGIG3jVS+BdZQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DAD340F681E38947656A8BC1B4D9FCD01274CEFED1673C562683ABF942079399BFB559BB5D741EB966A65DFF43FF7BE03240331F90058701C67EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 00/20] Rewrite performance critical parts of net.box 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 Thu, Jul 29, 2021 at 02:33:13PM +0300, Vladimir Davydov wrote: > On Thu, Jul 29, 2021 at 12:51:18AM +0200, Vladislav Shpilevoy wrote: > > Hi! Thanks for the patchset! > > > > Here is a first part of my review. I will return later to continue. > > > > Commits 02-05, 07-08, 10 are LGTM. > > Pushed to master. > > > > > > Asynchronous calls don't show as much of an improvement as synchronous, > > > because per each asynchronous call we still have to create a 'future' > > > object in Lua. Still, the improvement is quite noticeable - 30% for > > > REPLACE, 10% for UPDATE, 20% for SELECT, 25% for CALL. > > > > I didn't reach the end of the patchset yet, but did you try to create > > the futures as cdata objects? They could be allocated on mempool, their > > GC pressure might be optimized by doing similar to luaT_tuple_encode_table_ref > > optimization (there was found a way to make __gc and other C functions > > cheaper when it comes to amount of GC objects in Lua). > > > > The API would stay the same, they just would become C structs with > > methods instead of Lua tables. > > Good call. Going to to try that. Thanks. Quickly whipped up a patch that converts userdata and cdata. Applied on top of the series. Surprisingly, it only made things worse: With the patch (future is cdata): ==== FUTURE ==== REPLACE: WALL 221.182 PROC 343.368 KRPS UPDATE: WALL 178.918 PROC 291.504 KRPS SELECT: WALL 220.815 PROC 248.843 KRPS CALL: WALL 218.313 PROC 315.670 KRPS Without the patch (future is userdata): ==== FUTURE ==== REPLACE: WALL 262.454 PROC 450.425 KRPS UPDATE: WALL 191.538 PROC 322.888 KRPS SELECT: WALL 288.498 PROC 333.393 KRPS CALL: WALL 247.463 PROC 375.180 KRPS The patch is below. Note, it isn't entirely correct - future:pairs doesn't work, because luaL_checkcdata doesn't seem to handle upvalues, but it shouldn't affect the test. -- >From 8dc651e67f124a394cb6f4973b71080626d84d50 Mon Sep 17 00:00:00 2001 From: Vladimir Davydov Date: Thu, 29 Jul 2021 18:06:32 +0300 Subject: [PATCH] net.box: convert netbox_request to Lua cdata diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c index 957d4ff18a18..1fb3c566d59f 100644 --- a/src/box/lua/net_box.c +++ b/src/box/lua/net_box.c @@ -32,6 +32,7 @@ #include #include +#include #include /* mp_store_u32() */ #include "scramble.h" @@ -135,8 +136,9 @@ struct netbox_request { }; +struct mempool netbox_request_pool; +static uint32_t CTID_STRUCT_NETBOX_REQUEST_PTR; static const char netbox_registry_typename[] = "net.box.registry"; -static const char netbox_request_typename[] = "net.box.request"; /* Passed to mpstream_init() to set a boolean flag on error. */ static void @@ -1394,7 +1396,11 @@ luaT_netbox_registry_reset(struct lua_State *L) static inline struct netbox_request * luaT_check_netbox_request(struct lua_State *L, int idx) { - return luaL_checkudata(L, idx, netbox_request_typename); + uint32_t cdata_type; + struct netbox_request **ptr = luaL_checkcdata(L, idx, &cdata_type); + if (ptr == NULL || cdata_type != CTID_STRUCT_NETBOX_REQUEST_PTR) + luaL_error(L, "expected net.box future as %d argument", idx); + return *ptr; } static int @@ -1403,6 +1409,7 @@ luaT_netbox_request_gc(struct lua_State *L) struct netbox_request *request = luaT_check_netbox_request(L, 1); netbox_request_unregister(request); netbox_request_destroy(request); + mempool_free(&netbox_request_pool, request); return 0; } @@ -1642,10 +1649,14 @@ netbox_perform_async_request_impl(struct lua_State *L, int idx, static int netbox_perform_async_request(struct lua_State *L) { - struct netbox_request *request = lua_newuserdata(L, sizeof(*request)); + struct netbox_request *request = mempool_alloc(&netbox_request_pool); + if (request == NULL) + return luaL_error(L, "out of memory"); netbox_request_create(request); - luaL_getmetatable(L, netbox_request_typename); - lua_setmetatable(L, -2); + *(struct netbox_request **) + luaL_pushcdata(L, CTID_STRUCT_NETBOX_REQUEST_PTR) = request; + lua_pushcfunction(L, luaT_netbox_request_gc); + luaL_setcdatagc(L, -2); netbox_perform_async_request_impl(L, 1, request); return 1; } @@ -2020,6 +2031,13 @@ netbox_console_loop(struct lua_State *L) int luaopen_net_box(struct lua_State *L) { + mempool_create(&netbox_request_pool, cord_slab_cache(), + sizeof(struct netbox_request)); + + luaL_cdef(L, "struct netbox_request;"); + CTID_STRUCT_NETBOX_REQUEST_PTR = luaL_ctypeid( + L, "struct netbox_request *"); + static const struct luaL_Reg netbox_registry_meta[] = { { "__gc", luaT_netbox_registry_gc }, { "reset", luaT_netbox_registry_reset }, @@ -2027,17 +2045,6 @@ luaopen_net_box(struct lua_State *L) }; luaL_register_type(L, netbox_registry_typename, netbox_registry_meta); - static const struct luaL_Reg netbox_request_meta[] = { - { "__gc", luaT_netbox_request_gc }, - { "is_ready", luaT_netbox_request_is_ready }, - { "result", luaT_netbox_request_result }, - { "wait_result", luaT_netbox_request_wait_result }, - { "discard", luaT_netbox_request_discard }, - { "pairs", luaT_netbox_request_pairs }, - { NULL, NULL } - }; - luaL_register_type(L, netbox_request_typename, netbox_request_meta); - static const luaL_Reg net_box_lib[] = { { "decode_greeting",netbox_decode_greeting }, { "new_registry", netbox_new_registry }, @@ -2048,6 +2055,14 @@ luaopen_net_box(struct lua_State *L) { "iproto_loop", netbox_iproto_loop }, { "console_setup", netbox_console_setup }, { "console_loop", netbox_console_loop }, + + /* Request methods. */ + { "request_is_ready", luaT_netbox_request_is_ready }, + { "request_result", luaT_netbox_request_result }, + { "request_wait_result", luaT_netbox_request_wait_result }, + { "request_discard", luaT_netbox_request_discard }, + { "request_pairs", luaT_netbox_request_pairs }, + { NULL, NULL} }; /* luaL_register_module polutes _G */ diff --git a/src/box/lua/net_box.lua b/src/box/lua/net_box.lua index 9da900933fbc..2aa76427a940 100644 --- a/src/box/lua/net_box.lua +++ b/src/box/lua/net_box.lua @@ -53,6 +53,24 @@ local M_COUNT = 16 -- Injects raw data into connection. Used by console and tests. local M_INJECT = 17 +local request_t = ffi.typeof('struct netbox_request') + +local request_methods = { + ['is_ready'] = internal.request_is_ready, + ['result'] = internal.request_result, + ['wait_result'] = internal.request_wait_result, + ['discard'] = internal.request_discard, + ['pairs'] = internal.request_pairs, +} + +ffi.metatype(request_t, { + __index = function(self, key) + return request_methods[key] + end, + -- Lua 5.2 compatibility + __pairs = internal.request_pairs, +}) + -- utility tables local is_final_state = {closed = 1, error = 1}