Streamline code.
This commit is contained in:
parent
3d0626c76f
commit
38e47c909d
@ -493,6 +493,9 @@ impl fmt::Debug for Expr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.write_str(&x.3)?;
|
f.write_str(&x.3)?;
|
||||||
|
if let Some(n) = x.1.index() {
|
||||||
|
write!(f, " #{}", n)?;
|
||||||
|
}
|
||||||
if let Some(n) = i.map_or_else(|| x.0, |n| NonZeroUsize::new(n.get() as usize)) {
|
if let Some(n) = i.map_or_else(|| x.0, |n| NonZeroUsize::new(n.get() as usize)) {
|
||||||
write!(f, " #{}", n)?;
|
write!(f, " #{}", n)?;
|
||||||
}
|
}
|
||||||
|
@ -302,6 +302,10 @@ pub struct TryCatchBlock {
|
|||||||
pub catch_block: StmtBlock,
|
pub catch_block: StmtBlock,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Number of items to keep inline for [`StmtBlockContainer`].
|
||||||
|
#[cfg(not(feature = "no_std"))]
|
||||||
|
const STMT_BLOCK_INLINE_SIZE: usize = 8;
|
||||||
|
|
||||||
/// _(internals)_ The underlying container type for [`StmtBlock`].
|
/// _(internals)_ The underlying container type for [`StmtBlock`].
|
||||||
/// Exported under the `internals` feature only.
|
/// Exported under the `internals` feature only.
|
||||||
///
|
///
|
||||||
@ -309,7 +313,7 @@ pub struct TryCatchBlock {
|
|||||||
/// hold a statements block, with the assumption that most program blocks would container fewer than
|
/// hold a statements block, with the assumption that most program blocks would container fewer than
|
||||||
/// 8 statements, and those that do have a lot more statements.
|
/// 8 statements, and those that do have a lot more statements.
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
pub type StmtBlockContainer = smallvec::SmallVec<[Stmt; 8]>;
|
pub type StmtBlockContainer = smallvec::SmallVec<[Stmt; STMT_BLOCK_INLINE_SIZE]>;
|
||||||
|
|
||||||
/// _(internals)_ The underlying container type for [`StmtBlock`].
|
/// _(internals)_ The underlying container type for [`StmtBlock`].
|
||||||
/// Exported under the `internals` feature only.
|
/// Exported under the `internals` feature only.
|
||||||
@ -491,9 +495,9 @@ impl From<Stmt> for StmtBlock {
|
|||||||
impl IntoIterator for StmtBlock {
|
impl IntoIterator for StmtBlock {
|
||||||
type Item = Stmt;
|
type Item = Stmt;
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
type IntoIter = smallvec::IntoIter<[Stmt; 8]>;
|
type IntoIter = smallvec::IntoIter<[Stmt; STMT_BLOCK_INLINE_SIZE]>;
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
type IntoIter = smallvec::IntoIter<[Stmt; 3]>;
|
type IntoIter = smallvec::IntoIter<[Stmt; crate::STATIC_VEC_INLINE_SIZE]>;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
@ -24,13 +24,13 @@ impl Engine {
|
|||||||
|
|
||||||
let root = namespace.root();
|
let root = namespace.root();
|
||||||
|
|
||||||
// Qualified - check if the root module is directly indexed
|
|
||||||
let index = if global.always_search_scope {
|
let index = if global.always_search_scope {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
namespace.index()
|
namespace.index()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Qualified - check if the root module is directly indexed
|
||||||
if let Some(index) = index {
|
if let Some(index) = index {
|
||||||
let offset = global.num_imports() - index.get();
|
let offset = global.num_imports() - index.get();
|
||||||
|
|
||||||
|
13
src/lib.rs
13
src/lib.rs
@ -341,6 +341,9 @@ pub use eval::{Caches, FnResolutionCache, FnResolutionCacheEntry, GlobalRuntimeS
|
|||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
pub use api::definitions::Definitions;
|
pub use api::definitions::Definitions;
|
||||||
|
|
||||||
|
/// Number of items to keep inline for [`StaticVec`].
|
||||||
|
const STATIC_VEC_INLINE_SIZE: usize = 3;
|
||||||
|
|
||||||
/// Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec), which is a
|
/// Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec), which is a
|
||||||
/// specialized [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
|
/// specialized [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
|
||||||
///
|
///
|
||||||
@ -373,7 +376,7 @@ pub use api::definitions::Definitions;
|
|||||||
/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
|
/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
|
||||||
/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
|
/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
|
||||||
#[cfg(not(feature = "internals"))]
|
#[cfg(not(feature = "internals"))]
|
||||||
type StaticVec<T> = smallvec::SmallVec<[T; 3]>;
|
type StaticVec<T> = smallvec::SmallVec<[T; STATIC_VEC_INLINE_SIZE]>;
|
||||||
|
|
||||||
/// _(internals)_ Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec),
|
/// _(internals)_ Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec),
|
||||||
/// which is a [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
|
/// which is a [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
|
||||||
@ -408,7 +411,11 @@ type StaticVec<T> = smallvec::SmallVec<[T; 3]>;
|
|||||||
/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
|
/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
|
||||||
/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
|
/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
|
||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
pub type StaticVec<T> = smallvec::SmallVec<[T; 3]>;
|
pub type StaticVec<T> = smallvec::SmallVec<[T; STATIC_VEC_INLINE_SIZE]>;
|
||||||
|
|
||||||
|
/// Number of items to keep inline for [`FnArgsVec`].
|
||||||
|
#[cfg(not(feature = "no_closure"))]
|
||||||
|
const FN_ARGS_VEC_INLINE_SIZE: usize = 5;
|
||||||
|
|
||||||
/// Inline arguments storage for function calls.
|
/// Inline arguments storage for function calls.
|
||||||
///
|
///
|
||||||
@ -423,7 +430,7 @@ pub type StaticVec<T> = smallvec::SmallVec<[T; 3]>;
|
|||||||
///
|
///
|
||||||
/// Under `no_closure`, this type aliases to [`StaticVec`][crate::StaticVec] instead.
|
/// Under `no_closure`, this type aliases to [`StaticVec`][crate::StaticVec] instead.
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
type FnArgsVec<T> = smallvec::SmallVec<[T; 5]>;
|
type FnArgsVec<T> = smallvec::SmallVec<[T; FN_ARGS_VEC_INLINE_SIZE]>;
|
||||||
|
|
||||||
/// Inline arguments storage for function calls.
|
/// Inline arguments storage for function calls.
|
||||||
/// This type aliases to [`StaticVec`][crate::StaticVec].
|
/// This type aliases to [`StaticVec`][crate::StaticVec].
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
use crate::{Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, ERR};
|
use crate::{
|
||||||
|
Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, StaticVec, ERR,
|
||||||
|
STATIC_VEC_INLINE_SIZE,
|
||||||
|
};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
use std::{ops::AddAssign, slice::Iter, vec::IntoIter};
|
use std::{ops::AddAssign, slice::Iter};
|
||||||
|
|
||||||
/// [Module] resolution service that holds a collection of module resolvers,
|
/// [Module] resolution service that holds a collection of module resolvers,
|
||||||
/// to be searched in sequential order.
|
/// to be searched in sequential order.
|
||||||
@ -21,7 +24,7 @@ use std::{ops::AddAssign, slice::Iter, vec::IntoIter};
|
|||||||
/// engine.set_module_resolver(collection);
|
/// engine.set_module_resolver(collection);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ModuleResolversCollection(Vec<Box<dyn ModuleResolver>>);
|
pub struct ModuleResolversCollection(StaticVec<Box<dyn ModuleResolver>>);
|
||||||
|
|
||||||
impl ModuleResolversCollection {
|
impl ModuleResolversCollection {
|
||||||
/// Create a new [`ModuleResolversCollection`].
|
/// Create a new [`ModuleResolversCollection`].
|
||||||
@ -43,7 +46,7 @@ impl ModuleResolversCollection {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self(Vec::new())
|
Self(StaticVec::new_const())
|
||||||
}
|
}
|
||||||
/// Append a [module resolver][ModuleResolver] to the end.
|
/// Append a [module resolver][ModuleResolver] to the end.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
@ -109,7 +112,7 @@ impl ModuleResolversCollection {
|
|||||||
|
|
||||||
impl IntoIterator for ModuleResolversCollection {
|
impl IntoIterator for ModuleResolversCollection {
|
||||||
type Item = Box<dyn ModuleResolver>;
|
type Item = Box<dyn ModuleResolver>;
|
||||||
type IntoIter = IntoIter<Box<dyn ModuleResolver>>;
|
type IntoIter = smallvec::IntoIter<[Box<dyn ModuleResolver>; STATIC_VEC_INLINE_SIZE]>;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -110,7 +110,7 @@ impl StringsInterner<'_> {
|
|||||||
|
|
||||||
// If the interner is over capacity, remove the longest entry that has the lowest count
|
// If the interner is over capacity, remove the longest entry that has the lowest count
|
||||||
if self.cache.len() > self.capacity {
|
if self.cache.len() > self.capacity {
|
||||||
// Leave some buffer to grow when shrinking the cache.
|
// Throttle: leave some buffer to grow when shrinking the cache.
|
||||||
// We leave at least two entries, one for the empty string, and one for the string
|
// We leave at least two entries, one for the empty string, and one for the string
|
||||||
// that has just been inserted.
|
// that has just been inserted.
|
||||||
let max = if self.capacity < 5 {
|
let max = if self.capacity < 5 {
|
||||||
|
Loading…
Reference in New Issue
Block a user