Skip to content

Commit

Permalink
Fine tuned allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jan 18, 2024
1 parent 9e90a94 commit ef9e7e5
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use thin_vec::ThinVec;

/// _(internals)_ A binary expression.
/// Exported under the `internals` feature only.
#[derive(Debug, Clone, Hash)]
#[derive(Debug, Clone, Hash, Default)]
pub struct BinaryExpr {
/// LHS expression.
pub lhs: Expr,
Expand Down
4 changes: 2 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub use namespace_none::Namespace;
#[cfg(not(feature = "no_function"))]
pub use script_fn::{ScriptFnDef, ScriptFnMetadata};
pub use stmt::{
CaseBlocksList, ConditionalExpr, FlowControl, OpAssignment, RangeCase, Stmt, StmtBlock,
StmtBlockContainer, SwitchCasesCollection,
CaseBlocksList, FlowControl, OpAssignment, RangeCase, Stmt, StmtBlock, StmtBlockContainer,
SwitchCasesCollection,
};

/// _(internals)_ Placeholder for a script-defined function.
Expand Down
72 changes: 12 additions & 60 deletions src/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::func::StraightHashMap;
use crate::tokenizer::Token;
use crate::types::dynamic::Union;
use crate::types::Span;
use crate::{calc_fn_hash, Dynamic, Position, INT};
use crate::{calc_fn_hash, Dynamic, FnArgsVec, Position, StaticVec, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
Expand All @@ -17,7 +17,6 @@ use std::{
num::NonZeroUsize,
ops::{Range, RangeInclusive},
};
use thin_vec::ThinVec;

/// _(internals)_ An op-assignment operator.
/// Exported under the `internals` feature only.
Expand Down Expand Up @@ -180,53 +179,6 @@ impl fmt::Debug for OpAssignment {
}
}

/// _(internals)_ An expression with a condition.
/// Exported under the `internals` feature only.
///
/// The condition may simply be [`Expr::BoolConstant`] with `true` if there is actually no condition.
#[derive(Debug, Clone, Default, Hash)]
pub struct ConditionalExpr {
/// Condition.
pub condition: Expr,
/// Expression.
pub expr: Expr,
}

impl<E: Into<Expr>> From<E> for ConditionalExpr {
#[inline(always)]
fn from(value: E) -> Self {
Self {
condition: Expr::BoolConstant(true, Position::NONE),
expr: value.into(),
}
}
}

impl<E: Into<Expr>> From<(Expr, E)> for ConditionalExpr {
#[inline(always)]
fn from(value: (Expr, E)) -> Self {
Self {
condition: value.0,
expr: value.1.into(),
}
}
}

impl ConditionalExpr {
/// Is the condition always `true`?
#[inline(always)]
#[must_use]
pub const fn is_always_true(&self) -> bool {
matches!(self.condition, Expr::BoolConstant(true, ..))
}
/// Is the condition always `false`?
#[inline(always)]
#[must_use]
pub const fn is_always_false(&self) -> bool {
matches!(self.condition, Expr::BoolConstant(false, ..))
}
}

/// _(internals)_ A type containing a range case for a `switch` statement.
/// Exported under the `internals` feature only.
#[derive(Clone, Hash)]
Expand Down Expand Up @@ -379,12 +331,12 @@ pub type CaseBlocksList = smallvec::SmallVec<[usize; 2]>;
/// Exported under the `internals` feature only.
#[derive(Debug, Clone)]
pub struct SwitchCasesCollection {
/// List of [`ConditionalExpr`]'s.
pub expressions: ThinVec<ConditionalExpr>,
/// List of conditional expressions: LHS = condition, RHS = expression.
pub expressions: FnArgsVec<BinaryExpr>,
/// Dictionary mapping value hashes to [`ConditionalExpr`]'s.
pub cases: StraightHashMap<CaseBlocksList>,
/// List of range cases.
pub ranges: ThinVec<RangeCase>,
pub ranges: StaticVec<RangeCase>,
/// Statements block for the default case (there can be no condition for the default case).
pub def_case: Option<usize>,
}
Expand Down Expand Up @@ -902,14 +854,14 @@ impl Stmt {
expr.is_pure()
&& sw.cases.values().flat_map(|cases| cases.iter()).all(|&c| {
let block = &sw.expressions[c];
block.condition.is_pure() && block.expr.is_pure()
block.lhs.is_pure() && block.rhs.is_pure()
})
&& sw.ranges.iter().all(|r| {
let block = &sw.expressions[r.index()];
block.condition.is_pure() && block.expr.is_pure()
block.lhs.is_pure() && block.rhs.is_pure()
})
&& sw.def_case.is_some()
&& sw.expressions[sw.def_case.unwrap()].expr.is_pure()
&& sw.expressions[sw.def_case.unwrap()].rhs.is_pure()
}

// Loops that exit can be pure because it can never be infinite.
Expand Down Expand Up @@ -1059,26 +1011,26 @@ impl Stmt {
for &b in blocks {
let block = &sw.expressions[b];

if !block.condition.walk(path, on_node) {
if !block.lhs.walk(path, on_node) {
return false;
}
if !block.expr.walk(path, on_node) {
if !block.rhs.walk(path, on_node) {
return false;
}
}
}
for r in &sw.ranges {
let block = &sw.expressions[r.index()];

if !block.condition.walk(path, on_node) {
if !block.lhs.walk(path, on_node) {
return false;
}
if !block.expr.walk(path, on_node) {
if !block.rhs.walk(path, on_node) {
return false;
}
}
if let Some(index) = sw.def_case {
if !sw.expressions[index].expr.walk(path, on_node) {
if !sw.expressions[index].lhs.walk(path, on_node) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/eval/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub struct Debugger {
/// The current set of break-points.
break_points: Vec<BreakPoint>,
/// The current function call stack.
call_stack: ThinVec<CallStackFrame>,
call_stack: Vec<CallStackFrame>,
/// The current state.
state: Dynamic,
}
Expand All @@ -266,7 +266,7 @@ impl Debugger {
Self {
status,
break_points: Vec::new(),
call_stack: ThinVec::new(),
call_stack: Vec::new(),
state: Dynamic::UNIT,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/eval/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn search_scope_only<'s>(
{
let val: Dynamic = crate::FnPtr {
name: v.3.clone(),
curry: thin_vec::ThinVec::new(),
curry: <_>::default(),
environ: None,
fn_def: Some(fn_def.clone()),
}
Expand Down
15 changes: 7 additions & 8 deletions src/eval/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::ast::{
ASTFlags, BinaryExpr, ConditionalExpr, Expr, FlowControl, OpAssignment, Stmt,
SwitchCasesCollection,
ASTFlags, BinaryExpr, Expr, FlowControl, OpAssignment, Stmt, SwitchCasesCollection,
};
use crate::eval::search_namespace;
use crate::func::{get_builtin_op_assignment_fn, get_hasher};
Expand Down Expand Up @@ -526,7 +525,7 @@ impl Engine {
for &index in case_blocks_list {
let block = &expressions[index];

let cond_result = match block.condition {
let cond_result = match block.lhs {
Expr::BoolConstant(b, ..) => b,
ref c => self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), c)?
Expand All @@ -537,16 +536,16 @@ impl Engine {
};

if cond_result {
result = Some(&block.expr);
result = Some(&block.rhs);
break;
}
}
} else if !ranges.is_empty() {
// Then check integer ranges
for r in ranges.iter().filter(|r| r.contains(&value)) {
let ConditionalExpr { condition, expr } = &expressions[r.index()];
let BinaryExpr { lhs, rhs } = &expressions[r.index()];

let cond_result = match condition {
let cond_result = match lhs {
Expr::BoolConstant(b, ..) => *b,
c => self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), c)?
Expand All @@ -557,15 +556,15 @@ impl Engine {
};

if cond_result {
result = Some(expr);
result = Some(rhs);
break;
}
}
}
}

result
.or_else(|| def_case.as_ref().map(|&index| &expressions[index].expr))
.or_else(|| def_case.as_ref().map(|&index| &expressions[index].rhs))
.map_or(Ok(Dynamic::UNIT), |expr| {
self.eval_expr(global, caches, scope, this_ptr, expr)
})
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ pub use api::default_limits;

#[cfg(feature = "internals")]
pub use ast::{
ASTFlags, ASTNode, BinaryExpr, ConditionalExpr, Expr, FlowControl, FnCallExpr, FnCallHashes,
Ident, OpAssignment, RangeCase, ScriptFnDef, Stmt, StmtBlock, SwitchCasesCollection,
ASTFlags, ASTNode, BinaryExpr, Expr, FlowControl, FnCallExpr, FnCallHashes, Ident,
OpAssignment, RangeCase, ScriptFnDef, Stmt, StmtBlock, SwitchCasesCollection,
};

#[cfg(feature = "internals")]
Expand Down
Loading

0 comments on commit ef9e7e5

Please sign in to comment.