• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust accurate sleeping. Only use native sleep as far as it can be trusted, then spin.

spin_sleep crates.io Documentation

Accurate sleeping. Only use native sleep as far as it can be trusted, then spin.

The problem with thread::sleep is it isn't always very accurate, and this accuracy varies on platform and state. Spinning is as accurate as we can get, but consumes the CPU rather ungracefully.

This library adds a middle ground, using a configurable native accuracy setting allowing thread::sleep to wait the bulk of a sleep time, and spin the final section to guarantee accuracy.

SpinSleeper

The simplest usage with default native accuracy is a drop in replacement for thread::sleep.

spin_sleep::sleep(Duration::new(1, 12_550_000));

Configure

More advanced usage, including setting a custom native accuracy, can be achieved by constructing a SpinSleeper.

// Create a new sleeper that trusts native thread::sleep with 100μs accuracy
let spin_sleeper = spin_sleep::SpinSleeper::new(100_000)
    .with_spin_strategy(spin_sleep::SpinStrategy::YieldThread);

// Sleep for 1.01255 seconds, this will:
//  - thread:sleep for 1.01245 seconds, i.e., 100μs less than the requested duration
//  - spin until total 1.01255 seconds have elapsed
spin_sleeper.sleep(Duration::new(1, 12_550_000));

Sleep can also be requested in f64 seconds or u64 nanoseconds (useful when used with time crate)

spin_sleeper.sleep_s(1.01255);
spin_sleeper.sleep_ns(1_012_550_000);

OS-specific default settings should be good enough for most cases.

let sleeper = SpinSleeper::default();

LoopHelper

For controlling & report rates (e.g., game FPS) this crate provides LoopHelper. A SpinSleeper is used to maximise sleeping accuracy.

use spin_sleep::LoopHelper;

let mut loop_helper = LoopHelper::builder()
    .report_interval_s(0.5) // report every half a second
    .build_with_target_rate(250.0); // limit to 250 FPS if possible

let mut current_fps = None;

loop {
    let delta = loop_helper.loop_start(); // or .loop_start_s() for f64 seconds

    // compute_something(delta);

    if let Some(fps) = loop_helper.report_rate() {
        current_fps = Some(fps);
    }

    // render_fps(current_fps);

    loop_helper.loop_sleep(); // sleeps to achieve a 250 FPS rate
}

Windows Accuracy

Windows has particularly poor accuracy by default (~15ms), spin_sleep will automatically select the best accuracy on Windows generally achieving ~1ms native sleep accuracy (Since 0.3.3).

Minimum supported rust compiler

This crate is maintained with latest stable rust.

More Repositories

1

glyph-brush

Fast GPU cached text rendering
Rust
675
star
2

ab-av1

AV1 re-encoding using ffmpeg, svt-av1 & vmaf.
Rust
380
star
3

ab-glyph

Rust API for loading, scaling, positioning and rasterizing OpenType font glyphs
Rust
345
star
4

aurto

Arch Linux AUR tool for managing an auto-updating local 'aurto' package repository
Shell
134
star
5

apart-gtk

Linux GUI for cloning & restoring disk partitions to & from compressed image files
Python
63
star
6

linked-hash-set

Rust HashSet with insertion ordering
Rust
40
star
7

dynamics

Java library for handling nested dynamic data
Java
25
star
8

single-value-channel

Rust concurrent single-value update and receive channel
Rust
21
star
9

owned-ttf-parser

ttf-parser plus support for owned data
Rust
18
star
10

int-hash

Very fast, very simple hash algorithm designed for use in Rust integer hash maps & sets
Rust
18
star
11

vimg

CLI for video images. Generates animated video contact sheets fast.
Rust
16
star
12

ktx

Rust KTX texture storage format parsing
Rust
11
star
13

benjamin-batchly

Low latency batching tool. Bundle lots of single concurrent operations into sequential batches of work.
Rust
11
star
14

gfx-shader-watch

Gfx utility for watching shaders and reloading pipeline state on the fly
Rust
10
star
15

fluent

Java 8 common library for fluent coding
Java
6
star
16

mtd-vat-cli

CLI tool to query & submit UK VAT returns
Rust
5
star
17

dynamodb-lease

Dynamodb distributed lock client for Rust
Rust
5
star
18

old-school-gfx-glutin-ext

Extensions for glutin to initialize & update old school gfx
Rust
4
star
19

apart-core

Linux util for partition cloning GUIs
Rust
3
star
20

cargo-ab-lint

CLI with extra lints for rust projects
Rust
2
star
21

nix-user

Scripts and info for using nix packages without root access.
Shell
1
star
22

rusttmp

Rust issue reproductions
Rust
1
star