Skip to content

Tree shaking and optimizations - #1225

Merged
shiftkey merged 47 commits into
masterfrom
tjugondag-knut
Apr 29, 2017
Merged

shiftkey merged 47 commits into
masterfrom
tjugondag-knut

Conversation

@niik

@niik niik commented Apr 18, 2017 •

Copy link
Copy Markdown
Member

I got bored this weekend and I started working on an old side project. As I was upgrading it to use the latest and greatest technologies I got curious about how big of an impact tree shaking would have on desktop.

Before you read any further please note that this is mostly an exploratory thing and its primary purpose is to open it up for discussion, the code quality is utter shite in places.

What this does

Updates us to use webpack2

I don't think this is strictly necessary but it was what I was using in my side-project so I figured it would make sense to upgrade

Enables tree shaking

Previously we simply copied the entire node_modules directory from the app directory into the dist directory and called it a day. This approach makes it very easy to reason about modules but it has the downside of bringing along a lot of resources that we don't need (build scripts, package.json files, tests, fixtures etc). Tree shaking is a fancy word for bringing in just the code that's needed at runtime and bundling that along with the rest of the bundle.

With tree shaking enabled we only have to copy dugite, right now I explicitly copy it over from the node_modules folder since we can safely ignore all of its dependencies as they are only for install. A better approach here would be to include the dugite library in the tree shaking as well and only copy the git binaries to a known location. That would require a change to dugite so that we can pass a known location via an environment variable or something to that effect.

Dead code elimination

As part of the babili preset we get dead code elimination which, in combination with our webpack replacements completely removes things like if(__DEV__) {...}. React makes heavy use of NODE_ENV !== 'production', those checks are now all gone in release builds as well.

Parallel compillation

This PR uses parallel-webpack to speed up compilation, each bundle gets its own little web worker.

TypeScript caching

I've switched from ts-loader to awesome-typescript-loader which gives us a compile cache that allows us to bypass typescript transpiling for files that haven't changed. It also runs typechecking in a separate process which frees up cycles on the main worker.

Misc

  • Prunes localization files from momentjs (takes up lotsa space and we don't need them)

The measurements

I've only tested this on my machine running Windows so take all of these with a grain of salt. For reference my machine is pretty beefy with PCIe SSD, 32Gb ram, i7-6700HQ @ 2.60 GHz etc etc so I would expect the gains in loadtime to be higher for the majority of our users and even more so for users without fast disks as our node_modules approach ends up hitting the file system quite a bit and that's way more expensive on Windows than on Mac (where I'd expect these gains to be less grand)

Metric ⚖️ master this PR 💹
Installer size 👍 86.1 MB 78.6 MB -7.5 MB
Unpacked size 👍 216 MB 191.9 MB -24.1 MB
loadTime 👍 ~1240 ms ~780ms -460 ms
rendererReadyTime 👍 ~345 ms ~234 ms -111 ms
npm run rebuild:dev 👍 35s 27.13 -7.87 s
npm run rebuild:prod 👎 40 s 93 s +53 s

Bottom line

Enabling tree shaking, minification, and optimization would yield some reasonably good results at the cost of a pretty significantly increased build time (though only for production builds, dev builds would be faster). I would take an increase in prod build time for the gains at runtime but I'm curious to hear everyone's thoughts.

Fixes #1089, #487

@shiftkey

Copy link
Copy Markdown
Member

@joshaber

Copy link
Copy Markdown
Contributor

giphy

Those load time numbers are ✨ I'd totally take a build time increase for them.

@niik niik changed the title [WIP] [DISCUSSION] Tree shaking and optimizations Tree shaking and optimizations Apr 27, 2017
@niik niik added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Apr 27, 2017
@niik

niik commented Apr 27, 2017

Copy link
Copy Markdown
Member Author

Alright, I think this is ready for an initial look now. I'm now bundling dugite as well and keeping just the git environment external. I also had to manually copy the 7zip dependency (in dev mode) so that electron-devtools-installer could do its work.

@shiftkey shiftkey self-assigned this Apr 28, 2017
@hubot
hubot temporarily deployed to test April 28, 2017 04:39 Inactive

@shiftkey shiftkey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is really close - the test deploy of this branch is working nicely on macOS and Windows, just a couple of minor suggestions ✨.

Comment thread script/build
console.log(' Copying git environment…')
const gitDir = path.resolve(outRoot, 'git')
fs.mkdirpSync(gitDir)
fs.copySync(path.resolve(projectRoot, 'app/node_modules/dugite/git'), gitDir)

This comment was marked as spam.

This comment was marked as spam.

Comment thread package.json
"test:review": "node script/test-review",
"postinstall": "cd app && npm install && cd .. && git submodule update --recursive --init && npm run compile:tslint",
"start": "cross-env NODE_ENV=development node script/start",
"start:prod": "cross-env NODE_ENV=production node script/start",

This comment was marked as spam.

Comment thread script/start Outdated
})
}

const run = require('./run')

This comment was marked as spam.

Comment thread package.json
"build:prod": "npm run compile:prod && cross-env NODE_ENV=production node script/build",
"package": "node script/package",
"clean": "rimraf build",
"clean": "rimraf out",

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@niik

niik commented Apr 28, 2017

Copy link
Copy Markdown
Member Author

🔒

@shiftkey
shiftkey merged commit c69a1c3 into master Apr 29, 2017
@shiftkey
shiftkey deleted the tjugondag-knut branch April 29, 2017 07:11
@shiftkey shiftkey mentioned this pull request May 8, 2018
10 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review Pull Requests that are ready to be reviewed by the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants