Skip to content

Commit

Permalink
perf: remove handleWatch when possible (QwikDev#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat authored Jun 8, 2022
1 parent c10b275 commit 255d044
Show file tree
Hide file tree
Showing 46 changed files with 512 additions and 717 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "qwik-monorepo",
"version": "0.0.23",
"version": "0.0.24",
"scripts": {
"build": "yarn node scripts --tsc --build --api --platform-binding-wasm-copy",
"build.full": "yarn node scripts --tsc --build --api --eslint --qwikcity --platform-binding --wasm",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-qwik/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-qwik",
"version": "0.0.23",
"version": "0.0.24",
"description": "Interactive CLI and API for generating Qwik projects.",
"bin": "create-qwik",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-qwik/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-qwik",
"version": "0.0.23",
"version": "0.0.24",
"description": "An Open-Source sub-framework designed with a focus on server-side-rendering, lazy-loading, and styling/animation.",
"main": "index.js",
"author": "Builder Team",
Expand Down
3 changes: 2 additions & 1 deletion packages/qwik/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@builder.io/qwik",
"version": "0.0.23",
"version": "0.0.24",
"description": "An Open-Source sub-framework designed with a focus on server-side-rendering, lazy-loading, and styling/animation.",
"main": "./dist/core.cjs",
"module": "./dist/core.mjs",
"types": "./dist/core.d.ts",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/core.mjs",
Expand Down
81 changes: 50 additions & 31 deletions packages/qwik/src/optimizer/core/src/code_move.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::collector::{new_ident_from_id, GlobalCollect, Id, ImportKind};
use crate::parse::{emit_source_code, HookAnalysis, PathData, TransformModule, TransformOutput};
use crate::transform::{add_handle_watch, create_internal_call, create_synthetic_wildcard_import};
use crate::parse::{
emit_source_code, might_need_handle_watch, HookAnalysis, PathData, TransformModule,
TransformOutput,
};
use crate::transform::{add_handle_watch, create_synthetic_named_import};
use crate::words::*;

use std::collections::BTreeMap;
Expand All @@ -12,6 +15,13 @@ use swc_atoms::JsWord;
use swc_common::comments::{SingleThreadedComments, SingleThreadedCommentsMap};
use swc_common::{sync::Lrc, SourceMap, DUMMY_SP};
use swc_ecmascript::ast;
use swc_ecmascript::utils::private_ident;

macro_rules! id {
($ident: expr) => {
($ident.sym.clone(), $ident.span.ctxt())
};
}

pub struct NewModuleCtx<'a> {
pub expr: Box<ast::Expr>,
Expand All @@ -21,8 +31,8 @@ pub struct NewModuleCtx<'a> {
pub local_idents: &'a [Id],
pub scoped_idents: &'a [Id],
pub global: &'a GlobalCollect,
pub qwik_ident: &'a Id,
pub is_entry: bool,
pub need_handle_watch: bool,
pub leading_comments: SingleThreadedCommentsMap,
pub trailing_comments: SingleThreadedCommentsMap,
}
Expand All @@ -39,11 +49,15 @@ pub fn new_module(ctx: NewModuleCtx) -> Result<(ast::Module, SingleThreadedComme
shebang: None,
};

// Inject qwik internal import
module.body.push(create_synthetic_wildcard_import(
ctx.qwik_ident,
&BUILDER_IO_QWIK,
));
let use_lexical_scope = if !ctx.scoped_idents.is_empty() {
let new_local = id!(private_ident!(&USE_LEXICAL_SCOPE.clone()));
module
.body
.push(create_synthetic_named_import(&new_local, &BUILDER_IO_QWIK));
Some(new_local)
} else {
None
};

for id in ctx.local_idents {
if let Some(import) = ctx.global.imports.get(id) {
Expand Down Expand Up @@ -112,18 +126,18 @@ pub fn new_module(ctx: NewModuleCtx) -> Result<(ast::Module, SingleThreadedComme
}
}

let expr = if !ctx.scoped_idents.is_empty() {
let expr = if let Some(use_lexical_scope) = use_lexical_scope {
Box::new(transform_function_expr(
*ctx.expr,
ctx.qwik_ident,
&use_lexical_scope,
ctx.scoped_idents,
))
} else {
ctx.expr
};

module.body.push(create_named_export(expr, ctx.name));
if ctx.is_entry {
if ctx.need_handle_watch {
// Inject qwik internal import
add_handle_watch(&mut module.body, true);
}
Expand Down Expand Up @@ -258,11 +272,15 @@ fn new_entry_module(hooks: &[&HookAnalysis], explicity_extensions: bool) -> ast:
body: Vec::with_capacity(hooks.len()),
shebang: None,
};
let mut need_handle_watch = false;
for hook in hooks {
let mut src = ["./", &hook.canonical_filename].concat();
if explicity_extensions {
src = src + "." + hook.extension.as_ref();
}
if might_need_handle_watch(&hook.ctx_kind, &hook.ctx_name) {
need_handle_watch = true;
}
module
.body
.push(ast::ModuleItem::ModuleDecl(ast::ModuleDecl::ExportNamed(
Expand All @@ -287,36 +305,35 @@ fn new_entry_module(hooks: &[&HookAnalysis], explicity_extensions: bool) -> ast:
},
)));
}

add_handle_watch(&mut module.body, false);
if need_handle_watch {
add_handle_watch(&mut module.body, false);
}
module
}

pub fn transform_function_expr(
expr: ast::Expr,
qwik_ident: &Id,
use_lexical_scope: &Id,
scoped_idents: &[Id],
) -> ast::Expr {
match expr {
ast::Expr::Arrow(node) => {
ast::Expr::Arrow(transform_arrow_fn(node, qwik_ident, scoped_idents))
ast::Expr::Arrow(transform_arrow_fn(node, use_lexical_scope, scoped_idents))
}
ast::Expr::Fn(node) => ast::Expr::Fn(transform_fn(node, qwik_ident, scoped_idents)),
ast::Expr::Fn(node) => ast::Expr::Fn(transform_fn(node, use_lexical_scope, scoped_idents)),
_ => expr,
}
}

pub fn transform_arrow_fn(
fn transform_arrow_fn(
arrow: ast::ArrowExpr,
qwik_ident: &Id,
use_lexical_scope: &Id,
scoped_idents: &[Id],
) -> ast::ArrowExpr {
match arrow.body {
ast::BlockStmtOrExpr::BlockStmt(mut block) => {
let mut stmts = Vec::with_capacity(1 + block.stmts.len());
if !scoped_idents.is_empty() {
stmts.push(create_use_lexical_scope(qwik_ident, scoped_idents));
}
stmts.push(create_use_lexical_scope(use_lexical_scope, scoped_idents));
stmts.append(&mut block.stmts);
ast::ArrowExpr {
body: ast::BlockStmtOrExpr::BlockStmt(ast::BlockStmt {
Expand All @@ -329,7 +346,7 @@ pub fn transform_arrow_fn(
ast::BlockStmtOrExpr::Expr(expr) => {
let mut stmts = Vec::with_capacity(2);
if !scoped_idents.is_empty() {
stmts.push(create_use_lexical_scope(qwik_ident, scoped_idents));
stmts.push(create_use_lexical_scope(use_lexical_scope, scoped_idents));
}
stmts.push(create_return_stmt(expr));
ast::ArrowExpr {
Expand All @@ -343,7 +360,7 @@ pub fn transform_arrow_fn(
}
}

pub fn transform_fn(node: ast::FnExpr, qwik_ident: &Id, scoped_idents: &[Id]) -> ast::FnExpr {
fn transform_fn(node: ast::FnExpr, use_lexical_scope: &Id, scoped_idents: &[Id]) -> ast::FnExpr {
let mut stmts = Vec::with_capacity(
1 + node
.function
Expand All @@ -352,7 +369,7 @@ pub fn transform_fn(node: ast::FnExpr, qwik_ident: &Id, scoped_idents: &[Id]) ->
.map_or(0, |body| body.stmts.len()),
);
if !scoped_idents.is_empty() {
stmts.push(create_use_lexical_scope(qwik_ident, scoped_idents));
stmts.push(create_use_lexical_scope(use_lexical_scope, scoped_idents));
}
if let Some(mut body) = node.function.body {
stmts.append(&mut body.stmts);
Expand All @@ -376,20 +393,22 @@ const fn create_return_stmt(expr: Box<ast::Expr>) -> ast::Stmt {
})
}

fn create_use_lexical_scope(qwik_ident: &Id, scoped_idents: &[Id]) -> ast::Stmt {
fn create_use_lexical_scope(use_lexical_scope: &Id, scoped_idents: &[Id]) -> ast::Stmt {
ast::Stmt::Decl(ast::Decl::Var(ast::VarDecl {
span: DUMMY_SP,
declare: false,
kind: ast::VarDeclKind::Const,
decls: vec![ast::VarDeclarator {
definite: false,
span: DUMMY_SP,
init: Some(Box::new(ast::Expr::Call(create_internal_call(
qwik_ident,
&USE_LEXICAL_SCOPE,
vec![],
None,
)))),
init: Some(Box::new(ast::Expr::Call(ast::CallExpr {
callee: ast::Callee::Expr(Box::new(ast::Expr::Ident(new_ident_from_id(
use_lexical_scope,
)))),
span: DUMMY_SP,
type_args: None,
args: vec![],
}))),
name: ast::Pat::Array(ast::ArrayPat {
span: DUMMY_SP,
optional: false,
Expand Down
11 changes: 10 additions & 1 deletion packages/qwik/src/optimizer/core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
for h in hooks.into_iter() {
let is_entry = h.entry == None;
let hook_path = [&h.canonical_filename, ".", &h.data.extension].concat();
let need_handle_watch =
might_need_handle_watch(&h.data.ctx_kind, &h.data.ctx_name) && is_entry;

let (mut hook_module, comments) = new_module(NewModuleCtx {
expr: h.expr,
Expand All @@ -295,7 +297,7 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
local_idents: &h.data.local_idents,
scoped_idents: &h.data.scoped_idents,
global: &qwik_transform.options.global_collect,
qwik_ident: &qwik_transform.qwik_ident,
need_handle_watch,
is_entry,
leading_comments: comments_maps.0.clone(),
trailing_comments: comments_maps.1.clone(),
Expand Down Expand Up @@ -570,3 +572,10 @@ pub fn parse_path(src: &str) -> Result<PathData, Error> {
file_prefix: file_prefix.into(),
})
}

pub fn might_need_handle_watch(ctx_kind: &HookKind, ctx_name: &str) -> bool {
if matches!(ctx_kind, HookKind::Event) {
return false;
}
matches!(ctx_name, "useClientEffect$" | "$")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const renderHeader = component($(() => {

============================= renderheader_zbbhwn4e8cg.tsx (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
import { qrl } from "@builder.io/qwik";
export const renderHeader_zBbHWn4e8Cg = ()=>{
return <div onClick={qwik.qrl(()=>import("./renderheader_div_onclick_fv2uzal99u4")
return <div onClick={qrl(()=>import("./renderheader_div_onclick_fv2uzal99u4")
, "renderHeader_div_onClick_fV2uzAL99u4")}/>;
};
import { handleWatch as hW } from "@builder.io/qwik";
hW.issue456 && hW.issue123();
hW.issue123 && hW.issue123();
export { hW as handleWatch };

/*
Expand All @@ -49,13 +49,12 @@ export { hW as handleWatch };
*/
============================= renderheader_1_cpyr0uahiik.tsx (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
export const renderHeader_1_cPyr0uAhiIk = ()=>{
console.log("mount");
return render;
};
import { handleWatch as hW } from "@builder.io/qwik";
hW.issue456 && hW.issue123();
hW.issue123 && hW.issue123();
export { hW as handleWatch };

/*
Expand All @@ -79,20 +78,19 @@ export { hW as handleWatch };
*/
============================= test.tsx ==

import * as qwik from "@builder.io/qwik";
import { qrl } from "@builder.io/qwik";
import { component } from '@builder.io/qwik';
export const renderHeader = qwik.qrl(()=>import("./renderheader_zbbhwn4e8cg")
export const renderHeader = qrl(()=>import("./renderheader_zbbhwn4e8cg")
, "renderHeader_zBbHWn4e8Cg");
component(qwik.qrl(()=>import("./renderheader_1_cpyr0uahiik")
component(qrl(()=>import("./renderheader_1_cpyr0uahiik")
, "renderHeader_1_cPyr0uAhiIk"));

============================= renderheader_div_onclick_fv2uzal99u4.tsx (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
export const renderHeader_div_onClick_fV2uzAL99u4 = (ctx)=>console.log(ctx)
;
import { handleWatch as hW } from "@builder.io/qwik";
hW.issue456 && hW.issue123();
hW.issue123 && hW.issue123();
export { hW as handleWatch };

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const Header = $((decl1, {decl2}, [decl3]) => {

============================= header_wlr3xni6u38.tsx (ENTRY POINT)==

import * as qwik from "@builder.io/qwik";
export const Header_WlR3xnI6u38 = (decl1, { decl2 }, [decl3])=>{
const hola = ident1.no;
ident2;
Expand All @@ -50,7 +49,7 @@ export const Header_WlR3xnI6u38 = (decl1, { decl2 }, [decl3])=>{
} required={false}/>;
};
import { handleWatch as hW } from "@builder.io/qwik";
hW.issue456 && hW.issue123();
hW.issue123 && hW.issue123();
export { hW as handleWatch };

/*
Expand All @@ -74,8 +73,8 @@ export { hW as handleWatch };
*/
============================= project/test.tsx ==

import * as qwik from "@builder.io/qwik";
qwik.qrl(()=>import("../header_wlr3xni6u38")
import { qrl } from "@builder.io/qwik";
qrl(()=>import("../header_wlr3xni6u38")
, "Header_WlR3xnI6u38");

== DIAGNOSTICS ==
Expand Down
Loading

0 comments on commit 255d044

Please sign in to comment.