Change fold to for.

This commit is contained in:
Stephen Chung 2022-12-29 17:51:27 +08:00
parent 1a00ca0905
commit ee8e745429
4 changed files with 82 additions and 50 deletions

View File

@ -489,11 +489,13 @@ impl Expr {
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Self::Map(x, ..) if self.is_constant() => { Self::Map(x, ..) if self.is_constant() => {
Dynamic::from_map(x.0.iter().fold(x.1.clone(), |mut map, (k, v)| { let mut map = x.1.clone();
let value_ref = map.get_mut(k.name.as_str()).unwrap();
*value_ref = v.get_literal_value().unwrap(); for (k, v) in &x.0 {
map *map.get_mut(k.name.as_str()).unwrap() = v.get_literal_value().unwrap();
})) }
Dynamic::from_map(map)
} }
// Interpolated string // Interpolated string

View File

@ -18,23 +18,37 @@ impl Dynamic {
/// Panics if any interior data is shared (should never happen). /// Panics if any interior data is shared (should never happen).
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
#[inline] #[inline]
pub(crate) fn calc_array_sizes(array: &crate::Array, _top: bool) -> (usize, usize, usize) { pub(crate) fn calc_array_sizes(array: &crate::Array) -> (usize, usize, usize) {
array let (mut ax, mut mx, mut sx) = (0, 0, 0);
.iter()
.fold((0, 0, 0), |(ax, mx, sx), value| match value.0 { for value in array {
Union::Array(..) => { ax += 1;
let (a, m, s) = value.calc_data_sizes(false);
(ax + a + 1, mx + m, sx + s) match value.0 {
Union::Array(ref a, ..) => {
let (a, m, s) = Self::calc_array_sizes(a);
ax += a;
mx += m;
sx += s;
} }
Union::Blob(ref a, ..) => (ax + 1 + a.len(), mx, sx), Union::Blob(ref a, ..) => ax += 1 + a.len(),
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Union::Map(..) => { Union::Map(ref m, ..) => {
let (a, m, s) = value.calc_data_sizes(false); let (a, m, s) = Self::calc_map_sizes(m);
(ax + a + 1, mx + m, sx + s) ax += a;
mx += m;
sx += s;
} }
Union::Str(ref s, ..) => (ax + 1, mx, sx + s.len()), Union::Str(ref s, ..) => sx += s.len(),
_ => (ax + 1, mx, sx), #[cfg(not(feature = "no_closure"))]
}) Union::Shared(..) => {
unreachable!("shared values discovered within data")
}
_ => (),
}
}
(ax, mx, sx)
} }
/// Recursively calculate the sizes of a map. /// Recursively calculate the sizes of a map.
/// ///
@ -45,23 +59,37 @@ impl Dynamic {
/// Panics if any interior data is shared (should never happen). /// Panics if any interior data is shared (should never happen).
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline] #[inline]
pub(crate) fn calc_map_sizes(map: &crate::Map, _top: bool) -> (usize, usize, usize) { pub(crate) fn calc_map_sizes(map: &crate::Map) -> (usize, usize, usize) {
map.values() let (mut ax, mut mx, mut sx) = (0, 0, 0);
.fold((0, 0, 0), |(ax, mx, sx), value| match value.0 {
#[cfg(not(feature = "no_index"))] for value in map.values() {
Union::Array(..) => { mx += 1;
let (a, m, s) = value.calc_data_sizes(false);
(ax + a, mx + m + 1, sx + s) match value.0 {
Union::Array(ref a, ..) => {
let (a, m, s) = Self::calc_array_sizes(a);
ax += a;
mx += m;
sx += s;
} }
#[cfg(not(feature = "no_index"))] Union::Blob(ref a, ..) => ax += 1 + a.len(),
Union::Blob(ref a, ..) => (ax + a.len(), mx, sx), #[cfg(not(feature = "no_object"))]
Union::Map(..) => { Union::Map(ref m, ..) => {
let (a, m, s) = value.calc_data_sizes(false); let (a, m, s) = Self::calc_map_sizes(m);
(ax + a, mx + m + 1, sx + s) ax += a;
mx += m;
sx += s;
} }
Union::Str(ref s, ..) => (ax, mx + 1, sx + s.len()), Union::Str(ref s, ..) => sx += s.len(),
_ => (ax, mx + 1, sx), #[cfg(not(feature = "no_closure"))]
}) Union::Shared(..) => {
unreachable!("shared values discovered within data")
}
_ => (),
}
}
(ax, mx, sx)
} }
/// Recursively calculate the sizes of a value. /// Recursively calculate the sizes of a value.
/// ///
@ -74,11 +102,11 @@ impl Dynamic {
pub(crate) fn calc_data_sizes(&self, _top: bool) -> (usize, usize, usize) { pub(crate) fn calc_data_sizes(&self, _top: bool) -> (usize, usize, usize) {
match self.0 { match self.0 {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Array(ref arr, ..) => Self::calc_array_sizes(&**arr, _top), Union::Array(ref arr, ..) => Self::calc_array_sizes(&**arr),
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Blob(ref blob, ..) => (blob.len(), 0, 0), Union::Blob(ref blob, ..) => (blob.len(), 0, 0),
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Union::Map(ref map, ..) => Self::calc_map_sizes(&**map, _top), Union::Map(ref map, ..) => Self::calc_map_sizes(&**map),
Union::Str(ref s, ..) => (0, 0, s.len()), Union::Str(ref s, ..) => (0, 0, s.len()),
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
Union::Shared(..) if _top => self.read_lock::<Self>().unwrap().calc_data_sizes(true), Union::Shared(..) if _top => self.read_lock::<Self>().unwrap().calc_data_sizes(true),

View File

@ -237,7 +237,7 @@ pub mod array_functions {
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_array_size() > 0 { if _ctx.engine().max_array_size() > 0 {
let pad = len - array.len(); let pad = len - array.len();
let (a, m, s) = Dynamic::calc_array_sizes(array, true); let (a, m, s) = Dynamic::calc_array_sizes(array);
let (ax, mx, sx) = item.calc_data_sizes(true); let (ax, mx, sx) = item.calc_data_sizes(true);
_ctx.engine() _ctx.engine()

View File

@ -112,20 +112,22 @@ impl StringsInterner {
// 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.
while self.cache.len() > MAX_INTERNED_STRINGS - 3 { while self.cache.len() > MAX_INTERNED_STRINGS - 3 {
let (_, _, n) = self let mut max_len = 0;
.cache let mut min_count = usize::MAX;
.iter() let mut index = 0;
.fold((0, usize::MAX, 0), |(x, c, n), (&k, v)| {
if k != skip_hash
&& (v.strong_count() < c || (v.strong_count() == c && v.len() > x))
{
(v.len(), v.strong_count(), k)
} else {
(x, c, n)
}
});
self.cache.remove(&n); for (&k, v) in &self.cache {
if k != skip_hash
&& (v.strong_count() < min_count
|| (v.strong_count() == min_count && v.len() > max_len))
{
max_len = v.len();
min_count = v.strong_count();
index = k;
}
}
self.cache.remove(&index);
} }
} }