Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimising at the AST level #646

Closed
Rich-Harris opened this issue Jun 16, 2017 · 2 comments
Closed

Optimising at the AST level #646

Rich-Harris opened this issue Jun 16, 2017 · 2 comments
Labels
compiler Changes relating to the compiler feature request perf
Milestone

Comments

@Rich-Harris
Copy link
Member

Rich-Harris commented Jun 16, 2017

Second random thought after #645. In cases like this...

{#if foo}
  <p>foo!</p>
{:else}
  <p>not foo!</p>
{/if}

...we have to create three blocks (create_main_fragment, create_if_block and create_if_block_1), totalling 74 LOC as of the current version. If we optimised it to the equivalent of this...

<p>{foo ? 'foo!' : 'not foo!'}</p>

...we get the same result with one block (create_main_fragment) totalling 25 LOC, and there's less work to do to update things.

There are probably all sorts of optimisations like this we could perform. Might not be straightforward, but definitely worth investigating.

@mrkishi
Copy link
Member

mrkishi commented Aug 5, 2018

On the if optimization, we could actually get better output than the equivalent {foo ? 'foo!' : 'not foo!'}, since the ternary ends up duplicated as-is:

function create_main_fragment(component, ctx) {
	var p, text_value = ctx.foo ? 'Some large static string...' : 'Or an even bigger one here!', text, current;
	return {

		[...]

		p: function update(changed, ctx) {
			if ((changed.foo) && text_value !== (text_value = ctx.foo ? 'Some large static string...' : 'Or an even bigger one here!')) {
				setData(text, text_value);
			}
		}

		[...]

	};
}

@Rich-Harris
Copy link
Member Author

Yeah, that's unfortunate. I've wondered about having block-level helpers like

function create_main_fragment(component, ctx) {
  var p, get_text = ctx => ctx.foo ? 'Some large static string...' : 'Or an even bigger one here!', text_value=get_text(ctx), text, current;
  return {
    // ...
    p(changed, ctx) {
      if (changed.foo && (text_value !== get_text(ctx)) {
        setData(text, text_value);
      }
    }
  };
}

In fact those could be hoisted, and potentially even deduplicated across a bundle:

const get_text = ctx => ctx.foo ? 'Some large static string...' : 'Or an even bigger one here!';

function create_main_fragment(component, ctx) {
  var p, text_value=get_text(ctx), text, current;
  return {
    // ...
    p(changed, ctx) {
      if (changed.foo && (text_value !== get_text(ctx)) {
        setData(text, text_value);
      }
    }
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Changes relating to the compiler feature request perf
Projects
None yet
Development

No branches or pull requests

6 participants