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 0C58068F41; Thu, 25 Mar 2021 00:31:42 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0C58068F41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616621502; bh=krWFHmJ4ajhsrFXVjAT587bPErUAw1qjhphsltwDqY8=; 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=TKzL92vLy9Zpa5Gu1W+rYrFnkfCofuCSB01Alet7uU5MrWm0tGlVTI+Twnh3dcMxN uvwdBg4U6+Z9tz6X8J9xYi0U9iD5purv8GwJB3I6tjrDGDSNWH7xpOeA+15Lai6F8N weN0O5Bk9hsHjgC5R7V674+9iGz71iVVUgbD77aY= 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 CE9B768F42 for ; Thu, 25 Mar 2021 00:24:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CE9B768F42 Received: by smtp48.i.mail.ru with esmtpa (envelope-from ) id 1lPAzn-0004ib-KI; Thu, 25 Mar 2021 00:24:56 +0300 To: tarantool-patches@dev.tarantool.org, kyukhin@tarantool.org Date: Wed, 24 Mar 2021 22:24:34 +0100 Message-Id: <45068565dd18f7e65f534930d2ce6208b0ef2f43.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: 4F1203BC0FB41BD9064ADF4728AA0EE919EF87AC0718675712B828C563C11F50182A05F53808504063B795FAD0CB81ACD3C19D1B8201F8B8E2BEB32CCD8F85C0DC437F5C0942976B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7EB05B7739F1E6D56EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006371D26D2A8652661258638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C5DD32608FC869F5D8141AB1A84C4A8FDFDA88A8350DC4B31A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7328B01A8D746D8839FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C37EF884183F8E4D67117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF5E208EBACFE9213EBA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B6BC2916011759DE1776E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A2BEBFE083D3B9BA73A03B725D353964BB11811A4A51E3B096D1867E19FE14079BA9C0B312567BB23089D37D7C0E48F6CA18204E546F3947CB26E97DCB74E625257739F23D657EF2BC8A9BA7A39EFB7666BA297DBC24807EA089D37D7C0E48F6C8AA50765F790063757B1FBEA53BC6EDBEFF80C71ABB335746BA297DBC24807EA27F269C8F02392CD6143B1329D1CCA5627F269C8F02392CD5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46D0A00A614A4519F84355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C5DD32608FC869F5D8141AB1A84C4A8FDFDA88A8350DC4B319C2B6934AE262D3EE7EAB7254005DCED114C52B35DBB74F4E7EAB7254005DCEDEC0BC6DB5D5D7B8B1E0A4E2319210D9B64D260DF9561598F01A9E91200F654B0D18C7F408F36097E8E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D345DE7771146E56B0877D698B9E5B74E1958F8F5D56E1E7569F45D58A8E2BC3D99F7E5650C66EDE6CA1D7E09C32AA3244CF301D495F01BFC3732209DC1ACAE107F81560E2432555DBB729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojjqzNotmU+gcxo4Tz8Pq24A== X-Mailru-Sender: 504CC1E875BF3E7D9BC0E5172ADA3110A906815429F9D9604A4DCEDC2F31D819B8DC3784BF3AAB2607784C02288277CA03E0582D3806FB6A5317862B1921BA260ED6CFD6382C13A6112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH 08/15] uuid: replace static_alloc with 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" static_alloc() appears not to be safe to use in Lua, because it does not provide any ownership protection for the returned values. The problem appears when something is allocated, then Lua GC starts, and some __gc handlers might also use static_alloc(). In Lua and in C - both lead to the buffer being corrupted in its original usage place. The patch is a part of activity of getting rid of static_alloc() in Lua. It removes it from uuid Lua module and makes it use the new FFI stash feature, which helps to cache frequently used and heavy to allocate FFI values. ffi.new() is not used, because - It produces a new GC object; - ffi.new('struct tt_uuid') costs around 300ns while FFI stash costs around 0.8ns (although it is magically fixed when ffi.new('struct tt_uuid[1]') is used); - Without JIT ffi.new() costs about the same as the stash, ~280ns for small objects like tt_uuid. The patch makes uuid perf a bit better in the places where static_alloc() was used, because its cost was around 7ns for one allocation. (cherry picked from commit 5cbb69077ff9fe22224f94aeaa05d8da9ed9cb6e) --- src/lua/uuid.lua | 24 +++++++++++++++++------- test/app/uuid.result | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/lua/uuid.lua b/src/lua/uuid.lua index 956ad6e36..46b35075a 100644 --- a/src/lua/uuid.lua +++ b/src/lua/uuid.lua @@ -1,6 +1,7 @@ -- uuid.lua (internal file) local ffi = require("ffi") +local buffer = require('buffer') local builtin = ffi.C ffi.cdef[[ @@ -33,7 +34,9 @@ extern const struct tt_uuid uuid_nil; local uuid_t = ffi.typeof('struct tt_uuid') local UUID_STR_LEN = 36 local UUID_LEN = ffi.sizeof(uuid_t) -local uuidbuf = ffi.new(uuid_t) +local uuid_stash = buffer.ffi_stash_new(uuid_t) +local uuid_stash_take = uuid_stash.take +local uuid_stash_put = uuid_stash.put local uuid_tostring = function(uu) if not ffi.istype(uuid_t, uu) then @@ -69,11 +72,12 @@ local uuid_tobin = function(uu, byteorder) return error('Usage: uuid:bin([byteorder])') end if need_bswap(byteorder) then - if uu ~= uuidbuf then - ffi.copy(uuidbuf, uu, UUID_LEN) - end + local uuidbuf = uuid_stash_take() + ffi.copy(uuidbuf, uu, UUID_LEN) builtin.tt_uuid_bswap(uuidbuf) - return ffi.string(ffi.cast('char *', uuidbuf), UUID_LEN) + uu = ffi.string(ffi.cast('char *', uuidbuf), UUID_LEN) + uuid_stash_put(uuidbuf) + return uu end return ffi.string(ffi.cast('char *', uu), UUID_LEN) end @@ -114,12 +118,18 @@ local uuid_new = function() end local uuid_new_bin = function(byteorder) + local uuidbuf = uuid_stash_take() builtin.tt_uuid_create(uuidbuf) - return uuid_tobin(uuidbuf, byteorder) + local res = uuid_tobin(uuidbuf, byteorder) + uuid_stash_put(uuidbuf) + return res end local uuid_new_str = function() + local uuidbuf = uuid_stash_take() builtin.tt_uuid_create(uuidbuf) - return uuid_tostring(uuidbuf) + local res = uuid_tostring(uuidbuf) + uuid_stash_put(uuidbuf) + return res end local uuid_mt = { diff --git a/test/app/uuid.result b/test/app/uuid.result index 0713614c6..1da8b3e58 100644 --- a/test/app/uuid.result +++ b/test/app/uuid.result @@ -106,7 +106,7 @@ uu.node[5] -- invalid values uuid.fromstr(nil) --- -- error: 'builtin/uuid.lua:47: fromstr(str)' +- error: 'builtin/uuid.lua:50: fromstr(str)' ... uuid.fromstr('') --- -- 2.24.3 (Apple Git-128)