Skip to content

Commit

Permalink
🎢
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jan 28, 2022
1 parent b0376d6 commit 3c8d256
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
12 changes: 9 additions & 3 deletions src/optimizer/core/src/code_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub fn transform_arrow_fn(
) -> ast::ArrowExpr {
match arrow.body {
ast::BlockStmtOrExpr::BlockStmt(mut block) => {
let mut stmts = vec![];
let mut stmts = Vec::with_capacity(1 + block.stmts.len());
if !scoped_idents.is_empty() {
stmts.push(create_use_closure(qwik_ident, scoped_idents));
}
Expand All @@ -320,7 +320,7 @@ pub fn transform_arrow_fn(
}
}
ast::BlockStmtOrExpr::Expr(expr) => {
let mut stmts = vec![];
let mut stmts = Vec::with_capacity(2);
if !scoped_idents.is_empty() {
stmts.push(create_use_closure(qwik_ident, scoped_idents));
}
Expand All @@ -337,7 +337,13 @@ pub fn transform_arrow_fn(
}

pub fn transform_fn(node: ast::FnExpr, qwik_ident: &Id, scoped_idents: &[Id]) -> ast::FnExpr {
let mut stmts = vec![];
let mut stmts = Vec::with_capacity(
1 + node
.function
.body
.as_ref()
.map_or(0, |body| body.stmts.len()),
);
if !scoped_idents.is_empty() {
stmts.push(create_use_closure(qwik_ident, scoped_idents));
}
Expand Down
26 changes: 14 additions & 12 deletions src/optimizer/core/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ pub struct GlobalCollect {

pub fn global_collect(module: &ast::Module) -> GlobalCollect {
let mut collect = GlobalCollect {
imports: HashMap::new(),
exports: HashMap::new(),
imports: HashMap::with_capacity(16),
exports: HashMap::with_capacity(16),

root: HashMap::new(),
rev_imports: HashMap::new(),
root: HashMap::with_capacity(16),
rev_imports: HashMap::with_capacity(16),

in_export_decl: false,
};
Expand Down Expand Up @@ -116,7 +116,9 @@ impl Visit for GlobalCollect {
}
ast::Decl::Var(var) => {
for decl in &var.decls {
collect_from_pat(&decl.name, &mut self.root);
let mut identifiers: Vec<(Id, Span)> = vec![];
collect_from_pat(&decl.name, &mut identifiers);
self.root.extend(identifiers.into_iter());
}
}
_ => {}
Expand Down Expand Up @@ -277,7 +279,7 @@ impl IdentCollector {
pub fn new() -> Self {
Self {
local_idents: HashSet::new(),
expr_ctxt: vec![],
expr_ctxt: Vec::with_capacity(32),
use_h: false,
use_fragment: false,
}
Expand Down Expand Up @@ -348,10 +350,10 @@ impl Visit for IdentCollector {
}
}

pub fn collect_from_pat(pat: &ast::Pat, identifiers: &mut HashMap<Id, Span>) {
pub fn collect_from_pat(pat: &ast::Pat, identifiers: &mut Vec<(Id, Span)>) {
match pat {
ast::Pat::Ident(ident) => {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
ast::Pat::Array(array) => {
for el in array.elems.iter().flatten() {
Expand All @@ -360,26 +362,26 @@ pub fn collect_from_pat(pat: &ast::Pat, identifiers: &mut HashMap<Id, Span>) {
}
ast::Pat::Rest(rest) => {
if let ast::Pat::Ident(ident) = rest.arg.as_ref() {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
}
ast::Pat::Assign(expr) => {
if let ast::Pat::Ident(ident) = expr.left.as_ref() {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
}
ast::Pat::Object(obj) => {
for prop in &obj.props {
match prop {
ast::ObjectPatProp::Assign(ref v) => {
identifiers.insert(id!(v.key), v.key.span);
identifiers.push((id!(v.key), v.key.span));
}
ast::ObjectPatProp::KeyValue(ref v) => {
collect_from_pat(&v.value, identifiers);
}
ast::ObjectPatProp::Rest(ref v) => {
if let ast::Pat::Ident(ident) = v.arg.as_ref() {
identifiers.insert(id!(ident.id), ident.id.span);
identifiers.push((id!(ident.id), ident.id.span));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const d = onRender$(()=>console.log('thing'));
============================= test.js ==

import * as qwik from "@builder.io/qwik";
import { component } from "@builder.io/qwik";
import { onRender } from "@builder.io/qwik";
import { component } from "@builder.io/qwik";
export const Greeter = component(qwik.qrl(()=>import("./h_test_greeter_component")
, "Greeter_component"));
onRender(qwik.qrl(()=>import("./h_test_d_onrender")
Expand Down
20 changes: 12 additions & 8 deletions src/optimizer/core/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use regex::Regex;
use std::sync::{Arc, Mutex};
use swc_atoms::JsWord;
use swc_common::comments::{Comments, SingleThreadedComments};
use swc_common::{errors::HANDLER, Mark, Span, Spanned, DUMMY_SP};
use swc_common::{errors::HANDLER, Mark, Spanned, DUMMY_SP};
use swc_ecmascript::ast;
use swc_ecmascript::utils::{private_ident, ExprFactory};
use swc_ecmascript::visit::{fold_expr, noop_fold_type, Fold, FoldWith, VisitWith};
Expand Down Expand Up @@ -382,9 +382,9 @@ impl<'a> Fold for QwikTransform<'a> {
stacked = true;
}
if let Some(current_scope) = self.decl_stack.last_mut() {
let mut identifiers: HashMap<Id, _> = HashMap::new();
let mut identifiers = vec![];
collect_from_pat(&node.name, &mut identifiers);
current_scope.extend(identifiers.into_keys().map(|id| (id, IdentType::Var)));
current_scope.extend(identifiers.into_iter().map(|(id, _)| (id, IdentType::Var)));
}
let o = node.fold_children_with(self);
if stacked {
Expand All @@ -400,14 +400,18 @@ impl<'a> Fold for QwikTransform<'a> {
self.stack_ctxt.push(node.ident.sym.to_string());
self.decl_stack.push(vec![]);

let mut idents: HashMap<Id, Span> = HashMap::new();
let mut identifiers = vec![];
for param in &node.function.params {
collect_from_pat(&param.pat, &mut idents);
collect_from_pat(&param.pat, &mut identifiers);
}
self.decl_stack
.last_mut()
.expect("Declaration stack empty!")
.extend(idents.into_keys().map(|key| (key, IdentType::Var)));
.extend(
identifiers
.into_iter()
.map(|(key, _)| (key, IdentType::Var)),
);

let o = node.fold_children_with(self);
self.stack_ctxt.pop();
Expand All @@ -424,9 +428,9 @@ impl<'a> Fold for QwikTransform<'a> {
.expect("Declaration stack empty!");

for param in &node.params {
let mut identifiers: HashMap<Id, _> = HashMap::new();
let mut identifiers = vec![];
collect_from_pat(param, &mut identifiers);
current_scope.extend(identifiers.into_keys().map(|id| (id, IdentType::Var)));
current_scope.extend(identifiers.into_iter().map(|(id, _)| (id, IdentType::Var)));
}

let o = node.fold_children_with(self);
Expand Down

1 comment on commit 3c8d256

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 3c8d256 Previous: fce5ca7 Ratio
transform_todo_app 2171137 ns/iter (± 381757) 1734789 ns/iter (± 8900) 1.25

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.