yanchith / webglutenfree

We serve your draw calls type-safe and gluten-free
https://yanchith.github.io/webglutenfree/
MIT License
8 stars 1 forks source link

Change API to be tree-shaking friendly #53

Open yanchith opened 5 years ago

yanchith commented 5 years ago

Currently rollup does not attempt to prune class methods due to the dynamic nature of the language, and it seems that even if it did, it would have to be really conservative about it (https://github.com/rollup/rollup/issues/349). This means that our current API does not tree-shake well: Device is a class with a lot of methods that utilize imports from the rest of the library.

This is what the API could look like with plain functions:

import * as gf from "webglutenfree"

const dev = gf.createDevice(); // or gf.createDevice(canvas) or gf.createDevice(gl)
const cmd = gf.createCommand(dev, vertexShader, fragmentShader, options);
const attrs = gf.createAttributes(dev, primitive, attributeData);
gf.target(dev, (rt) => {
    gf.draw(rt, cmd, attrs);
});

We could potentially keep methods that don't make use of imports, or move everything into functions for more consistency.

yanchith commented 5 years ago

I did some experiments with a really trivial hello world app (basically the triangle example), here are the build size results:

Without tree-shaking optimized API (current master)

125k webglutenfree-app.prod.js
40k  webglutenfree-app.prod.min.js

With tree-shaking optimized API (https://github.com/yanchith/webglutenfree/tree/tree-shakable-api)

84k  webglutenfree-app-treeshake-opt.prod.js
28k  webglutenfree-app-treeshake-opt.prod.min.js

The tree-shaking optimized version is roughly 67% to 70% of the current size, making this desirable to have.

Since the change would be a major API change and the new API would have to be either less straightforward (some operations would be functions, some methods), or less simple (making every operation a function would make it harder to tell which functions are associated with what data types), I won't pursue this for 0.1.0, but may possibly revisit later.