chiark / gitweb /
sd-event: do not arm timers unnecessarily
authorTom Gundersen <teg@jklm.no>
Wed, 13 Aug 2014 22:22:27 +0000 (00:22 +0200)
committerTom Gundersen <teg@jklm.no>
Wed, 13 Aug 2014 23:29:51 +0000 (01:29 +0200)
Rather than recalculating the next timeout on every loop, we only do it when something changed.

src/libsystemd/sd-event/sd-event.c

index bf6f01c..d654914 100644 (file)
@@ -131,6 +131,8 @@ struct clock_data {
         Prioq *earliest;
         Prioq *latest;
         usec_t next;
+
+        bool needs_rearm:1;
 };
 
 struct sd_event {
@@ -616,6 +618,7 @@ static void source_disconnect(sd_event_source *s) {
 
                 prioq_remove(d->earliest, s, &s->time.earliest_index);
                 prioq_remove(d->latest, s, &s->time.latest_index);
+                d->needs_rearm = true;
                 break;
         }
 
@@ -715,6 +718,7 @@ static int source_set_pending(sd_event_source *s, bool b) {
 
                 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
                 prioq_reshuffle(d->latest, s, &s->time.latest_index);
+                d->needs_rearm = true;
         }
 
         return 0;
@@ -898,6 +902,8 @@ _public_ int sd_event_add_time(
         if (r < 0)
                 goto fail;
 
+        d->needs_rearm = true;
+
         if (ret)
                 *ret = s;
 
@@ -1408,6 +1414,7 @@ _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
 
                         prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+                        d->needs_rearm = true;
                         break;
                 }
 
@@ -1471,6 +1478,7 @@ _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
 
                         prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+                        d->needs_rearm = true;
                         break;
                 }
 
@@ -1548,6 +1556,7 @@ _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
 
         prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+        d->needs_rearm = true;
 
         return 0;
 }
@@ -1582,6 +1591,7 @@ _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec
         assert(d);
 
         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+        d->needs_rearm = true;
 
         return 0;
 }
@@ -1745,6 +1755,11 @@ static int event_arm_timer(
         assert(e);
         assert(d);
 
+        if (_likely_(!d->needs_rearm))
+                return 0;
+        else
+                d->needs_rearm = false;
+
         a = prioq_peek(d->earliest);
         if (!a || a->enabled == SD_EVENT_OFF) {