Reduce data sizes.

This commit is contained in:
Stephen Chung
2022-11-24 20:35:56 +08:00
parent 6c06481457
commit bda8c2b636
14 changed files with 264 additions and 191 deletions

View File

@@ -663,9 +663,10 @@ impl Engine {
let reset =
self.run_debugger_with_reset(global, caches, scope, this_ptr, rhs)?;
#[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset)
});
let global =
&mut *RestoreOnDrop::lock_if(reset.is_some(), global, move |g| {
g.debugger_mut().reset_status(reset)
});
let crate::ast::FnCallExpr {
name, hashes, args, ..
@@ -832,9 +833,11 @@ impl Engine {
global, caches, scope, this_ptr, _node,
)?;
#[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset)
});
let global = &mut *RestoreOnDrop::lock_if(
reset.is_some(),
global,
move |g| g.debugger_mut().reset_status(reset),
);
let crate::ast::FnCallExpr {
name, hashes, args, ..
@@ -956,9 +959,11 @@ impl Engine {
global, caches, scope, this_ptr, _node,
)?;
#[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset)
});
let global = &mut *RestoreOnDrop::lock_if(
reset.is_some(),
global,
move |g| g.debugger_mut().reset_status(reset),
);
let crate::ast::FnCallExpr {
name, hashes, args, ..

View File

@@ -419,7 +419,7 @@ impl Engine {
if let Some(cmd) =
self.run_debugger_with_reset_raw(global, caches, scope, this_ptr, node)?
{
global.debugger.status = cmd;
global.debugger_mut().status = cmd;
}
}
@@ -469,24 +469,28 @@ impl Engine {
_ => (),
}
let event = match global.debugger.status {
DebuggerStatus::Init => Some(DebuggerEvent::Start),
DebuggerStatus::NEXT if node.is_stmt() => Some(DebuggerEvent::Step),
DebuggerStatus::INTO if node.is_expr() => Some(DebuggerEvent::Step),
DebuggerStatus::STEP => Some(DebuggerEvent::Step),
DebuggerStatus::Terminate => Some(DebuggerEvent::End),
_ => None,
};
if let Some(ref dbg) = global.debugger {
let event = match dbg.status {
DebuggerStatus::Init => Some(DebuggerEvent::Start),
DebuggerStatus::NEXT if node.is_stmt() => Some(DebuggerEvent::Step),
DebuggerStatus::INTO if node.is_expr() => Some(DebuggerEvent::Step),
DebuggerStatus::STEP => Some(DebuggerEvent::Step),
DebuggerStatus::Terminate => Some(DebuggerEvent::End),
_ => None,
};
let event = match event {
Some(e) => e,
None => match global.debugger.is_break_point(global.source(), node) {
Some(bp) => DebuggerEvent::BreakPoint(bp),
None => return Ok(None),
},
};
let event = match event {
Some(e) => e,
None => match dbg.is_break_point(global.source(), node) {
Some(bp) => DebuggerEvent::BreakPoint(bp),
None => return Ok(None),
},
};
self.run_debugger_raw(global, caches, scope, this_ptr, node, event)
self.run_debugger_raw(global, caches, scope, this_ptr, node, event)
} else {
Ok(None)
}
}
/// Run the debugger callback unconditionally.
///
@@ -513,19 +517,19 @@ impl Engine {
match command {
DebuggerCommand::Continue => {
global.debugger.status = DebuggerStatus::CONTINUE;
global.debugger_mut().status = DebuggerStatus::CONTINUE;
Ok(None)
}
DebuggerCommand::Next => {
global.debugger.status = DebuggerStatus::CONTINUE;
global.debugger_mut().status = DebuggerStatus::CONTINUE;
Ok(Some(DebuggerStatus::NEXT))
}
DebuggerCommand::StepOver => {
global.debugger.status = DebuggerStatus::CONTINUE;
global.debugger_mut().status = DebuggerStatus::CONTINUE;
Ok(Some(DebuggerStatus::STEP))
}
DebuggerCommand::StepInto => {
global.debugger.status = DebuggerStatus::STEP;
global.debugger_mut().status = DebuggerStatus::STEP;
Ok(None)
}
DebuggerCommand::FunctionExit => {
@@ -539,7 +543,7 @@ impl Engine {
}
_ => global.level,
};
global.debugger.status = DebuggerStatus::FunctionExit(level);
global.debugger_mut().status = DebuggerStatus::FunctionExit(level);
Ok(None)
}
}

View File

@@ -227,9 +227,10 @@ impl Engine {
#[cfg(feature = "debugging")]
let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, expr)?;
#[cfg(feature = "debugging")]
let global = &mut *crate::types::RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset)
});
let global =
&mut *crate::types::RestoreOnDrop::lock_if(reset.is_some(), global, move |g| {
g.debugger_mut().reset_status(reset)
});
self.track_operation(global, expr.position())?;
@@ -260,9 +261,10 @@ impl Engine {
#[cfg(feature = "debugging")]
let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, expr)?;
#[cfg(feature = "debugging")]
let global = &mut *crate::types::RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset)
});
let global =
&mut *crate::types::RestoreOnDrop::lock_if(reset.is_some(), global, move |g| {
g.debugger_mut().reset_status(reset)
});
self.track_operation(global, expr.position())?;

View File

@@ -25,10 +25,11 @@ pub type GlobalConstants =
pub struct GlobalRuntimeState {
/// Names of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
imports: crate::StaticVec<ImmutableString>,
imports: Option<Box<crate::StaticVec<ImmutableString>>>,
/// Stack of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec<crate::SharedModule>,
modules: Option<Box<crate::StaticVec<crate::SharedModule>>>,
/// The current stack of loaded [modules][crate::Module] containing script-defined functions.
#[cfg(not(feature = "no_function"))]
pub lib: crate::StaticVec<crate::SharedModule>,
@@ -74,7 +75,7 @@ pub struct GlobalRuntimeState {
pub tag: Dynamic,
/// Debugging interface.
#[cfg(feature = "debugging")]
pub debugger: super::Debugger,
pub(crate) debugger: Option<super::Debugger>,
}
impl GlobalRuntimeState {
@@ -84,9 +85,9 @@ impl GlobalRuntimeState {
pub fn new(engine: &Engine) -> Self {
Self {
#[cfg(not(feature = "no_module"))]
imports: crate::StaticVec::new_const(),
imports: None,
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec::new_const(),
modules: None,
#[cfg(not(feature = "no_function"))]
lib: crate::StaticVec::new_const(),
source: None,
@@ -110,36 +111,28 @@ impl GlobalRuntimeState {
tag: engine.default_tag().clone(),
#[cfg(feature = "debugging")]
debugger: crate::eval::Debugger::new(
if engine.debugger.is_some() {
crate::eval::DebuggerStatus::Init
} else {
crate::eval::DebuggerStatus::CONTINUE
},
match engine.debugger {
Some((ref init, ..)) => init(engine),
None => Dynamic::UNIT,
},
),
debugger: engine.debugger.as_ref().map(|(init, ..)| {
crate::eval::Debugger::new(crate::eval::DebuggerStatus::Init, init(engine))
}),
}
}
/// Get the length of the stack of globally-imported [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[inline]
#[must_use]
pub fn num_imports(&self) -> usize {
self.modules.len()
self.modules.as_ref().map_or(0, |m| m.len())
}
/// Get the globally-imported [module][crate::Module] at a particular index.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[inline]
#[must_use]
pub fn get_shared_import(&self, index: usize) -> Option<crate::SharedModule> {
self.modules.get(index).cloned()
self.modules.as_ref().and_then(|m| m.get(index).cloned())
}
/// Get a mutable reference to the globally-imported [module][crate::Module] at a
/// particular index.
@@ -147,13 +140,13 @@ impl GlobalRuntimeState {
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline(always)]
#[inline]
#[must_use]
pub(crate) fn get_shared_import_mut(
&mut self,
index: usize,
) -> Option<&mut crate::SharedModule> {
self.modules.get_mut(index)
self.modules.as_mut().and_then(|m| m.get_mut(index))
}
/// Get the index of a globally-imported [module][crate::Module] by name.
///
@@ -162,33 +155,44 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn find_import(&self, name: &str) -> Option<usize> {
self.imports
.iter()
.rev()
.position(|key| key.as_str() == name)
.map(|i| self.imports.len() - 1 - i)
self.imports.as_ref().and_then(|imports| {
imports
.iter()
.rev()
.position(|key| key.as_str() == name)
.map(|i| imports.len() - 1 - i)
})
}
/// Push an imported [module][crate::Module] onto the stack.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[inline]
pub fn push_import(
&mut self,
name: impl Into<ImmutableString>,
module: impl Into<crate::SharedModule>,
) {
self.imports.push(name.into());
self.modules.push(module.into());
if self.imports.is_none() {
self.imports = Some(crate::StaticVec::new_const().into());
self.modules = Some(crate::StaticVec::new_const().into());
}
self.imports.as_mut().unwrap().push(name.into());
self.modules.as_mut().unwrap().push(module.into());
}
/// Truncate the stack of globally-imported [modules][crate::Module] to a particular length.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[inline]
pub fn truncate_imports(&mut self, size: usize) {
self.imports.truncate(size);
self.modules.truncate(size);
if size == 0 {
self.imports = None;
self.modules = None;
} else if self.imports.is_some() {
self.imports.as_mut().unwrap().truncate(size);
self.modules.as_mut().unwrap().truncate(size);
}
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
///
@@ -198,8 +202,9 @@ impl GlobalRuntimeState {
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
self.imports
.iter()
.zip(self.modules.iter())
.flat_map(|x| x.iter())
.rev()
.zip(self.modules.iter().flat_map(|x| x.iter()).rev())
.map(|(name, module)| (name.as_str(), &**module))
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
@@ -210,7 +215,11 @@ impl GlobalRuntimeState {
pub(crate) fn iter_imports_raw(
&self,
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports.iter().zip(self.modules.iter()).rev()
self.imports
.iter()
.flat_map(|x| x.iter())
.rev()
.zip(self.modules.iter().flat_map(|x| x.iter()).rev())
}
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
///
@@ -220,18 +229,21 @@ impl GlobalRuntimeState {
pub fn scan_imports_raw(
&self,
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports.iter().zip(self.modules.iter())
self.imports
.iter()
.flat_map(|x| x.iter())
.zip(self.modules.iter().flat_map(|x| x.iter()))
}
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
/// globally-imported [modules][crate::Module]?
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[inline]
pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool {
self.modules
.iter()
.any(|m| m.may_contain_dynamic_fn(hash_script))
self.modules.as_ref().map_or(false, |m| {
m.iter().any(|m| m.may_contain_dynamic_fn(hash_script))
})
}
/// Does the specified function hash key exist in the stack of globally-imported
/// [modules][crate::Module]?
@@ -242,7 +254,9 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
self.modules.iter().any(|m| m.contains_qualified_fn(hash))
self.modules
.as_ref()
.map_or(false, |m| m.iter().any(|m| m.contains_qualified_fn(hash)))
}
/// Get the specified function via its hash key from the stack of globally-imported
/// [modules][crate::Module].
@@ -255,10 +269,11 @@ impl GlobalRuntimeState {
&self,
hash: u64,
) -> Option<(&crate::func::CallableFunction, Option<&ImmutableString>)> {
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
self.modules.as_ref().and_then(|m| {
m.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
})
}
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in the stack of
/// globally-imported [modules][crate::Module]?
@@ -269,7 +284,9 @@ impl GlobalRuntimeState {
#[inline]
#[must_use]
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
self.modules.iter().any(|m| m.contains_qualified_iter(id))
self.modules
.as_ref()
.map_or(false, |m| m.iter().any(|m| m.contains_qualified_iter(id)))
}
/// Get the specified [`TypeId`][std::any::TypeId] iterator from the stack of globally-imported
/// [modules][crate::Module].
@@ -280,9 +297,8 @@ impl GlobalRuntimeState {
#[must_use]
pub fn get_iter(&self, id: std::any::TypeId) -> Option<&crate::func::IteratorFn> {
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_iter(id))
.as_ref()
.and_then(|m| m.iter().rev().find_map(|m| m.get_qualified_iter(id)))
}
/// Get the current source.
#[inline(always)]
@@ -311,15 +327,42 @@ impl GlobalRuntimeState {
pub(crate) fn hash_idx_set(&mut self) -> u64 {
self.fn_hash_indexing.1
}
/// Return a reference to the debugging interface.
///
/// # Panics
///
/// Panics if the debugging interface is not set.
#[cfg(feature = "debugging")]
pub fn debugger(&self) -> &super::Debugger {
self.debugger.as_ref().unwrap()
}
/// Return a mutable reference to the debugging interface.
///
/// # Panics
///
/// Panics if the debugging interface is not set.
#[cfg(feature = "debugging")]
pub fn debugger_mut(&mut self) -> &mut super::Debugger {
self.debugger.as_mut().unwrap()
}
}
#[cfg(not(feature = "no_module"))]
impl<K: Into<ImmutableString>, M: Into<crate::SharedModule>> Extend<(K, M)> for GlobalRuntimeState {
#[inline]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
if self.imports.is_none() {
self.imports = Some(crate::StaticVec::new_const().into());
self.modules = Some(crate::StaticVec::new_const().into());
}
let imports = self.imports.as_mut().unwrap();
let modules = self.modules.as_mut().unwrap();
for (k, m) in iter {
self.imports.push(k.into());
self.modules.push(m.into());
imports.push(k.into());
modules.push(m.into());
}
}
}

View File

@@ -194,7 +194,9 @@ impl Engine {
#[cfg(feature = "debugging")]
let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, stmt)?;
#[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::lock(global, move |g| g.debugger.reset_status(reset));
let global = &mut *RestoreOnDrop::lock_if(reset.is_some(), global, move |g| {
g.debugger_mut().reset_status(reset)
});
// Coded this way for better branch prediction.
// Popular branches are lifted out of the `match` statement into their own branches.