Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sleep_until functionality. #28

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

ErisianArchitect
Copy link

I felt like sleep_until functionality was missing, so I added it in. I hope that's alright.

src/lib.rs Outdated
Comment on lines 164 to 176
let accuracy = Duration::new(0, self.native_accuracy_ns);
let start = Instant::now();
let duration = instant.duration_since(start);
if duration > accuracy {
native_sleep(duration - accuracy);
}
// spin until instant
while Instant::now() < instant {
match self.spin_strategy {
SpinStrategy::YieldThread => thread::yield_now(),
SpinStrategy::SpinLoopHint => std::hint::spin_loop(),
}
}
Copy link
Owner

@alexheretic alexheretic Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be implemented simply as:

Suggested change
let accuracy = Duration::new(0, self.native_accuracy_ns);
let start = Instant::now();
let duration = instant.duration_since(start);
if duration > accuracy {
native_sleep(duration - accuracy);
}
// spin until instant
while Instant::now() < instant {
match self.spin_strategy {
SpinStrategy::YieldThread => thread::yield_now(),
SpinStrategy::SpinLoopHint => std::hint::spin_loop(),
}
}
self.sleep(instant.saturating_duration_since(Instant::now()))

That would mean calling Instant::now() twice, we can also avoid the 2 calls by having a private fn sleep that takes both duration & start instant. But maybe this detail isn't really a big deal anyway so I'd be ok with the simpler version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.sleep calls Instant::now() as well, so there would still be two calls. I'm not entirely sure what you mean by the private sleep fn. Wouldn't that just be the same thing?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The private fn would exist to solve that problem:

    fn sleep_from(self, start: Instant, duration: Duration) {
        let accuracy = Duration::new(0, self.native_accuracy_ns);
        if duration > accuracy {
            native_sleep(duration - accuracy);
        }
        // spin the rest of the duration
        while start.elapsed() < duration {
            match self.spin_strategy {
                SpinStrategy::YieldThread => thread::yield_now(),
                SpinStrategy::SpinLoopHint => std::hint::spin_loop(),
            }
        }
    }

    pub fn sleep(self, duration: Duration) {
        self.sleep_from(Instant::now(), duration)
    }

    pub fn sleep_until(self, deadline: Instant) {
        let start = Instant::now();
        self.sleep_from(start, deadline.saturating_duration_since(start))
    }

Copy link
Author

@ErisianArchitect ErisianArchitect Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elapsed() also calls Instant::now().

pub fn elapsed(&self) -> Duration {
    Instant::now() - *self
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least by comparing Instant::now() against instant you save an instruction.

(Instant::now() - *self) < duration

compared to

Instant::now() < instant

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elapsed() also calls Instant::now().

Yeah but there is no getting around calling now() in the spin loop. I'd rather both methods had common implementations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if sleep_until was the common implementation that sleep called?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's possible, but I don't think it makes quite as much sense. The first thing you have to do in sleep_until is compute the sleep duration, so we can compare to native sleep accuracy, so "sleeping a duration" seems to be the more founding operation to base both fns on.

start.elapsed() < duration vs Instant::now() < deadline is an implementation detail that applies to either impl (ie we could change that in sleep now regardless of sleep_until existence) so I don't think it is relevant.

src/lib.rs Outdated Show resolved Hide resolved
@alexheretic
Copy link
Owner

Thanks yeah I think having this makes sense 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants