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

Mangle context #2318

Closed
Rich-Harris opened this issue Mar 26, 2019 · 1 comment
Closed

Mangle context #2318

Rich-Harris opened this issue Mar 26, 2019 · 1 comment

Comments

@Rich-Harris
Copy link
Member

Just updated the todomvc implementation, and the size has gone down slightly from v2 which is pleasing. Nonetheless I'm annoyed by all the unmangled identifiers you can see in http://svelte-todomvc.surge.sh/bundle.js — several occurrences of things like numCompleted and click_handler.

There's a couple of ways we could approach this. One is simply to map identifiers to mangled equivalents — so ctx.numCompleted because ctx.a, for example. (This could easily be a production-only thing.)

Another would be for ctx to be an array rather than an object, so that the generated code for this counter would change to this:

/* App.svelte generated by Svelte v3.0.0-beta.20 */
import {
	SvelteComponent,
	append,
	detach,
	element,
	init,
	insert,
	listen,
	noop,
	safe_not_equal,
	set_data,
	text
} from "svelte/internal";

function create_fragment(ctx) {
	var button, t0, t1, dispose;

	return {
		c() {
			button = element("button");
			t0 = text("clicks: ");
-			t1 = text(ctx.count);
-			dispose = listen(button, "click", ctx.handleClick);
+			t1 = text(ctx[0]);
+			dispose = listen(button, "click", ctx[1]);
		},

		m(target, anchor) {
			insert(target, button, anchor);
			append(button, t0);
			append(button, t1);
		},

		p(changed, ctx) {
-			if (changed.count) {
-				set_data(t1, ctx.count);
+			if (changed[0]) {
+				set_data(t1, ctx[0]);
			}
		},

		i: noop,
		o: noop,

		d(detaching) {
			if (detaching) {
				detach(button);
			}

			dispose();
		}
	};
}

function instance($$self, $$props, $$invalidate) {
	let count = 0;
	
	function handleClick() {
-		count += 1; $$invalidate('count', count);
+		count += 1; $$invalidate(0, count);
	}

-	return { count, handleClick };
+	return [count, handleClick];
}

class App extends SvelteComponent {
	constructor(options) {
		super();
		init(this, options, instance, create_fragment, safe_not_equal, []);
	}
}

export default App;

The advantage of the array form is that it gets us half way to bitmask-based change tracking (#1922). ctx[0] is more bytes than ctx.a though, and it would be a bit more fiddly to only turn that on in production. (I'm not sure if there are performance implications of having an array versus an object, in terms of lookups etc. Someone else might though! Hint hint @lukeed, to whom I always defer on such questions :)

Relatedly, there's some other low-hanging fruit in there like having a helper for applying className.

@Rich-Harris
Copy link
Member Author

Closed via #3945

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants