supervision - discussion about system services, daemon supervision, init, runlevel management, and tools such as s6 and runit
 help / color / mirror / Atom feed
From: Adam Joseph <adam@westernsemico.com>
To: supervision@list.skarnet.org
Cc: Adam Joseph <adam@westernsemico.com>
Subject: [PATCH 2/3] s6-rc-update.c: rewrite O(n^2) loop as O(n) complexity
Date: Mon, 25 Sep 2023 17:44:17 -0700	[thread overview]
Message-ID: <20230926004418.24990-2-adam@westernsemico.com> (raw)
In-Reply-To: <20230926004418.24990-1-adam@westernsemico.com>

Prior to this commit, s6-rc-update had a loop with the following
comment:

  This part runs in O(oldn*newn). There are no syscalls in the loop,
  so it should still be negligible unless you have 10k services.

The loop does the following:

   for each old service which was already up
     for each new service
       if the old service converts to the new service,
         set some new service flags based on old service flags

The loop is O(n^2) due to the nested iteration.  We can rewrite this
with a single iteration by making use of the invimage[] array, which
maps from new services to old services:

   for each new service
     if *ANY* old service converts to the new service,
       set some new service flags based on that old service's flags

This takes advantage of the fact that invimage is an injective
(partial) function.

Signed-off-by: Adam Joseph <adam@westernsemico.com>
---
 src/s6-rc/s6-rc-update.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/src/s6-rc/s6-rc-update.c b/src/s6-rc/s6-rc-update.c
index c1074de..e0726a2 100644
--- a/src/s6-rc/s6-rc-update.c
+++ b/src/s6-rc/s6-rc-update.c
@@ -257,20 +257,17 @@ static void compute_transitions (char const *convfile, unsigned char *oldstate,
    /*
       Convert the old state to the new state: if an old service is up,
       the new service will be either up or wanted up.
-      This part runs in O(oldn*newn). There are no syscalls in the loop,
-      so it should still be negligible unless you have 10k services.
    */
 
-    i = oldn ;
+    i = newn;
     while (i--)
-      if (oldstate[i] & OLDSTATE_WAS_UP) {
-        unsigned int j = newn ;
-        while (j--)
-          if (bitarray_peek(conversion_table + i * newm, j))
-            newstate[j] |=
-              (oldstate[i] & OLDSTATE_WANT_DOWN_OR_RESTART)
+      if (newstate[i] & NEWSTATE_IS_CONVERSION_TARGET) {
+        if (oldstate[invimage[i]] & OLDSTATE_WAS_UP) {
+            newstate[i] |=
+              (oldstate[invimage[i]] & OLDSTATE_WANT_DOWN_OR_RESTART)
               ? NEWSTATE_INCLUDE_IN_UP_TRANSITION
               : (NEWSTATE_WAS_UP | NEWSTATE_WANT_UP_AFTER_CLOSURE) ;
+        }
       }
 
 
-- 
2.41.0


  reply	other threads:[~2023-09-26  0:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-26  0:44 [PATCH 1/3] s6-rc-update.c: add #define constants for bitflags Adam Joseph
2023-09-26  0:44 ` Adam Joseph [this message]
2023-09-26  0:57   ` [PATCH 2/3] s6-rc-update.c: rewrite O(n^2) loop as O(n) complexity Adam Joseph
2023-09-26  0:44 ` [PATCH 3/3] [WIP] s6-rc-update.c: add additional comments Adam Joseph
2023-09-26  0:59   ` Adam Joseph

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=20230926004418.24990-2-adam@westernsemico.com \
    --to=adam@westernsemico.com \
    --cc=supervision@list.skarnet.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).