summaryrefslogtreecommitdiff
path: root/src/common/loop.c
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-12 14:27:51 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-12 14:27:51 +0300
commit9ed89c17dee752154c36d71cae93d20cccfc71ef (patch)
treea32cd894643ff6f6ebce83dd745bd24b35eb6520 /src/common/loop.c
parentaedd863e6f66fd446227e9b107bc9c1970092634 (diff)
perf: optimize hot-path loops, data structures, and cache locality
Diffstat (limited to 'src/common/loop.c')
-rw-r--r--src/common/loop.c54
1 files changed, 36 insertions, 18 deletions
diff --git a/src/common/loop.c b/src/common/loop.c
index a63df16..18d62b1 100644
--- a/src/common/loop.c
+++ b/src/common/loop.c
@@ -25,9 +25,9 @@ struct yasl_fd_source {
struct yasl_timer_source {
yasl_timer_cb fn;
void *ud;
- yasl_timer_id id;
uint64_t interval_ns;
uint64_t deadline_ns;
+ yasl_timer_id id;
bool deleted;
};
@@ -330,13 +330,18 @@ int yasl_loop_step(struct yasl_loop *l, int timeout_ms)
for (int i = 0; i < count; i++) {
if (pcopy[i].revents) {
short revents = pcopy[i].revents;
- // Verify that the source was not deleted by an earlier callback in this step
+ // Fast-path: check if source is still at index i before linear scan
bool valid = false;
- for (int j = 0; j < l->count; j++) {
- if (l->fds[j].id == scopy[i].id) {
- l->pollfds[j].revents = 0;
- valid = true;
- break;
+ if (i < l->count && l->fds[i].id == scopy[i].id) {
+ l->pollfds[i].revents = 0;
+ valid = true;
+ } else {
+ for (int j = 0; j < l->count; j++) {
+ if (l->fds[j].id == scopy[i].id) {
+ l->pollfds[j].revents = 0;
+ valid = true;
+ break;
+ }
}
}
if (valid && scopy[i].fn) {
@@ -367,18 +372,31 @@ int yasl_loop_step(struct yasl_loop *l, int timeout_ms)
uint64_t next_interval = timer_src.fn(ud, tid, interval);
handled++;
- for (int j = 0; j < l->timer_count; j++) {
- if (l->timers[j].id == tid) {
- if (next_interval == 0 || l->timers[j].deleted) {
- l->timers[j].deleted = true;
- } else {
- l->timers[j].interval_ns = next_interval;
- if (l->timers[j].deadline_ns + next_interval > now)
- l->timers[j].deadline_ns += next_interval;
- else
- l->timers[j].deadline_ns = now + next_interval;
+ // Fast-path: timer is usually still at index i unless an earlier timer was deleted
+ if (i < l->timer_count && l->timers[i].id == tid) {
+ if (next_interval == 0 || l->timers[i].deleted) {
+ l->timers[i].deleted = true;
+ } else {
+ l->timers[i].interval_ns = next_interval;
+ if (l->timers[i].deadline_ns + next_interval > now)
+ l->timers[i].deadline_ns += next_interval;
+ else
+ l->timers[i].deadline_ns = now + next_interval;
+ }
+ } else {
+ for (int j = 0; j < l->timer_count; j++) {
+ if (l->timers[j].id == tid) {
+ if (next_interval == 0 || l->timers[j].deleted) {
+ l->timers[j].deleted = true;
+ } else {
+ l->timers[j].interval_ns = next_interval;
+ if (l->timers[j].deadline_ns + next_interval > now)
+ l->timers[j].deadline_ns += next_interval;
+ else
+ l->timers[j].deadline_ns = now + next_interval;
+ }
+ break;
}
- break;
}
}
}