From 60cf0e669da12c4e2ead50c376574566b3fe9b54 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Mon, 16 Oct 2023 17:07:55 +0300 Subject: [PATCH 1/5] remove some clones --- pax-compiler/src/parsing.rs | 8 ++-- pax-core/src/rendering.rs | 10 ++--- pax-core/src/repeat.rs | 2 +- pax-std/pax-std-primitives/src/frame.rs | 8 ++-- pax-std/pax-std-primitives/src/scroller.rs | 45 ++++++++++------------ pax-std/pax-std-primitives/src/text.rs | 8 ++-- pax-std/src/stacker.rs | 2 +- pax-std/src/types/mod.rs | 8 ++-- 8 files changed, 43 insertions(+), 48 deletions(-) diff --git a/pax-compiler/src/parsing.rs b/pax-compiler/src/parsing.rs index 513389ee4..e20ede668 100644 --- a/pax-compiler/src/parsing.rs +++ b/pax-compiler/src/parsing.rs @@ -368,7 +368,7 @@ fn recurse_visit_tag_pairs_for_template( //add self to parent's children_id_list let mut parents_children_id_list = ctx.child_id_tracking_stack.pop().unwrap(); - parents_children_id_list.push(new_id.clone()); + parents_children_id_list.push(new_id); ctx.child_id_tracking_stack.push(parents_children_id_list); match any_tag_pair.as_rule() { @@ -459,7 +459,7 @@ fn recurse_visit_tag_pairs_for_template( //`if` TemplateNodeDefinition TemplateNodeDefinition { - id: new_id.clone(), + id: new_id, control_flow_settings: Some(ControlFlowSettingsDefinition { condition_expression_paxel: Some(expression_body), condition_expression_vtable_id: None, //This will be written back to this data structure later, during expression compilation @@ -529,7 +529,7 @@ fn recurse_visit_tag_pairs_for_template( //`for` TemplateNodeDefinition TemplateNodeDefinition { - id: new_id.clone(), + id: new_id, type_id: TYPE_ID_REPEAT.to_string(), control_flow_settings: Some(cfavd), settings: None, @@ -549,7 +549,7 @@ fn recurse_visit_tag_pairs_for_template( } TemplateNodeDefinition { - id: *&new_id.clone(), + id: *&new_id, control_flow_settings: Some(ControlFlowSettingsDefinition { condition_expression_paxel: None, condition_expression_vtable_id: None, diff --git a/pax-core/src/rendering.rs b/pax-core/src/rendering.rs index 64ea77195..2e938cbd1 100644 --- a/pax-core/src/rendering.rs +++ b/pax-core/src/rendering.rs @@ -232,18 +232,16 @@ pub trait RenderNode { /// doesn't have a size (e.g. `Group`) fn get_size(&self) -> Option<(Size, Size)> { Some(( - self.get_common_properties() + *self.get_common_properties() .width .as_ref() .borrow() - .get() - .clone(), - self.get_common_properties() + .get(), + *self.get_common_properties() .height .as_ref() .borrow() - .get() - .clone(), + .get(), )) } diff --git a/pax-core/src/repeat.rs b/pax-core/src/repeat.rs index 953eb631e..f0e684c23 100644 --- a/pax-core/src/repeat.rs +++ b/pax-core/src/repeat.rs @@ -84,7 +84,7 @@ impl RenderNode for RepeatInstance { self.cached_old_value_vec.as_ref().unwrap().len() != new_value.len() } }; - self.cached_old_bounds = rtc.bounds.clone(); + self.cached_old_bounds = rtc.bounds; self.cached_old_value_vec = Some(new_value.clone()); (is_dirty, new_value) } else if let Some(se) = &self.source_expression_range { diff --git a/pax-std/pax-std-primitives/src/frame.rs b/pax-std/pax-std-primitives/src/frame.rs index bb37b199f..975a8000e 100644 --- a/pax-std/pax-std-primitives/src/frame.rs +++ b/pax-std/pax-std-primitives/src/frame.rs @@ -96,8 +96,8 @@ impl RenderNode for FrameInstance { None => true, }; if is_new_value { - new_message.size_x = Some(val.clone()); - last_patch.size_x = Some(val.clone()); + new_message.size_x = Some(val); + last_patch.size_x = Some(val); has_any_updates = true; } @@ -107,8 +107,8 @@ impl RenderNode for FrameInstance { None => true, }; if is_new_value { - new_message.size_y = Some(val.clone()); - last_patch.size_y = Some(val.clone()); + new_message.size_y = Some(val); + last_patch.size_y = Some(val); has_any_updates = true; } diff --git a/pax-std/pax-std-primitives/src/scroller.rs b/pax-std/pax-std-primitives/src/scroller.rs index 88879d1b6..60ffe2051 100644 --- a/pax-std/pax-std-primitives/src/scroller.rs +++ b/pax-std/pax-std-primitives/src/scroller.rs @@ -94,8 +94,8 @@ impl RenderNode for ScrollerInstance { fn get_scroll_offset(&mut self) -> (f64, f64) { ( - (*self.scroll_x_offset).borrow().get().clone(), - (*self.scroll_y_offset).borrow().get().clone(), + *(*self.scroll_x_offset).borrow().get(), + *(*self.scroll_y_offset).borrow().get(), ) } @@ -141,8 +141,8 @@ impl RenderNode for ScrollerInstance { None => true, }; if is_new_value { - new_message.size_x = Some(val.clone()); - last_patch.size_x = Some(val.clone()); + new_message.size_x = Some(val); + last_patch.size_x = Some(val); has_any_updates = true; } @@ -152,8 +152,8 @@ impl RenderNode for ScrollerInstance { None => true, }; if is_new_value { - new_message.size_y = Some(val.clone()); - last_patch.size_y = Some(val.clone()); + new_message.size_y = Some(val); + last_patch.size_y = Some(val); has_any_updates = true; } @@ -163,8 +163,8 @@ impl RenderNode for ScrollerInstance { None => true, }; if is_new_value { - new_message.size_inner_pane_x = Some(val.clone()); - last_patch.size_inner_pane_x = Some(val.clone()); + new_message.size_inner_pane_x = Some(val); + last_patch.size_inner_pane_x = Some(val); has_any_updates = true; } @@ -174,8 +174,8 @@ impl RenderNode for ScrollerInstance { None => true, }; if is_new_value { - new_message.size_inner_pane_y = Some(val.clone()); - last_patch.size_inner_pane_y = Some(val.clone()); + new_message.size_inner_pane_y = Some(val); + last_patch.size_inner_pane_y = Some(val); has_any_updates = true; } @@ -185,8 +185,8 @@ impl RenderNode for ScrollerInstance { None => true, }; if is_new_value { - new_message.scroll_x = Some(val.clone()); - last_patch.scroll_x = Some(val.clone()); + new_message.scroll_x = Some(*val); + last_patch.scroll_x = Some(*val); has_any_updates = true; } @@ -196,8 +196,8 @@ impl RenderNode for ScrollerInstance { None => true, }; if is_new_value { - new_message.scroll_y = Some(val.clone()); - last_patch.scroll_y = Some(val.clone()); + new_message.scroll_y = Some(*val); + last_patch.scroll_y = Some(*val); has_any_updates = true; } @@ -228,30 +228,27 @@ impl RenderNode for ScrollerInstance { fn get_clipping_bounds(&self) -> Option<(Size, Size)> { Some(( - self.common_properties.width.as_ref().borrow().get().clone(), - self.common_properties + *self.common_properties.width.as_ref().borrow().get(), + *self.common_properties .height .as_ref() .borrow() - .get() - .clone(), + .get(), )) } fn get_size(&self) -> Option<(Size, Size)> { Some(( - self.properties + *self.properties .as_ref() .borrow() .size_inner_pane_x - .get() - .clone(), - self.properties + .get(), + *self.properties .as_ref() .borrow() .size_inner_pane_y - .get() - .clone(), + .get(), )) } diff --git a/pax-std/pax-std-primitives/src/text.rs b/pax-std/pax-std-primitives/src/text.rs index c25115b53..2e6d25bf8 100644 --- a/pax-std/pax-std-primitives/src/text.rs +++ b/pax-std/pax-std-primitives/src/text.rs @@ -238,8 +238,8 @@ impl RenderNode for TextInstance { None => true, }; if is_new_value { - new_message.size_x = Some(val.clone()); - last_patch.size_x = Some(val.clone()); + new_message.size_x = Some(val); + last_patch.size_x = Some(val); has_any_updates = true; } @@ -249,8 +249,8 @@ impl RenderNode for TextInstance { None => true, }; if is_new_value { - new_message.size_y = Some(val.clone()); - last_patch.size_y = Some(val.clone()); + new_message.size_y = Some(val); + last_patch.size_y = Some(val); has_any_updates = true; } diff --git a/pax-std/src/stacker.rs b/pax-std/src/stacker.rs index 370c67d66..06bd63130 100644 --- a/pax-std/src/stacker.rs +++ b/pax-std/src/stacker.rs @@ -93,7 +93,7 @@ impl Stacker { } Size::Combined(pix, per) => { *pix + (Numeric::from(active_bound) - * (per.clone() / Numeric::from(100.0))) + * (*per / Numeric::from(100.0))) } } .get_as_float(); diff --git a/pax-std/src/types/mod.rs b/pax-std/src/types/mod.rs index d9cf74b07..e2e666c33 100644 --- a/pax-std/src/types/mod.rs +++ b/pax-std/src/types/mod.rs @@ -331,10 +331,10 @@ pub struct RectangleCornerRadii { impl Into for &RectangleCornerRadii { fn into(self) -> RoundedRectRadii { RoundedRectRadii::new( - self.top_left.get().clone(), - self.top_right.get().clone(), - self.bottom_right.get().clone(), - self.bottom_left.get().clone(), + *self.top_left.get(), + *self.top_right.get(), + *self.bottom_right.get(), + *self.bottom_left.get(), ) } } From 2c673c78c67812e64a9841d58d6bd11392e17f63 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Mon, 16 Oct 2023 17:20:10 +0300 Subject: [PATCH 2/5] cargo format and make intersects function leaner --- pax-cli/src/main.rs | 8 ++-- pax-compiler/src/lib.rs | 29 +++++------- pax-compiler/src/parsing.rs | 51 +++++++++++----------- pax-compiler/src/templating.rs | 9 ++-- pax-core/src/expressions.rs | 8 ---- pax-core/src/rendering.rs | 51 ++++++++-------------- pax-macro/src/lib.rs | 1 - pax-macro/src/parsing.rs | 51 ++++++++++------------ pax-std/pax-std-primitives/src/scroller.rs | 18 ++------ pax-std/src/stacker.rs | 6 +-- 10 files changed, 93 insertions(+), 139 deletions(-) diff --git a/pax-cli/src/main.rs b/pax-cli/src/main.rs index 49b8e56af..e29659ba5 100644 --- a/pax-cli/src/main.rs +++ b/pax-cli/src/main.rs @@ -72,9 +72,7 @@ fn main() -> Result<(), ()> { .hidden(true); //hidden because this is of negative value to end-users; things are expected to break when invoked outside of the pax monorepo #[allow(non_snake_case)] - let ARG_LSP = App::new("lsp") - .about("Start the Pax LSP server"); - + let ARG_LSP = App::new("lsp").about("Start the Pax LSP server"); let matches = App::new("pax") .name("pax") @@ -249,7 +247,9 @@ fn perform_nominal_action( } } ("lsp", Some(_)) => { - tokio::runtime::Runtime::new().unwrap().block_on(pax_language_server::start_server()); + tokio::runtime::Runtime::new() + .unwrap() + .block_on(pax_language_server::start_server()); Ok(()) } _ => unreachable!(), // If all subcommands are defined above, anything else is unreachable diff --git a/pax-compiler/src/lib.rs b/pax-compiler/src/lib.rs index c42770963..a4ad8e895 100644 --- a/pax-compiler/src/lib.rs +++ b/pax-compiler/src/lib.rs @@ -728,22 +728,18 @@ fn recurse_generate_render_nodes_literal( Some(format!("PropertyLiteral::new({})", lv)) } ValueDefinition::Expression(_, id) - | ValueDefinition::Identifier(_, id) => { - Some(format!( - "PropertyExpression::new({})", - id.expect("Tried to use expression but it wasn't compiled") - )) - } - ValueDefinition::Block(block) => { - Some(format!( - "PropertyLiteral::new({})", - recurse_literal_block( - block.clone(), - pd.get_type_definition(&rngc.type_table), - host_crate_info - ) - )) - } + | ValueDefinition::Identifier(_, id) => Some(format!( + "PropertyExpression::new({})", + id.expect("Tried to use expression but it wasn't compiled") + )), + ValueDefinition::Block(block) => Some(format!( + "PropertyLiteral::new({})", + recurse_literal_block( + block.clone(), + pd.get_type_definition(&rngc.type_table), + host_crate_info + ) + )), _ => { panic!("Incorrect value bound to inline setting") } @@ -757,7 +753,6 @@ fn recurse_generate_render_nodes_literal( } }; - if let Some(ril_literal_string) = ril_literal_string { Some((pd.name.clone(), ril_literal_string)) } else { diff --git a/pax-compiler/src/parsing.rs b/pax-compiler/src/parsing.rs index e20ede668..af6bc8f82 100644 --- a/pax-compiler/src/parsing.rs +++ b/pax-compiler/src/parsing.rs @@ -845,14 +845,13 @@ impl Default for ParsingContext { } } - #[derive(Debug)] pub struct ParsingError { pub error_name: String, pub error_message: String, pub matched_string: String, pub start: (usize, usize), - pub end: (usize, usize), + pub end: (usize, usize), } /// Extract all errors from a Pax parse result @@ -889,7 +888,7 @@ pub fn extract_errors(pairs: pest::iterators::Pairs) -> Vec format!("{:?}", pair.as_rule()), "Open tag is malformed".to_string(), )), - Rule::tag_error => Some(( + Rule::tag_error => Some(( format!("{:?}", pair.as_rule()), "Tag structure is unexpected.".to_string(), )), @@ -897,7 +896,8 @@ pub fn extract_errors(pairs: pest::iterators::Pairs) -> Vec }; if let Some((error_name, error_message)) = error { let span = pair.as_span(); - let ((line_start, start_col), (line_end, end_col)) = (pair.line_col(),span.end_pos().line_col()); + let ((line_start, start_col), (line_end, end_col)) = + (pair.line_col(), span.end_pos().line_col()); let error = ParsingError { error_name, error_message, @@ -907,7 +907,7 @@ pub fn extract_errors(pairs: pest::iterators::Pairs) -> Vec }; errors.push(error); } - errors.extend(extract_errors(pair.into_inner())); + errors.extend(extract_errors(pair.into_inner())); } errors @@ -927,27 +927,27 @@ pub fn assemble_component_definition( .expect(&format!("unsuccessful parse from {}", &pax)) // unwrap the parse result .next() .unwrap(); // get and unwrap the `pax_component_definition` rule - - let errors = extract_errors(_ast.clone().into_inner()); - if !errors.is_empty() { - let mut error_messages = String::new(); - - for error in &errors { - let msg = format!( - "error: {}\n --> {}:{}\n |\n{} | {}\n |{}\n\n", - error.error_message, - error.start.0, - error.start.1, - error.start.0, - error.matched_string, - "^".repeat(error.matched_string.len()) - ); - error_messages.push_str(&msg); - } - - panic!("{}", error_messages); + + let errors = extract_errors(_ast.clone().into_inner()); + if !errors.is_empty() { + let mut error_messages = String::new(); + + for error in &errors { + let msg = format!( + "error: {}\n --> {}:{}\n |\n{} | {}\n |{}\n\n", + error.error_message, + error.start.0, + error.start.1, + error.start.0, + error.matched_string, + "^".repeat(error.matched_string.len()) + ); + error_messages.push_str(&msg); } - + + panic!("{}", error_messages); + } + if is_main_component { ctx.main_component_type_id = self_type_id.to_string(); } @@ -1277,7 +1277,6 @@ impl Reflectable for pax_runtime_api::Numeric { } } - impl Reflectable for pax_runtime_api::SizePixels { fn get_import_path() -> String { "pax_lang::api::SizePixels".to_string() diff --git a/pax-compiler/src/templating.rs b/pax-compiler/src/templating.rs index f67f73231..70041362f 100644 --- a/pax-compiler/src/templating.rs +++ b/pax-compiler/src/templating.rs @@ -4,8 +4,7 @@ use serde_derive::{Deserialize, Serialize}; #[allow(unused_imports)] use serde_json; use std::collections::HashMap; -use tera::{Tera, Context}; - +use tera::{Context, Tera}; use crate::manifest::{ExpressionSpec, PropertyDefinition}; @@ -137,12 +136,12 @@ pub fn press_template_codegen_cartridge_render_node_literal( .unwrap(); let mut tera = Tera::default(); - tera.add_raw_template("cartridge-render-node-literal", template).unwrap(); + tera.add_raw_template("cartridge-render-node-literal", template) + .unwrap(); tera.render( "cartridge-render-node-literal", &Context::from_serialize(args).unwrap(), ) - .unwrap() + .unwrap() } - diff --git a/pax-core/src/expressions.rs b/pax-core/src/expressions.rs index 9cd5a1aa3..17052bc40 100644 --- a/pax-core/src/expressions.rs +++ b/pax-core/src/expressions.rs @@ -37,14 +37,6 @@ impl PropertyInstance for PropertyExpression { unreachable!() } - // fn is_fresh(&self) -> bool { - // self.is_fresh - // } - // - // fn _mark_not_fresh(&mut self) { - // self.is_fresh = false; - // } - fn _get_vtable_id(&self) -> Option { Some(self.id) } diff --git a/pax-core/src/rendering.rs b/pax-core/src/rendering.rs index 2e938cbd1..b7943ac8e 100644 --- a/pax-core/src/rendering.rs +++ b/pax-core/src/rendering.rs @@ -136,36 +136,31 @@ impl TransformAndBounds { .map(|&p| p.project_onto(axis)) .collect(); - if self_projections - .iter() - .cloned() - .max_by(|a, b| a.partial_cmp(b).unwrap()) - .unwrap() - < other_projections - .iter() - .cloned() - .min_by(|a, b| a.partial_cmp(b).unwrap()) - .unwrap() - || other_projections - .iter() - .cloned() - .max_by(|a, b| a.partial_cmp(b).unwrap()) - .unwrap() - < self_projections - .iter() - .cloned() - .min_by(|a, b| a.partial_cmp(b).unwrap()) - .unwrap() - { + let (min_self, max_self) = min_max_projections(&self_projections); + let (min_other, max_other) = min_max_projections(&other_projections); + + // Check for non-overlapping projections + if max_self < min_other || max_other < min_self { // By the separating axis theorem, non-overlap of projections on _any one_ of the axis-normals proves that these polygons do not intersect. return false; } } - true } } +fn min_max_projections(projections: &[f64]) -> (f64, f64) { + let min_projection = *projections + .iter() + .min_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap(); + let max_projection = *projections + .iter() + .max_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap(); + (min_projection, max_projection) +} + /// The base trait for a RenderNode, representing any node that can /// be rendered by the engine. /// T: a member of PropertiesCoproduct, representing the type of the set of properites @@ -232,16 +227,8 @@ pub trait RenderNode { /// doesn't have a size (e.g. `Group`) fn get_size(&self) -> Option<(Size, Size)> { Some(( - *self.get_common_properties() - .width - .as_ref() - .borrow() - .get(), - *self.get_common_properties() - .height - .as_ref() - .borrow() - .get(), + *self.get_common_properties().width.as_ref().borrow().get(), + *self.get_common_properties().height.as_ref().borrow().get(), )) } diff --git a/pax-macro/src/lib.rs b/pax-macro/src/lib.rs index b1fbcb9b1..06c2b7595 100644 --- a/pax-macro/src/lib.rs +++ b/pax-macro/src/lib.rs @@ -662,7 +662,6 @@ pub fn pax_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { include_imports, is_custom_interpolatable, ) - } else if is_primitive { pax_primitive( input.clone(), diff --git a/pax-macro/src/parsing.rs b/pax-macro/src/parsing.rs index 709b530ce..5a2aeb93c 100644 --- a/pax-macro/src/parsing.rs +++ b/pax-macro/src/parsing.rs @@ -11,14 +11,13 @@ use std::rc::Rc; #[grammar = "pax.pest"] pub struct PaxMacroParser; - #[derive(Debug)] pub struct ParsingError { pub error_name: String, pub error_message: String, pub matched_string: String, pub start: (usize, usize), - pub end: (usize, usize), + pub end: (usize, usize), } /// Extract all errors from a Pax parse result @@ -55,7 +54,7 @@ pub fn extract_errors(pairs: pest::iterators::Pairs) -> Vec format!("{:?}", pair.as_rule()), "Open tag is malformed".to_string(), )), - Rule::tag_error => Some(( + Rule::tag_error => Some(( format!("{:?}", pair.as_rule()), "Tag structure is unexpected.".to_string(), )), @@ -63,7 +62,8 @@ pub fn extract_errors(pairs: pest::iterators::Pairs) -> Vec }; if let Some((error_name, error_message)) = error { let span = pair.as_span(); - let ((line_start, start_col), (line_end, end_col)) = (pair.line_col(),span.end_pos().line_col()); + let ((line_start, start_col), (line_end, end_col)) = + (pair.line_col(), span.end_pos().line_col()); let error = ParsingError { error_name, error_message, @@ -73,40 +73,37 @@ pub fn extract_errors(pairs: pest::iterators::Pairs) -> Vec }; errors.push(error); } - errors.extend(extract_errors(pair.into_inner())); + errors.extend(extract_errors(pair.into_inner())); } errors } - pub fn parse_pascal_identifiers_from_component_definition_string(pax: &str) -> Vec { let pax_component_definition = PaxMacroParser::parse(Rule::pax_component_definition, pax) .expect(&format!("unsuccessful parse from {}", &pax)) .next() .unwrap(); - let errors = extract_errors(pax_component_definition.clone().into_inner()); - if !errors.is_empty() { - let mut error_messages = String::new(); - - for error in &errors { - let msg = format!( - "error: {}\n --> {}:{}\n |\n{} | {}\n |{}\n\n", - error.error_message, - error.start.0, - error.start.1, - error.start.0, - error.matched_string, - "^".repeat(error.matched_string.len()) - ); - error_messages.push_str(&msg); - } - - panic!("{}", error_messages); + let errors = extract_errors(pax_component_definition.clone().into_inner()); + if !errors.is_empty() { + let mut error_messages = String::new(); + + for error in &errors { + let msg = format!( + "error: {}\n --> {}:{}\n |\n{} | {}\n |{}\n\n", + error.error_message, + error.start.0, + error.start.1, + error.start.0, + error.matched_string, + "^".repeat(error.matched_string.len()) + ); + error_messages.push_str(&msg); } - - + + panic!("{}", error_messages); + } let pascal_identifiers = Rc::new(RefCell::new(HashSet::new())); pax_component_definition @@ -120,7 +117,7 @@ pub fn parse_pascal_identifiers_from_component_definition_string(pax: &str) -> V } _ => {} }); - + let unwrapped_hashmap = Rc::try_unwrap(pascal_identifiers).unwrap().into_inner(); unwrapped_hashmap.into_iter().collect() } diff --git a/pax-std/pax-std-primitives/src/scroller.rs b/pax-std/pax-std-primitives/src/scroller.rs index 60ffe2051..c9b2f33fe 100644 --- a/pax-std/pax-std-primitives/src/scroller.rs +++ b/pax-std/pax-std-primitives/src/scroller.rs @@ -229,26 +229,14 @@ impl RenderNode for ScrollerInstance { fn get_clipping_bounds(&self) -> Option<(Size, Size)> { Some(( *self.common_properties.width.as_ref().borrow().get(), - *self.common_properties - .height - .as_ref() - .borrow() - .get(), + *self.common_properties.height.as_ref().borrow().get(), )) } fn get_size(&self) -> Option<(Size, Size)> { Some(( - *self.properties - .as_ref() - .borrow() - .size_inner_pane_x - .get(), - *self.properties - .as_ref() - .borrow() - .size_inner_pane_y - .get(), + *self.properties.as_ref().borrow().size_inner_pane_x.get(), + *self.properties.as_ref().borrow().size_inner_pane_y.get(), )) } diff --git a/pax-std/src/stacker.rs b/pax-std/src/stacker.rs index 06bd63130..2cdcf1db3 100644 --- a/pax-std/src/stacker.rs +++ b/pax-std/src/stacker.rs @@ -3,8 +3,7 @@ use crate::types::{StackerCell, StackerDirection}; use pax_lang::api::numeric::Numeric; use pax_lang::api::{Property, Size, Transform2D}; use pax_lang::*; -use pax_runtime_api::{RuntimeContext, PropertyLiteral}; - +use pax_runtime_api::{PropertyLiteral, RuntimeContext}; /// Stacker lays out a series of nodes either /// vertically or horizontally (i.e. a single row or column) with a specified gutter in between @@ -92,8 +91,7 @@ impl Stacker { Numeric::from(active_bound) * (*per / Numeric::from(100.0)) } Size::Combined(pix, per) => { - *pix + (Numeric::from(active_bound) - * (*per / Numeric::from(100.0))) + *pix + (Numeric::from(active_bound) * (*per / Numeric::from(100.0))) } } .get_as_float(); From 1a9e1dbf407f9361d76ab47a5e62619626444715 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Mon, 16 Oct 2023 17:31:05 +0300 Subject: [PATCH 3/5] remove commented code and clone --- pax-core/src/repeat.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pax-core/src/repeat.rs b/pax-core/src/repeat.rs index f0e684c23..5b4cccbe8 100644 --- a/pax-core/src/repeat.rs +++ b/pax-core/src/repeat.rs @@ -74,7 +74,6 @@ impl RenderNode for RepeatInstance { se.get().clone() }; - //let is_dirty = true; //Major hack: will only consider a new vec dirty if its cardinality changes. let is_dirty = { rtc.bounds != self.cached_old_bounds @@ -101,7 +100,6 @@ impl RenderNode for RepeatInstance { unreachable!() }; - //let is_dirty = true; //Major hack: will only consider a new vec dirty if its cardinality changes. let is_dirty = { rtc.bounds != self.cached_old_bounds @@ -111,7 +109,7 @@ impl RenderNode for RepeatInstance { self.cached_old_value_range.as_ref().unwrap().len() != new_value.len() } }; - self.cached_old_bounds = rtc.bounds.clone(); + self.cached_old_bounds = rtc.bounds; self.cached_old_value_range = Some(new_value.clone()); let normalized_vec_of_props = new_value .into_iter() From ee597ce1e815b6733badabeffcfcad149206d049 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Tue, 17 Oct 2023 14:58:08 +0300 Subject: [PATCH 4/5] create a table for builtin types, avoid cloning --- pax-compiler/src/expressions.rs | 95 +++++++++++++++------------------ 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/pax-compiler/src/expressions.rs b/pax-compiler/src/expressions.rs index 523c529a1..dd0253bea 100644 --- a/pax-compiler/src/expressions.rs +++ b/pax-compiler/src/expressions.rs @@ -12,6 +12,21 @@ use crate::parsing::escape_identifier; use itertools::Itertools; use lazy_static::lazy_static; +const BUILTIN_TYPES: &'static [(&str, &str); 12] = &[ + ("transform", "Transform2D"), + ("width", "Size"), + ("height", "Size"), + ("x", "Size"), + ("y", "Size"), + ("anchor_x", "Size"), + ("anchor_y", "Size"), + ("skew_x", "Numeric"), + ("skew_y", "Numeric"), + ("scale_x", "Size"), + ("scale_y", "Size"), + ("rotate", "Rotation"), +]; + pub fn compile_all_expressions<'a>(manifest: &'a mut PaxManifest) { let mut swap_expression_specs: HashMap = HashMap::new(); let mut all_expression_specs: HashMap = HashMap::new(); @@ -76,22 +91,14 @@ fn pull_settings_with_selector( settings: &Option>, selector: String, ) -> Option> { - if let Some(val) = settings { + settings.as_ref().and_then(|val| { let merged_settings: Vec<(String, ValueDefinition)> = val .iter() .filter(|block| block.selector == selector) - .map(|block| block.value_block.settings_key_value_pairs.clone()) - .flatten() - .clone() + .flat_map(|block| block.value_block.settings_key_value_pairs.clone()) .collect(); - if merged_settings.len() > 0 { - Some(merged_settings) - } else { - None - } - } else { - None - } + (!merged_settings.is_empty()).then(|| merged_settings) + }) } fn merge_inline_settings_with_settings_block( @@ -155,12 +162,9 @@ fn recurse_compile_literal_block<'a>( ) { settings_pairs.for_each(|pair| { match &mut pair.1 { - ValueDefinition::LiteralValue(_) => { - //no need to compile literal values - } - ValueDefinition::EventBindingTarget(_) => { - //event bindings are handled on a separate compiler pass; no-op here - } + // LiteralValue: no need to compile literal values + // EventBindingTarget: event bindings are handled on a separate compiler pass; no-op here + ValueDefinition::LiteralValue(_) | ValueDefinition::EventBindingTarget(_) => {} ValueDefinition::Block(block) => { let type_def = (current_property_definitions .iter() @@ -183,22 +187,10 @@ fn recurse_compile_literal_block<'a>( let (output_statement, invocations) = compile_paxel_to_ril(&input, &ctx); - let builtin_types = HashMap::from([ - ("transform", "Transform2D".to_string()), - ("width", "Size".to_string()), - ("height", "Size".to_string()), - ("x", "Size".to_string()), - ("y", "Size".to_string()), - ("anchor_x", "Size".to_string()), - ("anchor_y", "Size".to_string()), - ("skew_x", "Numeric".to_string()), - ("skew_y", "Numeric".to_string()), - ("scale_x", "Size".to_string()), - ("scale_y", "Size".to_string()), - ("rotate", "Rotation".to_string()), - ]); - - let pascalized_return_type = if let Some(type_string) = builtin_types.get(&*pair.0) + let pascalized_return_type = if let Some(type_string) = BUILTIN_TYPES + .iter() + .find(|type_str| type_str.0 == &*pair.0) + .map(|type_str| type_str.1) { type_string.to_string() } else { @@ -776,26 +768,23 @@ impl<'a> ExpressionCompilationContext<'a> { }; // handle nested symbols like `foo.bar`. - match root_symbol_pd { - Some(root_symbol_pd) => { - let mut ret = vec![root_symbol_pd]; + root_symbol_pd.map(|root_symbol_pd| { + let mut ret = vec![root_symbol_pd]; + for atomic_symbol in split_symbols { + let td = ret.last().unwrap().get_type_definition(self.type_table); //return terminal nested symbol's PropertyDefinition, or root's if there are no nested symbols - while let Some(atomic_symbol) = split_symbols.next() { - let td = ret.last().unwrap().get_type_definition(self.type_table); - ret.push( - td.property_definitions - .iter() - .find(|pd| pd.name == *atomic_symbol) - .expect(&format!( - "Unable to resolve nested symbol `{}` while evaluating `{}`.", - atomic_symbol, symbol - )) - .clone(), - ); - } - Some(ret) + let next_pd = td + .property_definitions + .iter() + .find(|pd| pd.name == *atomic_symbol) + .expect(&format!( + "Unable to resolve nested symbol `{}` while evaluating `{}`.", + atomic_symbol, symbol + )) + .clone(); + ret.push(next_pd); } - None => None, - } + ret + }) } } From 77e6d29705e58ed895d9ffec41e0afa36201354d Mon Sep 17 00:00:00 2001 From: ouz-a Date: Thu, 19 Oct 2023 16:15:13 +0300 Subject: [PATCH 5/5] minor --- pax-core/src/repeat.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pax-core/src/repeat.rs b/pax-core/src/repeat.rs index 5b4cccbe8..eae9a6acf 100644 --- a/pax-core/src/repeat.rs +++ b/pax-core/src/repeat.rs @@ -113,8 +113,7 @@ impl RenderNode for RepeatInstance { self.cached_old_value_range = Some(new_value.clone()); let normalized_vec_of_props = new_value .into_iter() - .enumerate() - .map(|(_i, elem)| Rc::new(PropertiesCoproduct::isize(elem))) + .map(| elem| Rc::new(PropertiesCoproduct::isize(elem))) .collect(); (is_dirty, normalized_vec_of_props) } else { @@ -210,7 +209,6 @@ impl RenderNode for RepeatInstance { self.cached_old_value_vec = None; } } - /* lab journal, zb ---------------