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 58B4368F40; Thu, 25 Mar 2021 00:31:11 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 58B4368F40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616621471; bh=6/EjCuBHwBVAwkcEdROT8GYv9WH7zY3+ZuszKlOmATA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=oN4RWAEl19hIWn5E0hdKwcgGV8D/04+BqVfjwL621oMlKDJ1V6UeP5wd81zKcag7j c6ui0H/1MWY7T6q5OXgjHuk+FM5iO8cvlCyay5MLcvMmpWw5ZRRdBy8tSfSmqaFuSC 0n+/rzmeiP+8TmgOq9x4ymwRiHQr94d6HBt3XUYc= Received: from smtp48.i.mail.ru (smtp48.i.mail.ru [94.100.177.108]) (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 C870368F41 for ; Thu, 25 Mar 2021 00:24:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C870368F41 Received: by smtp48.i.mail.ru with esmtpa (envelope-from ) id 1lPAzl-0004ib-Nb; Thu, 25 Mar 2021 00:24:54 +0300 To: tarantool-patches@dev.tarantool.org, kyukhin@tarantool.org Date: Wed, 24 Mar 2021 22:24:33 +0100 Message-Id: <34b6d85e7c0b1d18e182a60abec602c0da6f5ee3.1616620860.git.v.shpilevoy@tarantool.org> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9064ADF4728AA0EE9587C800CEAD38A69F042767B24605C2B182A05F538085040C0E90DCFFA20360778595825B173B9B758A8C0CF4C9C569174300B6BF0681ECE X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A3589DC202DD7369EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637531CC3E3F637A59A8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C5DD32608FC869F5DF5118115B88ADDD065AA5C168F785C94A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7328B01A8D746D8839FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E478A468B35FE767117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF5E208EBACFE9213EBA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B636C6EC11B1A5ECB976E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A2BEBFE083D3B9BA73A03B725D353964BB11811A4A51E3B096D1867E19FE14079BA9C0B312567BB23089D37D7C0E48F6CA18204E546F3947CB26E97DCB74E625257739F23D657EF2BC8A9BA7A39EFB7666BA297DBC24807EA089D37D7C0E48F6C8AA50765F790063757B1FBEA53BC6EDBEFF80C71ABB335746BA297DBC24807EA27F269C8F02392CD6143B1329D1CCA5627F269C8F02392CD5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46D8838A6F459B704FE355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C5DD32608FC869F5DF5118115B88ADDD065AA5C168F785C949C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F6110710A6CC527F0460570DCE3A04382F08AC9D1E3FCD94991FF8226361D0DC032D5ECCC54B32311D7E09C32AA3244CEF2C527A9801215336D59EA5A250726E5A1673A01BA68E40729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojjqzNotmU+geKfvLOjUiGYA== X-Mailru-Sender: 504CC1E875BF3E7D9BC0E5172ADA3110A906815429F9D960A5CB6FE99AE09CDDFE82372B44F232A107784C02288277CA03E0582D3806FB6A5317862B1921BA260ED6CFD6382C13A6112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH 07/15] buffer: implement ffi stash 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: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Buffer module now exposes ffi_stash_new() function which returns 2 functions take() and put(). FFI stash implements proper ownership of global heavy-to-create objects which can only be created via FFI. Such as structs, pointers, arrays. It should help to fix buffer's registers (buffer.reg1, buffer.reg2, buffer.reg_array), and other global FFI objects such as 'struct port_c' in schema.lua. The issue is that when these objects are global, they might be re-used right during usage in case Lua starts GC and invokes __gc handlers. Just like it happened with IBUF_SHARED and static_alloc(). Part of #5632 (cherry picked from commit a5549a44db835a59ad6ab82d206172f9d8047a2d) --- src/lua/buffer.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/lua/buffer.lua b/src/lua/buffer.lua index 398319cf2..555970ea8 100644 --- a/src/lua/buffer.lua +++ b/src/lua/buffer.lua @@ -182,6 +182,55 @@ local function ibuf_new(arg, arg2) errorf('Usage: ibuf([size])') end +-- +-- Stash keeps an FFI object for re-usage and helps to ensure the proper +-- ownership. Is supposed to be used in yield-free code when almost always it is +-- possible to put the taken object back. +-- Then cost of the stash is almost the same as ffi.new() for small objects like +-- 'int[1]' even when jitted. Examples: +-- +-- * ffi.new('int[1]') is about ~0.4ns, while the stash take() + put() is about +-- ~0.8ns; +-- +-- * Much better on objects > 128 bytes in size. ffi.new('struct uri[1]') is +-- ~300ns, while the stash is still ~0.8ns; +-- +-- * For structs not allocated as an array is also much better than ffi.new(). +-- For instance, ffi.new('struct tt_uuid') is ~300ns, the stash is ~0.8ns. +-- Even though 'struct tt_uuid' is 16 bytes; +-- +local function ffi_stash_new(c_type) + local item = nil + + local function take() + local res + -- This line is guaranteed to be GC-safe. GC is not invoked. Because + -- there are no allocation. So it can be considered 'atomic'. + res, item = item, nil + -- The next lines don't need to be atomic and can survive GC. The only + -- important part was to take the global item and set it to nil. + if res then + return res + end + return ffi.new(c_type) + end + + local function put(i) + -- It is ok to rewrite the existing global item if it was set. Does + -- not matter. They are all the same. + item = i + end + + -- Due to some random reason if the stash returns a table with methods it + -- works faster than returning them as multiple values. Regardless of how + -- the methods are used later. Even if the caller will cache take and put + -- methods anyway. + return { + take = take, + put = put, + } +end + -- -- Cord buffer is useful for the places, where -- @@ -225,4 +274,5 @@ return { internal = internal, ibuf = ibuf_new; READAHEAD = READAHEAD; + ffi_stash_new = ffi_stash_new, } -- 2.24.3 (Apple Git-128)