Furtuer optimize data structure sizes.
This commit is contained in:
parent
4e5d009386
commit
85fcb74be9
@ -658,7 +658,7 @@ impl AST {
|
||||
pub(crate) fn iter_fn_def(&self) -> impl Iterator<Item = &ScriptFnDef> {
|
||||
self.functions
|
||||
.iter_script_fn()
|
||||
.map(|(_, _, _, _, fn_def)| fn_def)
|
||||
.map(|(_, _, _, _, fn_def)| fn_def.as_ref())
|
||||
}
|
||||
/// Iterate through all function definitions.
|
||||
///
|
||||
@ -668,7 +668,7 @@ impl AST {
|
||||
pub fn iter_functions<'a>(&'a self) -> impl Iterator<Item = ScriptFnMetadata> + 'a {
|
||||
self.functions
|
||||
.iter_script_fn()
|
||||
.map(|(_, _, _, _, fn_def)| fn_def.into())
|
||||
.map(|(_, _, _, _, fn_def)| fn_def.as_ref().into())
|
||||
}
|
||||
/// Clear all function definitions in the [`AST`].
|
||||
///
|
||||
|
@ -570,9 +570,9 @@ impl CallableFunction {
|
||||
/// Panics if the [`CallableFunction`] is not [`Pure`][CallableFunction::Pure] or
|
||||
/// [`Method`][CallableFunction::Method].
|
||||
#[inline(always)]
|
||||
pub fn get_native_fn(&self) -> &FnAny {
|
||||
pub fn get_native_fn(&self) -> &Shared<FnAny> {
|
||||
match self {
|
||||
Self::Pure(f) | Self::Method(f) => f.as_ref(),
|
||||
Self::Pure(f) | Self::Method(f) => f,
|
||||
Self::Iterator(_) | Self::Plugin(_) => panic!("function should be native"),
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
@ -586,12 +586,12 @@ impl CallableFunction {
|
||||
/// Panics if the [`CallableFunction`] is not [`Script`][CallableFunction::Script].
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[inline(always)]
|
||||
pub fn get_fn_def(&self) -> &crate::ast::ScriptFnDef {
|
||||
pub fn get_fn_def(&self) -> &Shared<crate::ast::ScriptFnDef> {
|
||||
match self {
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => {
|
||||
panic!("function should be scripted")
|
||||
}
|
||||
Self::Script(f) => f.as_ref(),
|
||||
Self::Script(f) => f,
|
||||
}
|
||||
}
|
||||
/// Get a reference to an iterator function.
|
||||
@ -617,9 +617,9 @@ impl CallableFunction {
|
||||
///
|
||||
/// Panics if the [`CallableFunction`] is not [`Plugin`][CallableFunction::Plugin].
|
||||
#[inline(always)]
|
||||
pub fn get_plugin_fn<'s>(&'s self) -> &FnPlugin {
|
||||
pub fn get_plugin_fn<'s>(&'s self) -> &Shared<FnPlugin> {
|
||||
match self {
|
||||
Self::Plugin(f) => f.as_ref(),
|
||||
Self::Plugin(f) => f,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => {
|
||||
panic!("function should a plugin")
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ pub struct Module {
|
||||
/// Flattened collection of all [`Module`] variables, including those in sub-modules.
|
||||
all_variables: HashMap<u64, Dynamic, StraightHasherBuilder>,
|
||||
/// External Rust functions.
|
||||
functions: HashMap<u64, FuncInfo, StraightHasherBuilder>,
|
||||
functions: HashMap<u64, Box<FuncInfo>, StraightHasherBuilder>,
|
||||
/// Flattened collection of all external Rust functions, native or scripted.
|
||||
/// including those in sub-modules.
|
||||
all_functions: HashMap<u64, CallableFunction, StraightHasherBuilder>,
|
||||
@ -207,7 +207,7 @@ impl fmt::Debug for Module {
|
||||
" functions: {}\n",
|
||||
self.functions
|
||||
.values()
|
||||
.map(|FuncInfo { func, .. }| func.to_string())
|
||||
.map(|f| f.func.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
@ -384,11 +384,11 @@ impl Module {
|
||||
pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
|
||||
self.functions
|
||||
.values()
|
||||
.filter(|FuncInfo { access, .. }| match access {
|
||||
.filter(|f| match f.access {
|
||||
FnAccess::Public => true,
|
||||
FnAccess::Private => false,
|
||||
})
|
||||
.map(FuncInfo::gen_signature)
|
||||
.map(|f| f.gen_signature())
|
||||
}
|
||||
|
||||
/// Does a variable exist in the [`Module`]?
|
||||
@ -490,7 +490,7 @@ impl Module {
|
||||
param_names.push("Dynamic".into());
|
||||
self.functions.insert(
|
||||
hash_script,
|
||||
FuncInfo {
|
||||
Box::new(FuncInfo {
|
||||
name: fn_def.name.to_string(),
|
||||
namespace: FnNamespace::Internal,
|
||||
access: fn_def.access,
|
||||
@ -498,14 +498,15 @@ impl Module {
|
||||
param_types: Default::default(),
|
||||
param_names,
|
||||
func: fn_def.into(),
|
||||
},
|
||||
}),
|
||||
);
|
||||
self.indexed = false;
|
||||
self.contains_indexed_global_functions = false;
|
||||
hash_script
|
||||
}
|
||||
|
||||
/// Get a script-defined function in the [`Module`] based on name and number of parameters.
|
||||
/// Get a shared reference to the script-defined function in the [`Module`] based on name
|
||||
/// and number of parameters.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[inline(always)]
|
||||
pub fn get_script_fn(
|
||||
@ -513,22 +514,15 @@ impl Module {
|
||||
name: &str,
|
||||
num_params: usize,
|
||||
public_only: bool,
|
||||
) -> Option<&crate::ast::ScriptFnDef> {
|
||||
) -> Option<&Shared<crate::ast::ScriptFnDef>> {
|
||||
self.functions
|
||||
.values()
|
||||
.find(
|
||||
|FuncInfo {
|
||||
name: fn_name,
|
||||
access,
|
||||
params,
|
||||
..
|
||||
}| {
|
||||
(!public_only || *access == FnAccess::Public)
|
||||
&& *params == num_params
|
||||
&& fn_name == name
|
||||
},
|
||||
)
|
||||
.map(|FuncInfo { func, .. }| func.get_fn_def())
|
||||
.find(|f| {
|
||||
(!public_only || f.access == FnAccess::Public)
|
||||
&& f.params == num_params
|
||||
&& f.name == name
|
||||
})
|
||||
.map(|f| f.func.get_fn_def())
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the underlying [`HashMap`] of sub-modules.
|
||||
@ -629,7 +623,7 @@ impl Module {
|
||||
if public_only {
|
||||
self.functions
|
||||
.get(&hash_fn)
|
||||
.map_or(false, |FuncInfo { access, .. }| match access {
|
||||
.map_or(false, |f| match f.access {
|
||||
FnAccess::Public => true,
|
||||
FnAccess::Private => false,
|
||||
})
|
||||
@ -717,7 +711,7 @@ impl Module {
|
||||
|
||||
self.functions.insert(
|
||||
hash_fn,
|
||||
FuncInfo {
|
||||
Box::new(FuncInfo {
|
||||
name,
|
||||
namespace,
|
||||
access,
|
||||
@ -729,7 +723,7 @@ impl Module {
|
||||
Default::default()
|
||||
},
|
||||
func: func.into(),
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
self.indexed = false;
|
||||
@ -1478,11 +1472,9 @@ impl Module {
|
||||
/// The [`u64`] hash is returned by the `set_fn_XXX` calls.
|
||||
#[inline(always)]
|
||||
pub(crate) fn get_fn(&self, hash_fn: u64, public_only: bool) -> Option<&CallableFunction> {
|
||||
self.functions
|
||||
.get(&hash_fn)
|
||||
.and_then(|FuncInfo { access, func, .. }| match access {
|
||||
_ if !public_only => Some(func),
|
||||
FnAccess::Public => Some(func),
|
||||
self.functions.get(&hash_fn).and_then(|f| match f.access {
|
||||
_ if !public_only => Some(&f.func),
|
||||
FnAccess::Public => Some(&f.func),
|
||||
FnAccess::Private => None,
|
||||
})
|
||||
}
|
||||
@ -1594,27 +1586,15 @@ impl Module {
|
||||
other
|
||||
.functions
|
||||
.iter()
|
||||
.filter(
|
||||
|(
|
||||
_,
|
||||
FuncInfo {
|
||||
namespace,
|
||||
access,
|
||||
name,
|
||||
params,
|
||||
func,
|
||||
..
|
||||
},
|
||||
)| {
|
||||
.filter(|(_, f)| {
|
||||
_filter(
|
||||
*namespace,
|
||||
*access,
|
||||
func.is_script(),
|
||||
name.as_str(),
|
||||
*params,
|
||||
)
|
||||
},
|
||||
f.namespace,
|
||||
f.access,
|
||||
f.func.is_script(),
|
||||
f.name.as_str(),
|
||||
f.params,
|
||||
)
|
||||
})
|
||||
.map(|(&k, v)| (k, v.clone())),
|
||||
);
|
||||
|
||||
@ -1634,23 +1614,13 @@ impl Module {
|
||||
&mut self,
|
||||
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
|
||||
) -> &mut Self {
|
||||
self.functions.retain(
|
||||
|_,
|
||||
FuncInfo {
|
||||
namespace,
|
||||
access,
|
||||
name,
|
||||
params,
|
||||
func,
|
||||
..
|
||||
}| {
|
||||
if func.is_script() {
|
||||
filter(*namespace, *access, name.as_str(), *params)
|
||||
self.functions.retain(|_, f| {
|
||||
if f.func.is_script() {
|
||||
filter(f.namespace, f.access, f.name.as_str(), f.params)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
self.all_functions.clear();
|
||||
self.all_variables.clear();
|
||||
@ -1686,7 +1656,7 @@ impl Module {
|
||||
#[inline(always)]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn iter_fn(&self) -> impl Iterator<Item = &FuncInfo> {
|
||||
self.functions.values()
|
||||
self.functions.values().map(Box::as_ref)
|
||||
}
|
||||
|
||||
/// Get an iterator over all script-defined functions in the [`Module`].
|
||||
@ -1701,26 +1671,27 @@ impl Module {
|
||||
#[inline(always)]
|
||||
pub(crate) fn iter_script_fn(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize, &crate::ast::ScriptFnDef)> + '_
|
||||
{
|
||||
self.functions.values().filter(|f| f.func.is_script()).map(
|
||||
|FuncInfo {
|
||||
namespace,
|
||||
access,
|
||||
name,
|
||||
params,
|
||||
func,
|
||||
..
|
||||
}| {
|
||||
) -> impl Iterator<
|
||||
Item = (
|
||||
FnNamespace,
|
||||
FnAccess,
|
||||
&str,
|
||||
usize,
|
||||
&Shared<crate::ast::ScriptFnDef>,
|
||||
),
|
||||
> + '_ {
|
||||
self.functions
|
||||
.values()
|
||||
.filter(|f| f.func.is_script())
|
||||
.map(|f| {
|
||||
(
|
||||
*namespace,
|
||||
*access,
|
||||
name.as_str(),
|
||||
*params,
|
||||
func.get_fn_def(),
|
||||
)
|
||||
},
|
||||
f.namespace,
|
||||
f.access,
|
||||
f.name.as_str(),
|
||||
f.params,
|
||||
f.func.get_fn_def(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get an iterator over all script-defined functions in the [`Module`].
|
||||
@ -1736,15 +1707,10 @@ impl Module {
|
||||
pub fn iter_script_fn_info(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize)> {
|
||||
self.functions.values().filter(|f| f.func.is_script()).map(
|
||||
|FuncInfo {
|
||||
name,
|
||||
namespace,
|
||||
access,
|
||||
params,
|
||||
..
|
||||
}| (*namespace, *access, name.as_str(), *params),
|
||||
)
|
||||
self.functions
|
||||
.values()
|
||||
.filter(|f| f.func.is_script())
|
||||
.map(|f| (f.namespace, f.access, f.name.as_str(), f.params))
|
||||
}
|
||||
|
||||
/// _(INTERNALS)_ Get an iterator over all script-defined functions in the [`Module`].
|
||||
@ -1825,14 +1791,14 @@ impl Module {
|
||||
ast.lib()
|
||||
.functions
|
||||
.values()
|
||||
.filter(|FuncInfo { access, .. }| match access {
|
||||
.filter(|f| match f.access {
|
||||
FnAccess::Public => true,
|
||||
FnAccess::Private => false,
|
||||
})
|
||||
.filter(|FuncInfo { func, .. }| func.is_script())
|
||||
.for_each(|FuncInfo { func, .. }| {
|
||||
.filter(|f| f.func.is_script())
|
||||
.for_each(|f| {
|
||||
// Encapsulate AST environment
|
||||
let mut func = func.get_fn_def().clone();
|
||||
let mut func = crate::fn_native::shared_take_or_clone(f.func.get_fn_def().clone());
|
||||
func.lib = Some(ast.shared_lib());
|
||||
func.mods = func_mods.clone();
|
||||
module.set_script_fn(func);
|
||||
@ -1891,43 +1857,33 @@ impl Module {
|
||||
});
|
||||
|
||||
// Index all Rust functions
|
||||
module.functions.iter().for_each(
|
||||
|(
|
||||
&hash,
|
||||
FuncInfo {
|
||||
name,
|
||||
namespace,
|
||||
access,
|
||||
params,
|
||||
param_types,
|
||||
func,
|
||||
..
|
||||
},
|
||||
)| {
|
||||
match namespace {
|
||||
module.functions.iter().for_each(|(&hash, f)| {
|
||||
match f.namespace {
|
||||
FnNamespace::Global => {
|
||||
// Flatten all functions with global namespace
|
||||
functions.insert(hash, func.clone());
|
||||
functions.insert(hash, f.func.clone());
|
||||
contains_indexed_global_functions = true;
|
||||
}
|
||||
FnNamespace::Internal => (),
|
||||
}
|
||||
match access {
|
||||
match f.access {
|
||||
FnAccess::Public => (),
|
||||
FnAccess::Private => return, // Do not index private functions
|
||||
}
|
||||
|
||||
if !func.is_script() {
|
||||
let hash_qualified_fn =
|
||||
calc_native_fn_hash(qualifiers.iter().cloned(), name, param_types);
|
||||
functions.insert(hash_qualified_fn, func.clone());
|
||||
if !f.func.is_script() {
|
||||
let hash_qualified_fn = calc_native_fn_hash(
|
||||
qualifiers.iter().cloned(),
|
||||
f.name.as_str(),
|
||||
&f.param_types,
|
||||
);
|
||||
functions.insert(hash_qualified_fn, f.func.clone());
|
||||
} else if cfg!(not(feature = "no_function")) {
|
||||
let hash_qualified_script =
|
||||
crate::calc_fn_hash(qualifiers.iter().cloned(), name, *params);
|
||||
functions.insert(hash_qualified_script, func.clone());
|
||||
crate::calc_fn_hash(qualifiers.iter().cloned(), f.name.as_str(), f.params);
|
||||
functions.insert(hash_qualified_script, f.func.clone());
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
contains_indexed_global_functions
|
||||
}
|
||||
|
@ -849,7 +849,7 @@ pub fn optimize_into_ast(
|
||||
engine: &Engine,
|
||||
scope: &Scope,
|
||||
mut statements: Vec<Stmt>,
|
||||
_functions: Vec<crate::ast::ScriptFnDef>,
|
||||
_functions: Vec<crate::Shared<crate::ast::ScriptFnDef>>,
|
||||
optimization_level: OptimizationLevel,
|
||||
) -> AST {
|
||||
let level = if cfg!(feature = "no_optimize") {
|
||||
@ -888,7 +888,9 @@ pub fn optimize_into_ast(
|
||||
|
||||
_functions
|
||||
.into_iter()
|
||||
.map(|mut fn_def| {
|
||||
.map(|fn_def| {
|
||||
let mut fn_def = crate::fn_native::shared_take_or_clone(fn_def);
|
||||
|
||||
let pos = fn_def.body.pos;
|
||||
|
||||
let mut body = fn_def.body.statements.into_vec();
|
||||
|
@ -26,7 +26,7 @@ use crate::token::{is_keyword_function, is_valid_identifier, Token, TokenStream}
|
||||
use crate::utils::{get_hasher, StraightHasherBuilder};
|
||||
use crate::{
|
||||
calc_fn_hash, Dynamic, Engine, ImmutableString, LexError, ParseError, ParseErrorType, Position,
|
||||
Scope, StaticVec, AST,
|
||||
Scope, Shared, StaticVec, AST,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
@ -37,7 +37,7 @@ use crate::FnAccess;
|
||||
|
||||
type PERR = ParseErrorType;
|
||||
|
||||
type FunctionsLib = HashMap<u64, ScriptFnDef, StraightHasherBuilder>;
|
||||
type FunctionsLib = HashMap<u64, Shared<ScriptFnDef>, StraightHasherBuilder>;
|
||||
|
||||
/// A type that encapsulates the current state of the parser.
|
||||
#[derive(Debug)]
|
||||
@ -1008,7 +1008,7 @@ fn parse_primary(
|
||||
});
|
||||
|
||||
let hash_script = calc_fn_hash(empty(), &func.name, func.params.len());
|
||||
lib.insert(hash_script, func);
|
||||
lib.insert(hash_script, func.into());
|
||||
|
||||
expr
|
||||
}
|
||||
@ -2530,7 +2530,7 @@ fn parse_stmt(
|
||||
.into_err(pos));
|
||||
}
|
||||
|
||||
lib.insert(hash, func);
|
||||
lib.insert(hash, func.into());
|
||||
|
||||
Ok(Stmt::Noop(pos))
|
||||
}
|
||||
@ -2969,7 +2969,7 @@ impl Engine {
|
||||
fn parse_global_level(
|
||||
&self,
|
||||
input: &mut TokenStream,
|
||||
) -> Result<(Vec<Stmt>, Vec<ScriptFnDef>), ParseError> {
|
||||
) -> Result<(Vec<Stmt>, Vec<Shared<ScriptFnDef>>), ParseError> {
|
||||
let mut statements = Vec::with_capacity(16);
|
||||
let mut functions = HashMap::with_capacity_and_hasher(16, StraightHasherBuilder);
|
||||
let mut state = ParseState::new(
|
||||
|
Loading…
Reference in New Issue
Block a user