Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH 15/20] net.box: rewrite request implementation in C
Date: Wed, 4 Aug 2021 19:14:58 +0300	[thread overview]
Message-ID: <20210804161458.ma42lpoh3lh5x3fi@esperanza> (raw)
In-Reply-To: <20210804153526.gr2uyri5dg52zw4b@esperanza>

On Wed, Aug 04, 2021 at 06:35:26PM +0300, Vladimir Davydov wrote:
> On Wed, Aug 04, 2021 at 03:30:01PM +0300, Vladimir Davydov wrote:
> > On Mon, Aug 02, 2021 at 11:54:43PM +0200, Vladislav Shpilevoy wrote:
> > > > +	lua_pushcclosure(L, luaT_netbox_request_iterator_next, 2);
> > > 
> > > 8. Push of cfunctions, especially closures, on a regular basis might
> > > be expensive. Could you please try to make it a non-closure function
> > > and cache its reference like I showed in the proposal about request __gc?
> > 
> > My test doesn't check performance of the request iterator so I need to
> > come up with a new test first to check this hypothesis. I'll reply to
> > this email with the results once they're ready.
> 
> Using getref instead of pushcclosure does improve performance of
> the final version by about 10%:
> 
>                 KRPS (WALL TIME)   KRPS (PROC TIME)
> pushcclosure :     233 +-  3     :     344 +-  7
> getref       :     259 +-  4     :     404 +- 10
> 
> The test is here:
> 
> https://gist.github.com/locker/9df4eaaf1bc889b16706640711c946f7
> 
> Amended the patch, thanks. The incremental diff is below:
> --
> diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
> index 7ef40792320e..fadd71a6813b 100644
> --- a/src/box/lua/net_box.c
> +++ b/src/box/lua/net_box.c
> @@ -131,11 +131,20 @@ struct netbox_request {
>  	 * the response hasn't been received yet.
>  	 */
>  	struct error *error;
> +	/** Timeout for luaT_netbox_request_iterator_next(). */
> +	double iterator_timeout;
>  };

It's totally wrong to store iterator_timeout in netbox_request, because
there may be more than one iterator open for the same request. Instead
we should push a table with {request, timeout}, just like the Lua
implementation did. This is a bit slower, but still faster than using
pushcclosure:

                   KRPS (WALL TIME)   KRPS (PROC TIME)
 pushcclosure   :     233 +-  3     :     344 +-  7
 getref         :     259 +-  4     :     404 +- 10
 getref + table :     257 +-  7     :     397 +- 15

The new incremental diff is below:
--
diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index de9c11736603..45b0c7f3bac3 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -136,6 +136,13 @@ struct netbox_request {
 static const char netbox_registry_typename[] = "net.box.registry";
 static const char netbox_request_typename[] = "net.box.request";
 
+/**
+ * Instead of pushing luaT_netbox_request_iterator_next with lua_pushcclosure
+ * in luaT_netbox_request_pairs, we get it by reference, because it works
+ * faster.
+ */
+static int luaT_netbox_request_iterator_next_ref;
+
 static void
 netbox_request_destroy(struct netbox_request *request)
 {
@@ -1488,8 +1495,8 @@ luaT_netbox_request_discard(struct lua_State *L)
 
 /**
  * Gets the next message or the final result. Takes the index of the last
- * returned message as a second argument (the first argument is ignored).
- * The request and timeout are passed as up-values (see request.pairs()).
+ * returned message as a second argument. The request and timeout are passed in
+ * the first argument as a table (see request.pairs()).
  *
  * On success returns the index of the current message (used by the iterator
  * implementation to continue iteration) and an object, which is either the
@@ -1504,9 +1511,12 @@ luaT_netbox_request_discard(struct lua_State *L)
 static int
 luaT_netbox_request_iterator_next(struct lua_State *L)
 {
-	struct netbox_request *request = luaT_check_netbox_request(
-		L, lua_upvalueindex(1));
-	double timeout = lua_tonumber(L, lua_upvalueindex(2));
+	/* The first argument is a table: {request, timeout}. */
+	lua_rawgeti(L, 1, 1);
+	struct netbox_request *request = luaT_check_netbox_request(L, -1);
+	lua_rawgeti(L, 1, 2);
+	double timeout = lua_tonumber(L, -1);
+	/* The second argument is the index of the last returned message. */
 	if (luaL_isnull(L, 2)) {
 		/* The previous call returned an error. */
 		goto stop;
@@ -1582,8 +1592,17 @@ luaT_netbox_request_pairs(struct lua_State *L)
 			lua_pop(L, 1);
 		lua_pushnumber(L, TIMEOUT_INFINITY);
 	}
-	lua_pushcclosure(L, luaT_netbox_request_iterator_next, 2);
-	lua_pushnil(L);
+	lua_settop(L, 2);
+	/* Create a table passed to next(): {request, timeout}. */
+	lua_createtable(L, 2, 0);
+	lua_insert(L, 1);
+	lua_rawseti(L, 1, 2); /* timeout */
+	lua_rawseti(L, 1, 1); /* request */
+	/* Push the next() function. It must go first. */
+	lua_rawgeti(L, LUA_REGISTRYINDEX,
+		    luaT_netbox_request_iterator_next_ref);
+	lua_insert(L, 1);
+	/* Push the iterator index. */
 	lua_pushinteger(L, 0);
 	return 3;
 }
@@ -1749,6 +1768,9 @@ netbox_dispatch_response_console(struct lua_State *L)
 int
 luaopen_net_box(struct lua_State *L)
 {
+	lua_pushcfunction(L, luaT_netbox_request_iterator_next);
+	luaT_netbox_request_iterator_next_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+
 	static const struct luaL_Reg netbox_registry_meta[] = {
 		{ "__gc",           luaT_netbox_registry_gc },
 		{ "reset",          luaT_netbox_registry_reset },

  reply	other threads:[~2021-08-04 16:15 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-23 11:07 [Tarantool-patches] [PATCH 00/20] Rewrite performance critical parts of net.box " Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 01/20] net.box: fix console connection breakage when request is discarded Vladimir Davydov via Tarantool-patches
2021-07-28 22:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 10:40     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 02/20] net.box: wake up wait_result callers " Vladimir Davydov via Tarantool-patches
2021-07-29 10:47   ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 03/20] net.box: do not check worker_fiber in request:result, is_ready Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 04/20] net.box: remove decode_push from method_decoder table Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 05/20] net.box: use decode_tuple instead of decode_get Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 06/20] net.box: rename request.ctx to request.format Vladimir Davydov via Tarantool-patches
2021-07-28 22:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 10:54     ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:39       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30  8:15         ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 07/20] net.box: use integer id instead of method name Vladimir Davydov via Tarantool-patches
2021-07-28 22:50   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 11:30     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 08/20] net.box: remove useless encode optimization Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 09/20] net.box: rewrite request encoder in C Vladimir Davydov via Tarantool-patches
2021-07-28 22:51   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 14:08     ` Vladimir Davydov via Tarantool-patches
2021-07-29 14:10       ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 10/20] lua/utils: make char ptr Lua CTIDs public Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 11/20] net.box: rewrite response decoder in C Vladimir Davydov via Tarantool-patches
2021-07-27 14:07   ` Cyrill Gorcunov via Tarantool-patches
2021-07-27 14:14     ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:39   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30  8:44     ` Vladimir Davydov via Tarantool-patches
2021-07-30 22:12       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-02  7:36         ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 12/20] net.box: rewrite error " Vladimir Davydov via Tarantool-patches
2021-07-30 22:13   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-02  8:00     ` Vladimir Davydov via Tarantool-patches
2021-08-02 21:47       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 13/20] net.box: rewrite send_and_recv_{iproto, console} " Vladimir Davydov via Tarantool-patches
2021-08-02 21:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-03 15:44     ` Vladimir Davydov via Tarantool-patches
2021-08-03 23:06       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 13:56         ` Vladimir Davydov via Tarantool-patches
2021-08-04 21:18           ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05  8:37             ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 14/20] net.box: rename netbox_{prepare, encode}_request to {begin, end} Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 15/20] net.box: rewrite request implementation in C Vladimir Davydov via Tarantool-patches
2021-08-02 21:54   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 12:30     ` Vladimir Davydov via Tarantool-patches
2021-08-04 15:35       ` Vladimir Davydov via Tarantool-patches
2021-08-04 16:14         ` Vladimir Davydov via Tarantool-patches [this message]
2021-08-04 21:20       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 12:46         ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 16/20] net.box: store next_request_id in C code Vladimir Davydov via Tarantool-patches
2021-08-03 23:06   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 16:25     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 17/20] net.box: rewrite console handlers in C Vladimir Davydov via Tarantool-patches
2021-08-03 23:07   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 11:53     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 18/20] net.box: rewrite iproto " Vladimir Davydov via Tarantool-patches
2021-08-03 23:08   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 11:54     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 19/20] net.box: merge new_id, new_request and encode_method Vladimir Davydov via Tarantool-patches
2021-08-03 23:08   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 11:55     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 20/20] net.box: do not create request object in Lua for sync requests Vladimir Davydov via Tarantool-patches
2021-08-03 23:09   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 12:23     ` Vladimir Davydov via Tarantool-patches
2021-07-23 12:48 ` [Tarantool-patches] [PATCH 00/20] Rewrite performance critical parts of net.box in C Vladimir Davydov via Tarantool-patches
2021-07-26  7:26 ` Kirill Yukhin via Tarantool-patches
2021-07-27  9:59   ` Vladimir Davydov via Tarantool-patches
2021-07-28 22:51 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 11:33   ` Vladimir Davydov via Tarantool-patches
2021-07-29 15:23     ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:38       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30 10:04         ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:40 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30  8:16   ` Vladimir Davydov via Tarantool-patches
2021-08-03 23:05 ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 12:40   ` Vladimir Davydov via Tarantool-patches
2021-08-05 20:59 ` Vladislav Shpilevoy via Tarantool-patches
2021-08-09 11:22 ` Igor Munkin via Tarantool-patches
2021-08-09 11:48   ` Vitaliia Ioffe via Tarantool-patches
2021-08-09 13:56     ` 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=20210804161458.ma42lpoh3lh5x3fi@esperanza \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 15/20] net.box: rewrite request implementation in C' \
    /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