Skip to content

Commit 506a976

Browse files
committed
Add ability to show time when running scripts
1 parent 803b5b9 commit 506a976

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

crates/rune/src/cli/run.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub(super) struct Flags {
1414
/// Provide detailed tracing for each instruction executed.
1515
#[arg(short, long)]
1616
trace: bool,
17+
/// Time how long the script took to execute.
18+
#[arg(long)]
19+
time: bool,
1720
/// Perform a default dump.
1821
#[arg(short, long)]
1922
dump: bool,
@@ -228,18 +231,16 @@ pub(super) async fn run(
228231

229232
let errored = match result {
230233
VmResult::Ok(result) => {
231-
let duration = Instant::now().duration_since(last);
232-
233-
if c.verbose {
234+
if c.verbose || args.time {
235+
let duration = Instant::now().saturating_duration_since(last);
234236
writeln!(io.stderr, "== {:?} ({:?})", result, duration)?;
235237
}
236238

237239
None
238240
}
239241
VmResult::Err(error) => {
240-
let duration = Instant::now().duration_since(last);
241-
242-
if c.verbose {
242+
if c.verbose || args.time {
243+
let duration = Instant::now().saturating_duration_since(last);
243244
writeln!(io.stderr, "== ! ({}) ({:?})", error, duration)?;
244245
}
245246

crates/rune/src/runtime/object.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use core::fmt;
55
use core::hash;
66
use core::iter;
77

8+
use crate::alloc::hashbrown::raw::RawIter;
89
use crate::alloc::prelude::*;
910
use crate::alloc::{self, String};
10-
use crate::alloc::{btree_map, BTreeMap};
11+
use crate::alloc::{hash_map, HashMap};
1112

1213
use crate as rune;
1314
use crate::compile::ItemBuf;
@@ -21,7 +22,7 @@ use crate::Any;
2122
///
2223
/// [`into_iter`]: struct.Object.html#method.into_iter
2324
/// [`Object`]: struct.Object.html
24-
pub type IntoIter = btree_map::IntoIter<String, Value>;
25+
pub type IntoIter = hash_map::IntoIter<String, Value>;
2526

2627
/// A mutable iterator over the entries of a `Object`.
2728
///
@@ -30,7 +31,7 @@ pub type IntoIter = btree_map::IntoIter<String, Value>;
3031
///
3132
/// [`iter_mut`]: struct.Object.html#method.iter_mut
3233
/// [`Object`]: struct.Object.html
33-
pub type IterMut<'a> = btree_map::IterMut<'a, String, Value>;
34+
pub type IterMut<'a> = hash_map::IterMut<'a, String, Value>;
3435

3536
/// An iterator over the entries of a `Object`.
3637
///
@@ -39,7 +40,7 @@ pub type IterMut<'a> = btree_map::IterMut<'a, String, Value>;
3940
///
4041
/// [`iter`]: struct.Object.html#method.iter
4142
/// [`Object`]: struct.Object.html
42-
pub type Iter<'a> = btree_map::Iter<'a, String, Value>;
43+
pub type Iter<'a> = hash_map::Iter<'a, String, Value>;
4344

4445
/// An iterator over the keys of a `HashMap`.
4546
///
@@ -48,7 +49,7 @@ pub type Iter<'a> = btree_map::Iter<'a, String, Value>;
4849
///
4950
/// [`keys`]: struct.Object.html#method.keys
5051
/// [`Object`]: struct.Object.html
51-
pub type Keys<'a> = btree_map::Keys<'a, String, Value>;
52+
pub type Keys<'a> = hash_map::Keys<'a, String, Value>;
5253

5354
/// An iterator over the values of a `HashMap`.
5455
///
@@ -57,7 +58,7 @@ pub type Keys<'a> = btree_map::Keys<'a, String, Value>;
5758
///
5859
/// [`values`]: struct.Object.html#method.values
5960
/// [`Object`]: struct.Object.html
60-
pub type Values<'a> = btree_map::Values<'a, String, Value>;
61+
pub type Values<'a> = hash_map::Values<'a, String, Value>;
6162

6263
/// Struct representing a dynamic anonymous object.
6364
///
@@ -82,7 +83,7 @@ pub type Values<'a> = btree_map::Values<'a, String, Value>;
8283
#[repr(transparent)]
8384
#[rune(builtin, static_type = OBJECT_TYPE)]
8485
pub struct Object {
85-
inner: BTreeMap<String, Value>,
86+
inner: HashMap<String, Value>,
8687
}
8788

8889
impl Object {
@@ -98,7 +99,7 @@ impl Object {
9899
#[rune::function(keep, path = Self::new)]
99100
pub fn new() -> Self {
100101
Self {
101-
inner: BTreeMap::new(),
102+
inner: HashMap::new(),
102103
}
103104
}
104105

@@ -117,11 +118,11 @@ impl Object {
117118
}
118119

119120
/// Construct a new object with the given capacity.
120-
pub fn with_capacity(#[allow(unused)] capacity: usize) -> alloc::Result<Self> {
121+
pub fn with_capacity(capacity: usize) -> alloc::Result<Self> {
121122
// BTreeMap doesn't support setting capacity on creation but we keep
122123
// this here in case we want to switch store later.
123124
Ok(Self {
124-
inner: BTreeMap::new(),
125+
inner: HashMap::try_with_capacity(capacity)?,
125126
})
126127
}
127128

@@ -248,7 +249,7 @@ impl Object {
248249
///
249250
/// If the map did not have this key present, `None` is returned.
250251
pub fn insert(&mut self, k: String, v: Value) -> alloc::Result<Option<Value>> {
251-
Ok(self.inner.try_insert(k, v)?)
252+
self.inner.try_insert(k, v)
252253
}
253254

254255
/// Clears the object, removing all key-value pairs. Keeps the allocated
@@ -260,7 +261,7 @@ impl Object {
260261
}
261262

262263
/// Convert into inner.
263-
pub fn into_inner(self) -> BTreeMap<String, Value> {
264+
pub fn into_inner(self) -> HashMap<String, Value> {
264265
self.inner
265266
}
266267

@@ -308,30 +309,31 @@ impl Object {
308309
#[rune::function(keep, path = Self::iter)]
309310
pub fn rune_iter(this: Ref<Self>) -> Iterator {
310311
struct Iter {
311-
iter: btree_map::IterRaw<String, Value>,
312+
iter: RawIter<(String, Value)>,
312313
_guard: RawRef,
313314
}
314315

315316
impl iter::Iterator for Iter {
316317
type Item = VmResult<(String, Value)>;
317318

318319
fn next(&mut self) -> Option<Self::Item> {
319-
let (key, value) = self.iter.next()?;
320-
321320
unsafe {
322-
let key = match (*key).try_clone() {
321+
let bucket = self.iter.next()?;
322+
let (key, value) = bucket.as_ref();
323+
324+
let key = match key.try_clone() {
323325
Ok(key) => key,
324326
Err(err) => return Some(VmResult::err(err)),
325327
};
326328

327-
Some(VmResult::Ok((key, (*value).clone())))
329+
Some(VmResult::Ok((key, value.clone())))
328330
}
329331
}
330332
}
331333

332334
// SAFETY: we're holding onto the related reference guard, and making
333335
// sure that it's dropped after the iterator.
334-
let iter = unsafe { this.inner.iter_raw() };
336+
let iter = unsafe { this.inner.raw_table().iter() };
335337
let (_, _guard) = Ref::into_raw(this);
336338

337339
let iter = Iter { iter, _guard };

0 commit comments

Comments
 (0)