Skip to content

Commit

Permalink
Optimisation: reduce size of ScheduleAfterOp
Browse files Browse the repository at this point in the history
By moving the duration in the unused timer field, we save on a few bytes
for the operational state.

Test Plan: `dub test`
  • Loading branch information
skoppe committed Dec 29, 2024
1 parent 24dabe5 commit 9efee0d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
13 changes: 10 additions & 3 deletions source/concurrency/io/iouring.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ struct IOUringContext {
return RunSender!(Sender)(&this, sender);
}

private void addTimer(ref Timer timer, Duration dur) @trusted shared {
timer.scheduled_at = dur.split!"hnsecs".hnsecs;
timer.command = TimerCommand.Register;
private void addTimer(ref Timer timer) @trusted shared {
timers.push(&timer);
wakeup();
}

private void addTimer(ref Timer timer, Duration dur) @trusted shared {
timer.setScheduledAt(dur);
addTimer(timer);
}

private void cancelTimer(ref Timer timer) @trusted shared {
timer.command = TimerCommand.Cancel;
timers.push(&timer);
Expand Down Expand Up @@ -475,6 +478,10 @@ private struct IOUringTimer {

private shared IOUringContext* context;

void addTimer(ref Timer timer) @safe {
context.addTimer(timer);
}

void addTimer(ref Timer timer, Duration dur) @safe {
context.addTimer(timer, dur);
}
Expand Down
14 changes: 12 additions & 2 deletions source/concurrency/scheduler.d
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ struct ScheduleAfterOp(Worker, Receiver) {
}

Worker worker;
Duration dur;
Receiver receiver;
Timer timer;
shared StopCallback stopCb;
Expand All @@ -144,6 +143,12 @@ struct ScheduleAfterOp(Worker, Receiver) {
@disable void opAssign(typeof(this) rhs) nothrow @safe @nogc;
@disable void opAssign(ref typeof(this) rhs) nothrow @safe @nogc;

this(return Worker worker, Duration dur, return Receiver receiver) @trusted scope {
this.worker = worker;
this.receiver = receiver;
this.timer.setScheduledAt(dur);
}

// ~this() @safe scope {}
void start() @trusted scope nothrow {
if (receiver.getStopToken().isStopRequested) {
Expand All @@ -156,7 +161,7 @@ struct ScheduleAfterOp(Worker, Receiver) {

try {
timer.userdata = cast(void delegate(TimerTrigger) @safe shared nothrow) &trigger;
worker.addTimer(timer, dur);
worker.addTimer(timer);
} catch (Exception e) {
receiver.setError(e);
return;
Expand Down Expand Up @@ -257,6 +262,11 @@ class ManualTimeWorker {
return ManualTimeScheduler(this);
}

void addTimer(ref Timer timer) @trusted shared {
import core.time : hnsecs;
addTimer(timer, timer.scheduled_at.hnsecs);
}

void addTimer(ref Timer timer, Duration dur) @trusted shared {
import core.atomic : atomicOp;
with (lock()) {
Expand Down
5 changes: 5 additions & 0 deletions source/concurrency/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ package struct LocalThreadWorker {
executor.queue.push(new WorkNode(WorkItem(dg)));
}

void addTimer(ref Timer timer) @trusted {
import core.time : hnsecs;
addTimer(timer, timer.scheduled_at.hnsecs);
}

void addTimer(ref Timer timer, Duration dur) @trusted {
// import core.atomic : atomicOp;
// ulong id = executor.nextTimerId.atomicOp!("+=")(1);
Expand Down
4 changes: 4 additions & 0 deletions source/concurrency/timingwheels.d
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ struct ListElement(T) {
ushort position = 0xffff;
TimerCommand command;
ListElement!T* prev, next;
void setScheduledAt(Duration dur) @system {
scheduled_at = dur.split!"hnsecs".hnsecs;
command = TimerCommand.Register;
}
}

enum TimerCommand : ushort {
Expand Down

0 comments on commit 9efee0d

Please sign in to comment.