Use a bloom filter.
This commit is contained in:
parent
1476b14831
commit
1bfedf516d
@ -424,11 +424,10 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut func_infos = self.iter_fn().collect::<Vec<_>>();
|
let mut func_infos = self.iter_fn().collect::<Vec<_>>();
|
||||||
func_infos.sort_by(|a, b| match a.metadata.name.cmp(&b.metadata.name) {
|
func_infos.sort_by(|a, b| match a.name.cmp(&b.name) {
|
||||||
Ordering::Equal => match a.metadata.params.cmp(&b.metadata.params) {
|
Ordering::Equal => match a.num_params.cmp(&b.num_params) {
|
||||||
Ordering::Equal => (a.metadata.params_info.join("")
|
Ordering::Equal => (a.params_info.join("") + a.return_type.as_str())
|
||||||
+ a.metadata.return_type.as_str())
|
.cmp(&(b.params_info.join("") + b.return_type.as_str())),
|
||||||
.cmp(&(b.metadata.params_info.join("") + b.metadata.return_type.as_str())),
|
|
||||||
o => o,
|
o => o,
|
||||||
},
|
},
|
||||||
o => o,
|
o => o,
|
||||||
@ -440,17 +439,16 @@ impl Module {
|
|||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
|
|
||||||
if f.metadata.access == FnAccess::Private {
|
if f.access == FnAccess::Private {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_custom_syntax"))]
|
#[cfg(not(feature = "no_custom_syntax"))]
|
||||||
let operator = def.engine.custom_keywords.contains_key(&f.metadata.name)
|
let operator = def.engine.custom_keywords.contains_key(&f.name)
|
||||||
|| (!f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name));
|
|| (!f.name.contains('$') && !is_valid_function_name(&f.name));
|
||||||
|
|
||||||
#[cfg(feature = "no_custom_syntax")]
|
#[cfg(feature = "no_custom_syntax")]
|
||||||
let operator =
|
let operator = !f.name.contains('$') && !is_valid_function_name(&f.name);
|
||||||
!f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name);
|
|
||||||
|
|
||||||
f.write_definition(writer, def, operator)?;
|
f.write_definition(writer, def, operator)?;
|
||||||
}
|
}
|
||||||
@ -467,7 +465,7 @@ impl FuncInfo {
|
|||||||
def: &Definitions,
|
def: &Definitions,
|
||||||
operator: bool,
|
operator: bool,
|
||||||
) -> fmt::Result {
|
) -> fmt::Result {
|
||||||
for comment in &*self.metadata.comments {
|
for comment in &*self.comments {
|
||||||
writeln!(writer, "{comment}")?;
|
writeln!(writer, "{comment}")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -477,33 +475,29 @@ impl FuncInfo {
|
|||||||
writer.write_str("fn ")?;
|
writer.write_str("fn ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(name) = self.metadata.name.strip_prefix("get$") {
|
if let Some(name) = self.name.strip_prefix("get$") {
|
||||||
write!(writer, "get {name}(")?;
|
write!(writer, "get {name}(")?;
|
||||||
} else if let Some(name) = self.metadata.name.strip_prefix("set$") {
|
} else if let Some(name) = self.name.strip_prefix("set$") {
|
||||||
write!(writer, "set {name}(")?;
|
write!(writer, "set {name}(")?;
|
||||||
} else {
|
} else {
|
||||||
write!(writer, "{}(", self.metadata.name)?;
|
write!(writer, "{}(", self.name)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for i in 0..self.metadata.params {
|
for i in 0..self.num_params {
|
||||||
if !first {
|
if !first {
|
||||||
writer.write_str(", ")?;
|
writer.write_str(", ")?;
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
|
|
||||||
let (param_name, param_type) =
|
let (param_name, param_type) = self.params_info.get(i).map_or(("_", "?".into()), |s| {
|
||||||
self.metadata
|
let mut s = s.splitn(2, ':');
|
||||||
.params_info
|
(
|
||||||
.get(i)
|
s.next().unwrap_or("_").split(' ').last().unwrap(),
|
||||||
.map_or(("_", "?".into()), |s| {
|
s.next()
|
||||||
let mut s = s.splitn(2, ':');
|
.map_or(Cow::Borrowed("?"), |ty| def_type_name(ty, def.engine)),
|
||||||
(
|
)
|
||||||
s.next().unwrap_or("_").split(' ').last().unwrap(),
|
});
|
||||||
s.next()
|
|
||||||
.map_or(Cow::Borrowed("?"), |ty| def_type_name(ty, def.engine)),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
if operator {
|
if operator {
|
||||||
write!(writer, "{param_type}")?;
|
write!(writer, "{param_type}")?;
|
||||||
@ -515,7 +509,7 @@ impl FuncInfo {
|
|||||||
write!(
|
write!(
|
||||||
writer,
|
writer,
|
||||||
") -> {};",
|
") -> {};",
|
||||||
def_type_name(&self.metadata.return_type, def.engine)
|
def_type_name(&self.return_type, def.engine)
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -239,21 +239,21 @@ impl Engine {
|
|||||||
|
|
||||||
// Check `Dynamic` parameters for functions with parameters
|
// Check `Dynamic` parameters for functions with parameters
|
||||||
if allow_dynamic && max_bitmask == 0 && num_args > 0 {
|
if allow_dynamic && max_bitmask == 0 && num_args > 0 {
|
||||||
let is_dynamic = lib.iter().any(|&m| m.contains_dynamic_fn(hash_base))
|
let is_dynamic = lib.iter().any(|&m| m.may_contain_dynamic_fn(hash_base))
|
||||||
|| self
|
|| self
|
||||||
.global_modules
|
.global_modules
|
||||||
.iter()
|
.iter()
|
||||||
.any(|m| m.contains_dynamic_fn(hash_base));
|
.any(|m| m.may_contain_dynamic_fn(hash_base));
|
||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
let is_dynamic = is_dynamic
|
let is_dynamic = is_dynamic
|
||||||
|| _global
|
|| _global
|
||||||
.iter_imports_raw()
|
.iter_imports_raw()
|
||||||
.any(|(_, m)| m.contains_dynamic_fn(hash_base))
|
.any(|(_, m)| m.may_contain_dynamic_fn(hash_base))
|
||||||
|| self
|
|| self
|
||||||
.global_sub_modules
|
.global_sub_modules
|
||||||
.values()
|
.values()
|
||||||
.any(|m| m.contains_dynamic_fn(hash_base));
|
.any(|m| m.may_contain_dynamic_fn(hash_base));
|
||||||
|
|
||||||
// Set maximum bitmask when there are dynamic versions of the function
|
// Set maximum bitmask when there are dynamic versions of the function
|
||||||
if is_dynamic {
|
if is_dynamic {
|
||||||
|
@ -9,13 +9,9 @@ use std::{
|
|||||||
|
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
pub type StraightHashMap<K, V> = hashbrown::HashMap<K, V, StraightHasherBuilder>;
|
pub type StraightHashMap<K, V> = hashbrown::HashMap<K, V, StraightHasherBuilder>;
|
||||||
#[cfg(feature = "no_std")]
|
|
||||||
pub type StraightHashSet<K> = hashbrown::HashSet<K, StraightHasherBuilder>;
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
pub type StraightHashMap<K, V> = std::collections::HashMap<K, V, StraightHasherBuilder>;
|
pub type StraightHashMap<K, V> = std::collections::HashMap<K, V, StraightHasherBuilder>;
|
||||||
#[cfg(not(feature = "no_std"))]
|
|
||||||
pub type StraightHashSet<K> = std::collections::HashSet<K, StraightHasherBuilder>;
|
|
||||||
|
|
||||||
/// Dummy hash value to map zeros to. This value can be anything.
|
/// Dummy hash value to map zeros to. This value can be anything.
|
||||||
///
|
///
|
||||||
|
@ -21,7 +21,7 @@ pub use callable_function::CallableFunction;
|
|||||||
pub use func::Func;
|
pub use func::Func;
|
||||||
pub use hashing::{
|
pub use hashing::{
|
||||||
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash,
|
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash,
|
||||||
combine_hashes, get_hasher, StraightHashMap, StraightHashSet,
|
combine_hashes, get_hasher, StraightHashMap,
|
||||||
};
|
};
|
||||||
pub use native::{
|
pub use native::{
|
||||||
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,
|
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,
|
||||||
|
@ -7,7 +7,7 @@ use crate::func::{
|
|||||||
shared_take_or_clone, CallableFunction, FnCallArgs, IteratorFn, RegisterNativeFunction,
|
shared_take_or_clone, CallableFunction, FnCallArgs, IteratorFn, RegisterNativeFunction,
|
||||||
SendSync,
|
SendSync,
|
||||||
};
|
};
|
||||||
use crate::types::{dynamic::Variant, CustomTypesCollection};
|
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
|
||||||
use crate::{
|
use crate::{
|
||||||
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, combine_hashes, Dynamic, Identifier,
|
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, combine_hashes, Dynamic, Identifier,
|
||||||
ImmutableString, NativeCallContext, RhaiResultOf, Shared, SmartString, StaticVec,
|
ImmutableString, NativeCallContext, RhaiResultOf, Shared, SmartString, StaticVec,
|
||||||
@ -16,7 +16,6 @@ use crate::{
|
|||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
use std::{
|
use std::{
|
||||||
any::TypeId,
|
any::TypeId,
|
||||||
cmp::Ordering,
|
|
||||||
collections::{BTreeMap, BTreeSet},
|
collections::{BTreeMap, BTreeSet},
|
||||||
fmt,
|
fmt,
|
||||||
ops::{Add, AddAssign},
|
ops::{Add, AddAssign},
|
||||||
@ -25,7 +24,7 @@ use std::{
|
|||||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
use crate::func::register::Mut;
|
use crate::func::register::Mut;
|
||||||
|
|
||||||
use crate::func::{StraightHashMap, StraightHashSet};
|
use crate::func::StraightHashMap;
|
||||||
|
|
||||||
/// A type representing the namespace of a function.
|
/// A type representing the namespace of a function.
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
@ -62,10 +61,12 @@ impl FnNamespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A type containing all metadata for a registered function.
|
/// A type containing a single registered function.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct FnMetadata {
|
pub struct FuncInfo {
|
||||||
|
/// Function instance.
|
||||||
|
pub func: CallableFunction,
|
||||||
/// Function namespace.
|
/// Function namespace.
|
||||||
pub namespace: FnNamespace,
|
pub namespace: FnNamespace,
|
||||||
/// Function access mode.
|
/// Function access mode.
|
||||||
@ -73,7 +74,9 @@ pub struct FnMetadata {
|
|||||||
/// Function name.
|
/// Function name.
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
/// Number of parameters.
|
/// Number of parameters.
|
||||||
pub params: usize,
|
pub num_params: usize,
|
||||||
|
/// Parameter types (if applicable).
|
||||||
|
pub param_types: StaticVec<TypeId>,
|
||||||
/// Parameter names and types (if available).
|
/// Parameter names and types (if available).
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
pub params_info: StaticVec<Identifier>,
|
pub params_info: StaticVec<Identifier>,
|
||||||
@ -85,58 +88,25 @@ pub struct FnMetadata {
|
|||||||
pub comments: Box<[Box<str>]>,
|
pub comments: Box<[Box<str>]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for FnMetadata {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for FnMetadata {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
match self.name.cmp(&other.name) {
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
Ordering::Equal => match self.params.cmp(&other.params) {
|
|
||||||
Ordering::Equal => self.params_info.cmp(&other.params_info),
|
|
||||||
cmp => cmp,
|
|
||||||
},
|
|
||||||
#[cfg(not(feature = "metadata"))]
|
|
||||||
Ordering::Equal => self.params.cmp(&other.params),
|
|
||||||
cmp => cmp,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A type containing a single registered function.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct FuncInfo {
|
|
||||||
/// Function instance.
|
|
||||||
pub func: CallableFunction,
|
|
||||||
/// Parameter types (if applicable).
|
|
||||||
pub param_types: StaticVec<TypeId>,
|
|
||||||
/// Function metadata.
|
|
||||||
pub metadata: FnMetadata,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FuncInfo {
|
impl FuncInfo {
|
||||||
/// _(metadata)_ Generate a signature of the function.
|
/// _(metadata)_ Generate a signature of the function.
|
||||||
/// Exported under the `metadata` feature only.
|
/// Exported under the `metadata` feature only.
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn gen_signature(&self) -> String {
|
pub fn gen_signature(&self) -> String {
|
||||||
let mut sig = format!("{}(", self.metadata.name);
|
let mut sig = format!("{}(", self.name);
|
||||||
|
|
||||||
let return_type = format_type(&self.metadata.return_type, true);
|
let return_type = format_type(&self.return_type, true);
|
||||||
|
|
||||||
if self.metadata.params_info.is_empty() {
|
if self.params_info.is_empty() {
|
||||||
for x in 0..self.metadata.params {
|
for x in 0..self.num_params {
|
||||||
sig.push('_');
|
sig.push('_');
|
||||||
if x < self.metadata.params - 1 {
|
if x < self.num_params - 1 {
|
||||||
sig.push_str(", ");
|
sig.push_str(", ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let params: StaticVec<_> = self
|
let params: StaticVec<_> = self
|
||||||
.metadata
|
|
||||||
.params_info
|
.params_info
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
@ -213,7 +183,7 @@ pub struct Module {
|
|||||||
/// including those in sub-modules.
|
/// including those in sub-modules.
|
||||||
all_functions: StraightHashMap<u64, CallableFunction>,
|
all_functions: StraightHashMap<u64, CallableFunction>,
|
||||||
/// Native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters.
|
/// Native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters.
|
||||||
dynamic_functions: StraightHashSet<u64>,
|
dynamic_functions: BloomFilterU64,
|
||||||
/// Iterator functions, keyed by the type producing the iterator.
|
/// Iterator functions, keyed by the type producing the iterator.
|
||||||
type_iterators: BTreeMap<TypeId, Shared<IteratorFn>>,
|
type_iterators: BTreeMap<TypeId, Shared<IteratorFn>>,
|
||||||
/// Flattened collection of iterator functions, including those in sub-modules.
|
/// Flattened collection of iterator functions, including those in sub-modules.
|
||||||
@ -314,7 +284,7 @@ impl Module {
|
|||||||
all_variables: StraightHashMap::default(),
|
all_variables: StraightHashMap::default(),
|
||||||
functions: StraightHashMap::default(),
|
functions: StraightHashMap::default(),
|
||||||
all_functions: StraightHashMap::default(),
|
all_functions: StraightHashMap::default(),
|
||||||
dynamic_functions: StraightHashSet::default(),
|
dynamic_functions: BloomFilterU64::new(),
|
||||||
type_iterators: BTreeMap::new(),
|
type_iterators: BTreeMap::new(),
|
||||||
all_type_iterators: BTreeMap::new(),
|
all_type_iterators: BTreeMap::new(),
|
||||||
indexed: true,
|
indexed: true,
|
||||||
@ -587,7 +557,7 @@ impl Module {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
|
pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
|
||||||
self.iter_fn()
|
self.iter_fn()
|
||||||
.filter(|&f| match f.metadata.access {
|
.filter(|&f| match f.access {
|
||||||
FnAccess::Public => true,
|
FnAccess::Public => true,
|
||||||
FnAccess::Private => false,
|
FnAccess::Private => false,
|
||||||
})
|
})
|
||||||
@ -706,20 +676,18 @@ impl Module {
|
|||||||
self.functions.insert(
|
self.functions.insert(
|
||||||
hash_script,
|
hash_script,
|
||||||
FuncInfo {
|
FuncInfo {
|
||||||
metadata: FnMetadata {
|
name: fn_def.name.as_str().into(),
|
||||||
name: fn_def.name.as_str().into(),
|
namespace: FnNamespace::Internal,
|
||||||
namespace: FnNamespace::Internal,
|
access: fn_def.access,
|
||||||
access: fn_def.access,
|
num_params,
|
||||||
params: num_params,
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
params_info,
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
return_type: "".into(),
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
comments: Box::default(),
|
|
||||||
},
|
|
||||||
func: fn_def.into(),
|
|
||||||
param_types: StaticVec::new_const(),
|
param_types: StaticVec::new_const(),
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
params_info,
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
return_type: "".into(),
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
comments: Box::default(),
|
||||||
|
func: fn_def.into(),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
@ -744,7 +712,7 @@ impl Module {
|
|||||||
let name = name.as_ref();
|
let name = name.as_ref();
|
||||||
|
|
||||||
self.iter_fn()
|
self.iter_fn()
|
||||||
.find(|f| f.metadata.params == num_params && f.metadata.name == name)
|
.find(|f| f.num_params == num_params && f.name == name)
|
||||||
.and_then(|f| f.func.get_script_fn_def())
|
.and_then(|f| f.func.get_script_fn_def())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -886,14 +854,14 @@ impl Module {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
||||||
let (param_names, return_type_name) = if param_names.len() > f.metadata.params {
|
let (param_names, return_type_name) = if param_names.len() > f.num_params {
|
||||||
let return_type = param_names.pop().unwrap();
|
let return_type = param_names.pop().unwrap();
|
||||||
(param_names, return_type)
|
(param_names, return_type)
|
||||||
} else {
|
} else {
|
||||||
(param_names, crate::SmartString::new_const())
|
(param_names, crate::SmartString::new_const())
|
||||||
};
|
};
|
||||||
f.metadata.params_info = param_names;
|
f.params_info = param_names;
|
||||||
f.metadata.return_type = return_type_name;
|
f.return_type = return_type_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -935,7 +903,7 @@ impl Module {
|
|||||||
|
|
||||||
if !comments.is_empty() {
|
if !comments.is_empty() {
|
||||||
let f = self.functions.get_mut(&hash_fn).unwrap();
|
let f = self.functions.get_mut(&hash_fn).unwrap();
|
||||||
f.metadata.comments = comments.iter().map(|s| s.as_ref().into()).collect();
|
f.comments = comments.iter().map(|s| s.as_ref().into()).collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -947,7 +915,7 @@ impl Module {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn update_fn_namespace(&mut self, hash_fn: u64, namespace: FnNamespace) -> &mut Self {
|
pub fn update_fn_namespace(&mut self, hash_fn: u64, namespace: FnNamespace) -> &mut Self {
|
||||||
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
||||||
f.metadata.namespace = namespace;
|
f.namespace = namespace;
|
||||||
self.indexed = false;
|
self.indexed = false;
|
||||||
self.contains_indexed_global_functions = false;
|
self.contains_indexed_global_functions = false;
|
||||||
}
|
}
|
||||||
@ -1036,26 +1004,24 @@ impl Module {
|
|||||||
|
|
||||||
if is_dynamic {
|
if is_dynamic {
|
||||||
self.dynamic_functions
|
self.dynamic_functions
|
||||||
.insert(calc_fn_hash(name, param_types.len()));
|
.mark(calc_fn_hash(name, param_types.len()));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.functions.insert(
|
self.functions.insert(
|
||||||
hash_fn,
|
hash_fn,
|
||||||
FuncInfo {
|
FuncInfo {
|
||||||
metadata: FnMetadata {
|
|
||||||
name: name.into(),
|
|
||||||
namespace,
|
|
||||||
access,
|
|
||||||
params: param_types.len(),
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
params_info: param_names,
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
return_type: return_type_name,
|
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
comments: Box::default(),
|
|
||||||
},
|
|
||||||
func,
|
func,
|
||||||
|
name: name.into(),
|
||||||
|
namespace,
|
||||||
|
access,
|
||||||
|
num_params: param_types.len(),
|
||||||
param_types,
|
param_types,
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
params_info: param_names,
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
return_type: return_type_name,
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
comments: Box::default(),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
@ -1110,7 +1076,7 @@ impl Module {
|
|||||||
|
|
||||||
if !comments.is_empty() {
|
if !comments.is_empty() {
|
||||||
let f = self.functions.get_mut(&hash).unwrap();
|
let f = self.functions.get_mut(&hash).unwrap();
|
||||||
f.metadata.comments = comments.iter().map(|s| s.as_ref().into()).collect();
|
f.comments = comments.iter().map(|s| s.as_ref().into()).collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
hash
|
hash
|
||||||
@ -1554,15 +1520,13 @@ impl Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Does the particular function with [`Dynamic`] parameter(s) exist in the [`Module`]?
|
/// Can the particular function with [`Dynamic`] parameter(s) exist in the [`Module`]?
|
||||||
|
///
|
||||||
|
/// A `true` return value does not automatically imply that the function _must_ exist.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub(crate) fn contains_dynamic_fn(&self, hash_script: u64) -> bool {
|
pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool {
|
||||||
if self.dynamic_functions.is_empty() {
|
!self.dynamic_functions.is_absent(hash_script)
|
||||||
false
|
|
||||||
} else {
|
|
||||||
self.dynamic_functions.contains(&hash_script)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Does the particular namespace-qualified function exist in the [`Module`]?
|
/// Does the particular namespace-qualified function exist in the [`Module`]?
|
||||||
@ -1599,8 +1563,7 @@ impl Module {
|
|||||||
self.modules.extend(other.modules.into_iter());
|
self.modules.extend(other.modules.into_iter());
|
||||||
self.variables.extend(other.variables.into_iter());
|
self.variables.extend(other.variables.into_iter());
|
||||||
self.functions.extend(other.functions.into_iter());
|
self.functions.extend(other.functions.into_iter());
|
||||||
self.dynamic_functions
|
self.dynamic_functions += &other.dynamic_functions;
|
||||||
.extend(other.dynamic_functions.into_iter());
|
|
||||||
self.type_iterators.extend(other.type_iterators.into_iter());
|
self.type_iterators.extend(other.type_iterators.into_iter());
|
||||||
self.all_functions.clear();
|
self.all_functions.clear();
|
||||||
self.all_variables.clear();
|
self.all_variables.clear();
|
||||||
@ -1629,8 +1592,7 @@ impl Module {
|
|||||||
}
|
}
|
||||||
self.variables.extend(other.variables.into_iter());
|
self.variables.extend(other.variables.into_iter());
|
||||||
self.functions.extend(other.functions.into_iter());
|
self.functions.extend(other.functions.into_iter());
|
||||||
self.dynamic_functions
|
self.dynamic_functions += &other.dynamic_functions;
|
||||||
.extend(other.dynamic_functions.into_iter());
|
|
||||||
self.type_iterators.extend(other.type_iterators.into_iter());
|
self.type_iterators.extend(other.type_iterators.into_iter());
|
||||||
self.all_functions.clear();
|
self.all_functions.clear();
|
||||||
self.all_variables.clear();
|
self.all_variables.clear();
|
||||||
@ -1666,8 +1628,7 @@ impl Module {
|
|||||||
for (&k, v) in &other.functions {
|
for (&k, v) in &other.functions {
|
||||||
self.functions.entry(k).or_insert_with(|| v.clone());
|
self.functions.entry(k).or_insert_with(|| v.clone());
|
||||||
}
|
}
|
||||||
self.dynamic_functions
|
self.dynamic_functions += &other.dynamic_functions;
|
||||||
.extend(other.dynamic_functions.iter().copied());
|
|
||||||
for (&k, v) in &other.type_iterators {
|
for (&k, v) in &other.type_iterators {
|
||||||
self.type_iterators.entry(k).or_insert_with(|| v.clone());
|
self.type_iterators.entry(k).or_insert_with(|| v.clone());
|
||||||
}
|
}
|
||||||
@ -1718,18 +1679,16 @@ impl Module {
|
|||||||
.iter()
|
.iter()
|
||||||
.filter(|&(.., f)| {
|
.filter(|&(.., f)| {
|
||||||
_filter(
|
_filter(
|
||||||
f.metadata.namespace,
|
f.namespace,
|
||||||
f.metadata.access,
|
f.access,
|
||||||
f.func.is_script(),
|
f.func.is_script(),
|
||||||
f.metadata.name.as_str(),
|
f.name.as_str(),
|
||||||
f.metadata.params,
|
f.num_params,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.map(|(&k, v)| (k, v.clone())),
|
.map(|(&k, v)| (k, v.clone())),
|
||||||
);
|
);
|
||||||
// This may introduce entries that are superfluous because the function has been filtered away.
|
self.dynamic_functions += &other.dynamic_functions;
|
||||||
self.dynamic_functions
|
|
||||||
.extend(other.dynamic_functions.iter().copied());
|
|
||||||
|
|
||||||
self.type_iterators
|
self.type_iterators
|
||||||
.extend(other.type_iterators.iter().map(|(&k, v)| (k, v.clone())));
|
.extend(other.type_iterators.iter().map(|(&k, v)| (k, v.clone())));
|
||||||
@ -1761,12 +1720,7 @@ impl Module {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|(.., f)| {
|
.filter(|(.., f)| {
|
||||||
if f.func.is_script() {
|
if f.func.is_script() {
|
||||||
filter(
|
filter(f.namespace, f.access, f.name.as_str(), f.num_params)
|
||||||
f.metadata.namespace,
|
|
||||||
f.metadata.access,
|
|
||||||
f.metadata.name.as_str(),
|
|
||||||
f.metadata.params,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
@ -1835,10 +1789,10 @@ impl Module {
|
|||||||
> + '_ {
|
> + '_ {
|
||||||
self.iter_fn().filter(|&f| f.func.is_script()).map(|f| {
|
self.iter_fn().filter(|&f| f.func.is_script()).map(|f| {
|
||||||
(
|
(
|
||||||
f.metadata.namespace,
|
f.namespace,
|
||||||
f.metadata.access,
|
f.access,
|
||||||
f.metadata.name.as_str(),
|
f.name.as_str(),
|
||||||
f.metadata.params,
|
f.num_params,
|
||||||
f.func.get_script_fn_def().expect("script-defined function"),
|
f.func.get_script_fn_def().expect("script-defined function"),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -1857,14 +1811,9 @@ impl Module {
|
|||||||
pub fn iter_script_fn_info(
|
pub fn iter_script_fn_info(
|
||||||
&self,
|
&self,
|
||||||
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize)> {
|
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize)> {
|
||||||
self.iter_fn().filter(|&f| f.func.is_script()).map(|f| {
|
self.iter_fn()
|
||||||
(
|
.filter(|&f| f.func.is_script())
|
||||||
f.metadata.namespace,
|
.map(|f| (f.namespace, f.access, f.name.as_str(), f.params))
|
||||||
f.metadata.access,
|
|
||||||
f.metadata.name.as_str(),
|
|
||||||
f.metadata.params,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// _(internals)_ Get an iterator over all script-defined functions in the [`Module`].
|
/// _(internals)_ Get an iterator over all script-defined functions in the [`Module`].
|
||||||
@ -2026,7 +1975,7 @@ impl Module {
|
|||||||
|
|
||||||
ast.shared_lib()
|
ast.shared_lib()
|
||||||
.iter_fn()
|
.iter_fn()
|
||||||
.filter(|&f| match f.metadata.access {
|
.filter(|&f| match f.access {
|
||||||
FnAccess::Public => true,
|
FnAccess::Public => true,
|
||||||
FnAccess::Private => false,
|
FnAccess::Private => false,
|
||||||
})
|
})
|
||||||
@ -2105,7 +2054,7 @@ impl Module {
|
|||||||
|
|
||||||
// Index all Rust functions
|
// Index all Rust functions
|
||||||
for (&hash, f) in &module.functions {
|
for (&hash, f) in &module.functions {
|
||||||
match f.metadata.namespace {
|
match f.namespace {
|
||||||
FnNamespace::Global => {
|
FnNamespace::Global => {
|
||||||
// Flatten all functions with global namespace
|
// Flatten all functions with global namespace
|
||||||
functions.insert(hash, f.func.clone());
|
functions.insert(hash, f.func.clone());
|
||||||
@ -2113,24 +2062,18 @@ impl Module {
|
|||||||
}
|
}
|
||||||
FnNamespace::Internal => (),
|
FnNamespace::Internal => (),
|
||||||
}
|
}
|
||||||
match f.metadata.access {
|
match f.access {
|
||||||
FnAccess::Public => (),
|
FnAccess::Public => (),
|
||||||
FnAccess::Private => continue, // Do not index private functions
|
FnAccess::Private => continue, // Do not index private functions
|
||||||
}
|
}
|
||||||
|
|
||||||
if !f.func.is_script() {
|
if !f.func.is_script() {
|
||||||
let hash_qualified_fn = calc_native_fn_hash(
|
let hash_qualified_fn =
|
||||||
path.iter().copied(),
|
calc_native_fn_hash(path.iter().copied(), f.name.as_str(), &f.param_types);
|
||||||
f.metadata.name.as_str(),
|
|
||||||
&f.param_types,
|
|
||||||
);
|
|
||||||
functions.insert(hash_qualified_fn, f.func.clone());
|
functions.insert(hash_qualified_fn, f.func.clone());
|
||||||
} else if cfg!(not(feature = "no_function")) {
|
} else if cfg!(not(feature = "no_function")) {
|
||||||
let hash_qualified_script = crate::calc_qualified_fn_hash(
|
let hash_qualified_script =
|
||||||
path.iter().copied(),
|
crate::calc_qualified_fn_hash(path.iter().copied(), &f.name, f.num_params);
|
||||||
f.metadata.name.as_str(),
|
|
||||||
f.metadata.params,
|
|
||||||
);
|
|
||||||
functions.insert(hash_qualified_script, f.func.clone());
|
functions.insert(hash_qualified_script, f.func.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,13 +66,13 @@ impl Ord for FnMetadata<'_> {
|
|||||||
|
|
||||||
impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
|
impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
|
||||||
fn from(info: &'a FuncInfo) -> Self {
|
fn from(info: &'a FuncInfo) -> Self {
|
||||||
let base_hash = calc_fn_hash(&info.metadata.name, info.metadata.params);
|
let base_hash = calc_fn_hash(&info.name, info.num_params);
|
||||||
let (typ, full_hash) = if info.func.is_script() {
|
let (typ, full_hash) = if info.func.is_script() {
|
||||||
(FnType::Script, base_hash)
|
(FnType::Script, base_hash)
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
FnType::Native,
|
FnType::Native,
|
||||||
calc_native_fn_hash(None, &info.metadata.name, &info.param_types),
|
calc_native_fn_hash(None, &info.name, &info.param_types),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -80,13 +80,12 @@ impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
|
|||||||
base_hash,
|
base_hash,
|
||||||
full_hash,
|
full_hash,
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
namespace: info.metadata.namespace,
|
namespace: info.namespace,
|
||||||
access: info.metadata.access,
|
access: info.access,
|
||||||
name: &info.metadata.name,
|
name: &info.name,
|
||||||
typ,
|
typ,
|
||||||
num_params: info.metadata.params,
|
num_params: info.num_params,
|
||||||
params: info
|
params: info
|
||||||
.metadata
|
|
||||||
.params_info
|
.params_info
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
@ -100,7 +99,7 @@ impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
|
|||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
_dummy: None,
|
_dummy: None,
|
||||||
return_type: format_type(&info.metadata.return_type, true),
|
return_type: format_type(&info.return_type, true),
|
||||||
signature: info.gen_signature().into(),
|
signature: info.gen_signature().into(),
|
||||||
doc_comments: if info.func.is_script() {
|
doc_comments: if info.func.is_script() {
|
||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
@ -115,7 +114,7 @@ impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
|
|||||||
.map(<_>::as_ref)
|
.map(<_>::as_ref)
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
info.metadata.comments.iter().map(<_>::as_ref).collect()
|
info.comments.iter().map(<_>::as_ref).collect()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
105
src/types/bloom_filter.rs
Normal file
105
src/types/bloom_filter.rs
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
//! A simple bloom filter implementation for `u64` hash values only.
|
||||||
|
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
use std::prelude::v1::*;
|
||||||
|
use std::{
|
||||||
|
mem,
|
||||||
|
ops::{Add, AddAssign},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Number of `usize` values required for 256 bits.
|
||||||
|
const SIZE: usize = (256 / 8) / mem::size_of::<usize>();
|
||||||
|
|
||||||
|
/// A simple bloom filter implementation for `u64` hash values only - i.e., all 64 bits are assumed
|
||||||
|
/// to be relatively random.
|
||||||
|
///
|
||||||
|
/// For this reason, the implementation is simplistic - it just looks at the least significant byte
|
||||||
|
/// of the `u64` hash value and sets the corresponding bit in a 256-long bit vector.
|
||||||
|
///
|
||||||
|
/// The rationale of this type is to avoid pulling in another dependent crate.
|
||||||
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
|
||||||
|
pub struct BloomFilterU64([usize; SIZE]);
|
||||||
|
|
||||||
|
impl BloomFilterU64 {
|
||||||
|
/// Get the bit position of a `u64` hash value.
|
||||||
|
#[inline(always)]
|
||||||
|
const fn hash(value: u64) -> (usize, usize) {
|
||||||
|
let hash = (value & 0x00ff) as usize;
|
||||||
|
(hash / 64, 0x01 << (hash % 64))
|
||||||
|
}
|
||||||
|
/// Create a new [`BloomFilterU64`].
|
||||||
|
#[inline(always)]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self([0; SIZE])
|
||||||
|
}
|
||||||
|
/// Is this [`BloomFilterU64`] empty?
|
||||||
|
#[inline(always)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.0.iter().all(|&v| v == 0)
|
||||||
|
}
|
||||||
|
/// Clear this [`BloomFilterU64`].
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn clear(&mut self) -> &mut Self {
|
||||||
|
self.0 = [0; SIZE];
|
||||||
|
self
|
||||||
|
}
|
||||||
|
/// Mark a `u64` hash into this [`BloomFilterU64`].
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn mark(&mut self, hash: u64) -> &mut Self {
|
||||||
|
let (offset, mask) = Self::hash(hash);
|
||||||
|
self.0[offset] |= mask;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
/// Is a `u64` hash definitely absent from this [`BloomFilterU64`]?
|
||||||
|
#[inline]
|
||||||
|
pub const fn is_absent(&self, hash: u64) -> bool {
|
||||||
|
let (offset, mask) = Self::hash(hash);
|
||||||
|
(self.0[offset] & mask) == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for &BloomFilterU64 {
|
||||||
|
type Output = BloomFilterU64;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
let mut buf = [0; SIZE];
|
||||||
|
|
||||||
|
self.0
|
||||||
|
.iter()
|
||||||
|
.zip(rhs.0.iter())
|
||||||
|
.map(|(&a, &b)| a | b)
|
||||||
|
.zip(buf.iter_mut())
|
||||||
|
.for_each(|(v, x)| *x = v);
|
||||||
|
|
||||||
|
BloomFilterU64(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<BloomFilterU64> for &BloomFilterU64 {
|
||||||
|
type Output = BloomFilterU64;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn add(self, rhs: BloomFilterU64) -> Self::Output {
|
||||||
|
self + &rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddAssign<Self> for BloomFilterU64 {
|
||||||
|
#[inline(always)]
|
||||||
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
|
*self += &rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddAssign<&Self> for BloomFilterU64 {
|
||||||
|
#[inline]
|
||||||
|
fn add_assign(&mut self, rhs: &Self) {
|
||||||
|
self.0
|
||||||
|
.iter_mut()
|
||||||
|
.zip(rhs.0.iter())
|
||||||
|
.for_each(|(x, &v)| *x |= v);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
//! Module defining Rhai data types.
|
//! Module defining Rhai data types.
|
||||||
|
|
||||||
|
pub mod bloom_filter;
|
||||||
pub mod custom_types;
|
pub mod custom_types;
|
||||||
pub mod dynamic;
|
pub mod dynamic;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
@ -9,6 +10,7 @@ pub mod interner;
|
|||||||
pub mod parse_error;
|
pub mod parse_error;
|
||||||
pub mod scope;
|
pub mod scope;
|
||||||
|
|
||||||
|
pub use bloom_filter::BloomFilterU64;
|
||||||
pub use custom_types::{CustomTypeInfo, CustomTypesCollection};
|
pub use custom_types::{CustomTypeInfo, CustomTypesCollection};
|
||||||
pub use dynamic::Dynamic;
|
pub use dynamic::Dynamic;
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
|
Loading…
Reference in New Issue
Block a user