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

Functions for more precise time measurement #77

Merged
merged 5 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dmsort = {version = "1.0.0", optional = true}
toml-dep = { version = "0.5.8", package="toml", optional = true }

[features]
default = ["cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "toml", "url"]
default = ["cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "time", "toml", "url"]

# default features
cellularnoise = ["rand"]
Expand All @@ -56,6 +56,7 @@ http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
json = ["serde", "serde_json"]
log = ["chrono"]
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
time = []
toml = ["serde", "serde_json", "toml-dep"]
url = ["url-dep", "percent-encoding"]

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The default features are:
* sql: Asynchronous MySQL/MariaDB client library.
* noise: 2d Perlin noise.
* toml: TOML parser.
* time: High-accuracy time measuring.

Additional features are:
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.
Expand Down
3 changes: 3 additions & 0 deletions dmsrc/time.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define rustg_time_microseconds(id) text2num(call(RUST_G, "time_microseconds")(id))
#define rustg_time_milliseconds(id) text2num(call(RUST_G, "time_milliseconds")(id))
#define rustg_time_reset(id) call(RUST_G, "time_reset")(id)
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod log;
pub mod noise_gen;
#[cfg(feature = "sql")]
pub mod sql;
#[cfg(feature = "time")]
pub mod time;
#[cfg(feature = "toml")]
pub mod toml;
#[cfg(feature = "unzip")]
Expand Down
38 changes: 38 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{
cell::RefCell,
collections::hash_map::{Entry, HashMap},
time::Instant,
};

thread_local!( static INSTANTS: RefCell<HashMap<String, Instant>> = RefCell::new(HashMap::new()) );

byond_fn! { time_microseconds(instant_id) {
INSTANTS.with(|instants| {
let mut map = instants.borrow_mut();
let instant = match map.entry(instant_id.into()) {
Entry::Occupied(elem) => elem.into_mut(),
Entry::Vacant(elem) => elem.insert(Instant::now()),
};
Some(instant.elapsed().as_micros().to_string())
})
} }

byond_fn! { time_milliseconds(instant_id) {
INSTANTS.with(|instants| {
let mut map = instants.borrow_mut();
let instant = match map.entry(instant_id.into()) {
Entry::Occupied(elem) => elem.into_mut(),
Entry::Vacant(elem) => elem.insert(Instant::now()),
};
Some(instant.elapsed().as_millis().to_string())
})
} }

byond_fn! { time_reset(instant_id) {
INSTANTS.with(|instants| {
let mut map = instants.borrow_mut();
map.insert(instant_id.into(), Instant::now());
Some("")
})
} }