From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: Vladimir Davydov <vdavydov@tarantool.org> Cc: tarantool-patches@dev.tarantool.org Subject: Re: [Tarantool-patches] [PATCH] net.box: allow to store user-defined fields in future object Date: Thu, 12 Aug 2021 21:02:46 +0300 [thread overview] Message-ID: <c3f8c9e2-f768-c003-a437-b23ba618e11c@tarantool.org> (raw) In-Reply-To: <ba159cef7c33bb8eef3044a18dfdef9f0ac330e1.1628614117.git.vdavydov@tarantool.org> 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; > +}
next prev parent reply other threads:[~2021-08-12 18:02 UTC|newest] Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-08-10 16:53 Vladimir Davydov via Tarantool-patches 2021-08-10 17:15 ` Cyrill Gorcunov via Tarantool-patches 2021-08-11 7:49 ` Vladimir Davydov via Tarantool-patches 2021-08-12 17:07 ` [Tarantool-patches] [PATCH v2] " Vladimir Davydov via Tarantool-patches 2021-08-12 18:02 ` Vladislav Shpilevoy via Tarantool-patches [this message] 2021-08-12 18:13 ` [Tarantool-patches] [PATCH] " Vladislav Shpilevoy via Tarantool-patches 2021-08-13 10:01 ` Vladimir Davydov via Tarantool-patches 2021-08-12 18:13 ` Igor Munkin via Tarantool-patches 2021-08-13 9:57 ` Vladimir Davydov via Tarantool-patches 2021-08-13 9:59 ` [Tarantool-patches] [PATCH v3] " Vladimir Davydov via Tarantool-patches 2021-08-14 11:17 ` Igor Munkin via Tarantool-patches 2021-08-16 8:27 ` Vladimir Davydov via Tarantool-patches 2021-08-16 8:56 ` Igor Munkin via Tarantool-patches 2021-08-16 11:32 ` Vitaliia Ioffe via Tarantool-patches 2021-08-16 11:35 ` Vladimir Davydov via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=c3f8c9e2-f768-c003-a437-b23ba618e11c@tarantool.org \ --to=tarantool-patches@dev.tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --cc=vdavydov@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH] net.box: allow to store user-defined fields in future object' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox