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 87E686EC40; Thu, 12 Aug 2021 21:02:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 87E686EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628791373; bh=wKBvVvzghzZD9V7IOa9gUCvc05NTbIWI4H2o1R3FDDA=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=hxZyVSvLQlKNGh4+fNDJmI1wTIfAqa3kstRNjwhesG0/BsRZZ6/nKZM7EWu9GxzyR 8owNl1L7XtPDP+EKsz157Et3e1oyjhFQekKBmiuizPT47fpi4RDkSQGbE7ov3FiVYd 2IjhRsdZM1tqIiTNi20EnEvPwaZnekFfiWc4pHmo= Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [217.69.128.41]) (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 07DD06EC40 for ; Thu, 12 Aug 2021 21:02:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 07DD06EC40 Received: by smtp61.i.mail.ru with esmtpa (envelope-from ) id 1mEF2Y-0000Tp-LE; Thu, 12 Aug 2021 21:02:51 +0300 To: Vladimir Davydov Cc: tarantool-patches@dev.tarantool.org References: Message-ID: Date: Thu, 12 Aug 2021 21:02:46 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9D5AC6413C25DCF08CC98B8FCC5CD86F3182A05F538085040476DB6FC0DE574F5329C67AC703A747B3F4C5E98FD47E4817DF099CA686A0CEB X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7081BBE264C6D7F42EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637148A3F1B2020C2A38638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D85D4B8FF4621FB88DF8121E0C385BE84A117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751FCB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE7B96B19DC4093321D7C2347B1D03E769D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3632EDEA9CD5989A32D242C3BD2E3F4C6C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637B8F435DEDE9E76EBEFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A587885487CD69EE679256AE4E7EC716DB067972A95A553F10D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA752DA3D96DA0CEF5C48E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D349379E7F8541B6C9A60610B77E7236AF53B75FC7F3804867265601146F9D347CD7FBC21AC91E4387A1D7E09C32AA3244CAD4E84802774DD0FA8A8E83CAA3368C3F522A1CF68F4BE05729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojKW4rnL99YhLckWuYukY66Q== X-Mailru-Sender: DCB18673505F245B5D9D3B35E842BD3CA329B89340ABC879EE671FB88E09180D3EEE5674EB50DCBC841FB911095AA09146E8006E22572C39C920B61C43410E8717BDA556287159EE9437F6177E88F7363CDA0F3B3F5B9367 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH] net.box: allow to store user-defined fields in future object 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" Hi! Thanks for the patch! See 3 comments below. > diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c > index 06e574cdf746..162ff6c82fb0 100644 > --- a/src/box/lua/net_box.c > +++ b/src/box/lua/net_box.c <...> > + > +static int > +luaT_netbox_request_newindex(struct lua_State *L) > +{ > + struct netbox_request *request = luaT_check_netbox_request(L, 1); > + struct mh_strnptr_t *h = request->index; > + size_t field_name_len; > + const char *field_name = lua_tolstring(L, 2, &field_name_len); > + if (field_name == NULL) > + return luaL_error(L, "invalid index"); > + int field_value_ref = luaL_ref(L, LUA_REGISTRYINDEX); > + if (field_value_ref == LUA_REFNIL) { > + /* The field is set to nil. Delete it from the index. */ 1. But it is not deleted. It is just ignored, which is fine, but the comment is wrong then. > + if (h == NULL) > + return 0; > + mh_int_t k = mh_strnptr_find_inp(h, field_name, > + field_name_len); > + int ref = (intptr_t)mh_strnptr_node(h, k)->val; > + luaL_unref(L, LUA_REGISTRYINDEX, ref); > + mh_strnptr_del(h, k, NULL); > + return 0; > + } > + if (h == NULL) { > + /* Lazily create the index on the first invocation. */ > + h = request->index = mh_strnptr_new(); > + if (h == NULL) { > + luaL_unref(L, LUA_REGISTRYINDEX, field_value_ref); > + return luaL_error(L, "out of memory"); > + } > + } > + /* Insert a reference to the new value into the index. */ > + struct mh_strnptr_node_t node = { > + .str = field_name, > + .len = field_name_len, > + .hash = mh_strn_hash(field_name, field_name_len), 2. lua_hashstring() might be faster. You could reuse already calculated hash value from Lua. Although for that 'field_name' must be of exactly string type (lua_tolstring() works not only for strings). So if you would make a more precise type check, you could avoid hash calculation completely. Or use lua_hash() for non-string values' strings. (But I might be wrong about how lua_hashstring() works for non-string values, you would need to double-check). > + .val = (void *)(intptr_t)field_value_ref, 3. Can you simply cast to void * right away? Why do you need the intermediate intptr_t cast? > + }; > + struct mh_strnptr_node_t old_node = { NULL, 0, 0, NULL }; > + struct mh_strnptr_node_t *p_old_node = &old_node; > + if (mh_strnptr_put(h, &node, &p_old_node, > + NULL) == mh_end(h)) { > + luaL_unref(L, LUA_REGISTRYINDEX, field_value_ref); > + return luaL_error(L, "out of memory"); > + } > + /* Drop the reference to the overwritten value. */ > + if (p_old_node != NULL) > + luaL_unref(L, LUA_REGISTRYINDEX, (intptr_t)old_node.val); > + return 0; > +}