From de47745d4281f7872b121d9b425b57cf139ed707 Mon Sep 17 00:00:00 2001 From: Kisaragi Marine Date: Mon, 16 Oct 2023 22:14:09 +0900 Subject: [PATCH 1/4] perf: avoid unnecessary clone --- package/origlang-compiler/src/type_check.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package/origlang-compiler/src/type_check.rs b/package/origlang-compiler/src/type_check.rs index 89618aa1..b490e4b9 100644 --- a/package/origlang-compiler/src/type_check.rs +++ b/package/origlang-compiler/src/type_check.rs @@ -269,7 +269,7 @@ fn desugar( let tuple_element_types = tuple_element_types.0; if outer_destruction.len() == tuple_element_types.len() { desugar(outer_destruction, TypedExpression::Tuple { - expressions: tuple_element_types.clone().into_iter().enumerate().map(|(i, _)| TypedExpression::ExtractTuple { + expressions: tuple_element_types.iter().enumerate().map(|(i, _)| TypedExpression::ExtractTuple { expr: Box::new( TypedExpression::Variable { ident: ident.clone(), tp: Type::tuple(tuple_element_types.clone()) } ), @@ -300,8 +300,8 @@ fn desugar( desugar(outer_destruction, *final_expression, checker) } TypedExpression::Tuple { expressions } => { - let m = outer_destruction.into_iter().enumerate().map(|(i, element_binding)| { - handle_atomic_pattern(expressions[i].clone(), &element_binding, checker) + let m = outer_destruction.iter().enumerate().map(|(i, element_binding)| { + handle_atomic_pattern(expressions[i].clone(), element_binding, checker) }).collect::>(); let mut k = vec![]; From 4bcf701c8798a53be21c59d552de8cd5cfedb605 Mon Sep 17 00:00:00 2001 From: Kisaragi Marine Date: Mon, 16 Oct 2023 22:21:57 +0900 Subject: [PATCH 2/4] perf: avoid unnecessary clone --- package/origlang-compiler/src/type_check.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package/origlang-compiler/src/type_check.rs b/package/origlang-compiler/src/type_check.rs index b490e4b9..ed233a72 100644 --- a/package/origlang-compiler/src/type_check.rs +++ b/package/origlang-compiler/src/type_check.rs @@ -300,8 +300,8 @@ fn desugar( desugar(outer_destruction, *final_expression, checker) } TypedExpression::Tuple { expressions } => { - let m = outer_destruction.iter().enumerate().map(|(i, element_binding)| { - handle_atomic_pattern(expressions[i].clone(), element_binding, checker) + let m = outer_destruction.iter().zip(expressions).enumerate().map(|(i, (element_binding, expression))| { + handle_atomic_pattern(expression, element_binding, checker) }).collect::>(); let mut k = vec![]; From 43295cd3d75f527c21421cb26066a9d254eaa963 Mon Sep 17 00:00:00 2001 From: Kisaragi Marine Date: Mon, 16 Oct 2023 22:28:10 +0900 Subject: [PATCH 3/4] perf: use Cow --- package/origlang-compiler/src/type_check.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/package/origlang-compiler/src/type_check.rs b/package/origlang-compiler/src/type_check.rs index ed233a72..ff899dbe 100644 --- a/package/origlang-compiler/src/type_check.rs +++ b/package/origlang-compiler/src/type_check.rs @@ -1,5 +1,6 @@ pub mod error; +use std::borrow::Cow; use std::cell::RefCell; use std::collections::hash_map::RandomState; use std::collections::{HashMap, VecDeque}; @@ -252,13 +253,13 @@ fn handle_atomic_pattern( }]) } AtomicPattern::Tuple(tp) => { - desugar(tp.clone(), expr, checker) + desugar(tp.into(), expr, checker) } } } fn desugar( - outer_destruction: Vec, rhs: TypedExpression, checker: &TypeChecker, + outer_destruction: Cow<'_, [AtomicPattern]>, rhs: TypedExpression, checker: &TypeChecker, ) -> Result, TypeCheckError> { debug!("check: {outer_destruction:?} = {rhs:?}"); @@ -279,7 +280,7 @@ fn desugar( } else { debug!("tuple arity mismatch"); Err(TypeCheckError::UnsatisfiablePattern { - pattern: AtomicPattern::Tuple(outer_destruction), + pattern: AtomicPattern::Tuple(outer_destruction.to_vec()), expression: TypedExpression::Variable { ident, tp: Type::tuple(tuple_element_types.clone()) }, expr_type: Type::tuple(tuple_element_types.clone()), }) @@ -288,7 +289,7 @@ fn desugar( other => { debug!("non-tuple expression"); Err(TypeCheckError::UnsatisfiablePattern { - pattern: AtomicPattern::Tuple(outer_destruction), + pattern: AtomicPattern::Tuple(outer_destruction.to_vec()), expression: TypedExpression::Variable { ident, tp: other.clone() }, expr_type: other, }) @@ -371,7 +372,7 @@ fn desugar( other => { debug!("unsupported expression"); Err(TypeCheckError::UnsatisfiablePattern { - pattern: AtomicPattern::Tuple(outer_destruction), + pattern: AtomicPattern::Tuple(outer_destruction.to_vec()), expr_type: other.actual_type(), expression: other, }) From 981b47f55401e4fc0bed7c130dfcea9a3ad705d0 Mon Sep 17 00:00:00 2001 From: Kisaragi Marine Date: Mon, 16 Oct 2023 22:43:36 +0900 Subject: [PATCH 4/4] refactor: express intuition more explicitly --- package/origlang-compiler/src/type_check.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/origlang-compiler/src/type_check.rs b/package/origlang-compiler/src/type_check.rs index ff899dbe..e08646f9 100644 --- a/package/origlang-compiler/src/type_check.rs +++ b/package/origlang-compiler/src/type_check.rs @@ -253,7 +253,7 @@ fn handle_atomic_pattern( }]) } AtomicPattern::Tuple(tp) => { - desugar(tp.into(), expr, checker) + desugar(Cow::Borrowed(tp), expr, checker) } } }