[Tarantool-patches] [PATCH v1 luajit 07/41] perf: adjust coroutine-ring in LuaJIT-benches

Sergey Kaplun skaplun at tarantool.org
Fri Dec 26 11:11:49 MSK 2025


Hi, Sergey!
Thanks for the review!
Fixes your comments and added the description to the benchmark.

On 13.11.25, Sergey Bronnikov wrote:
> Hi, Sergey!
> 
> thanks for the patch! See my comments.
> 
> Sergey
> 
> On 10/24/25 13:50, Sergey Kaplun wrote:
> > This patch adjusts the aforementioned test to use the benchmark
> > framework introduced before. The default arguments are adjusted
> > according to the <PARAM_x86.txt> file. The arguments to the script still
> > can be provided in the command line run.
> > ---
> >   perf/LuaJIT-benches/coroutine-ring.lua | 45 ++++++++++++++++----------
> >   1 file changed, 28 insertions(+), 17 deletions(-)
> >
> > diff --git a/perf/LuaJIT-benches/coroutine-ring.lua b/perf/LuaJIT-benches/coroutine-ring.lua
> > index 1e8c5ef6..1b86a5ba 100644
> > --- a/perf/LuaJIT-benches/coroutine-ring.lua
> > +++ b/perf/LuaJIT-benches/coroutine-ring.lua
> > @@ -1,3 +1,5 @@

Also, added the comment with the benchmark description as we discussed
offline.

===================================================================
diff --git a/perf/LuaJIT-benches/coroutine-ring.lua b/perf/LuaJIT-benches/coroutine-ring.lua
index 1b86a5ba..8efe7b2a 100644
--- a/perf/LuaJIT-benches/coroutine-ring.lua
+++ b/perf/LuaJIT-benches/coroutine-ring.lua
@@ -1,3 +1,9 @@
+-- The benchmark to check the performance of coroutine interaction
+-- to test possible "death by concurrency," when one coroutine
+-- is active and others are waiting their turn.
+-- For the details see:
+-- https://pybenchmarks.org/u64q/performance.php?test=threadring
+
 local bench = require("bench").new(arg)

 -- The Computer Language Benchmarks Game
===================================================================

> > +local bench = require("bench").new(arg)
> > +
> >   -- The Computer Language Benchmarks Game
> >   --http://shootout.alioth.debian.org/
> >   -- contributed by Sam Roberts

<snipped>

> > +bench:add({
> > +  name = "coroutine_ring",
> > +  payload = function()
> > +    local token     = 0
> a single whitespace before "="

Fixed. See the iterative patch below.

> > +    -- create all threads
> First letter is in uppercase and a dot at the end.

Fixed. See the iterative patch below.

> > +    local threads   = {}
> a single whitespace before "="

Fixed. See the iterative patch below.

> > +    for id = 1, poolsize do
> > +      threads[id] = create(body)
> > +    end
> > +
> > +    -- send the token
> First letter is in uppercase and a dot at the end.

Fixed:

===================================================================
diff --git a/perf/LuaJIT-benches/coroutine-ring.lua b/perf/LuaJIT-benches/coroutine-ring.lua
index 1b86a5ba..e1455205 100644
--- a/perf/LuaJIT-benches/coroutine-ring.lua
+++ b/perf/LuaJIT-benches/coroutine-ring.lua
@@ -27,14 +27,14 @@ end
 bench:add({
   name = "coroutine_ring",
   payload = function()
-    local token     = 0
-    -- create all threads
-    local threads   = {}
+    local token = 0
+    -- Create all threads.
+    local threads = {}
     for id = 1, poolsize do
       threads[id] = create(body)
     end
 
-    -- send the token
+    -- Send the token.
     repeat
       if id == poolsize then
         id = 1
===================================================================

> > +    repeat
> > +      if id == poolsize then
> > +        id = 1
> > +      else
> > +        id = id + 1
> > +      end
> > +      ok, token = resume(threads[id], token)
> > +    until token == n
> > +    return id
> > +  end,
> > +  checker = function(id) return id == (n % poolsize + 1) end,
> > +  items = n,
> > +})
> > +
> > +bench:run_and_report()
> >   
> > -io.write(id, "\n")

I've also refactored the benchmark to make it performance match the
original version:

===================================================================
diff --git a/perf/LuaJIT-benches/coroutine-ring.lua b/perf/LuaJIT-benches/coroutine-ring.lua
index 8efe7b2a..747a2ecc 100644
--- a/perf/LuaJIT-benches/coroutine-ring.lua
+++ b/perf/LuaJIT-benches/coroutine-ring.lua
@@ -17,13 +17,8 @@ local n         = tonumber(arg and arg[1]) or 2e7
 local poolsize  = 503
 
 -- cache these to avoid global environment lookups
-local create    = coroutine.create
-local resume    = coroutine.resume
 local yield     = coroutine.yield
 
-local id        = 1
-local ok
-
 local body = function(token)
   while true do
     token = yield(token + 1)
@@ -33,7 +28,18 @@ end
 bench:add({
   name = "coroutine_ring",
   payload = function()
+    -- Cache to avoid upvalue lookups.
     local token = 0
+    local n = n
+    local poolsize = poolsize
+
+    -- Cache these to avoid global environment lookups.
+    local create = coroutine.create
+    local resume = coroutine.resume
+
+    local id = 1
+    local ok
+
     -- Create all threads.
     local threads = {}
     for id = 1, poolsize do
===================================================================

Also, removed excess empty line at the end of the file:
===================================================================
diff --git a/perf/LuaJIT-benches/coroutine-ring.lua b/perf/LuaJIT-benches/coroutine-ring.lua
index 8efe7b2a..747a2ecc 100644
--- a/perf/LuaJIT-benches/coroutine-ring.lua
+++ b/perf/LuaJIT-benches/coroutine-ring.lua
@@ -56,4 +62,3 @@ bench:add({
 })

 bench:run_and_report()
-
===================================================================

-- 
Best regards,
Sergey Kaplun


More information about the Tarantool-patches mailing list