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

Dynamic importing of CodeMirror modes at runtime #4764

Merged
merged 11 commits into from Jun 13, 2018

Conversation

niik
Copy link
Member

@niik niik commented May 23, 2018

When we initially shipped syntax highlighted diffs we supported 18 languages and formats. Since then and thanks to community contributions the list has grown to 36 with more on the way.

As the list of supported languages has grown so has the bundle size of the highlighter module and it sits at about 0.5Mb (unminimized) at the moment. Since that file has to be loaded, parsed, and initialized for each highlighter web worker we spin up (in normal usage 2 for the life time of the application) this creates unnecessary overhead when most users rarely combine more than a few languages in their day to day work.

This PR leverages chunk splitting in webpack 4 to split out each language (or technically each CodeMirror mode) into its own, separate javascript file that then gets loaded, at runtime, when that particular language mode is needed through the importScript web worker call.

This ultimately leads to less code being loaded into the runtime and faster initial highlighting. Instead of loading 0.5Mb we're now loading the bare minimum (~16kb) plus whatever mode is required.

Highlighter bundle before

screen shot 2018-05-23 at 10 54 42

Highlighter bundle after

screen shot 2018-05-23 at 10 45 44

Performance

Essentially we sacrifice a little bit (in absolute numbers) of performance whenever a new language is loaded in exchaneg for a big gain in initial load time and zero overhead of adding new language modes in the future. These timings are all taken in dev mode on my early 2015 macbook 12"

Scenario Monolith Split Delta (lower is better)
First highlighting of language 1 261ms 137ms 52%
First highlighting of language 2 7ms 23ms 328%
Second highlighting of language 1 6ms 4ms No significant change

@niik niik added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label May 23, 2018
* is idempotent and can be called multiple times with no adverse
* effect.
*/
readonly require: () => Promise<void>

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@shiftkey shiftkey added this to the 1.2.x milestone May 23, 2018
@agisilaos
Copy link
Member

@niik As I can understand this will change the way for someone to add support for a new programming language? So the docs need to be updated as well in this PR? 🤔

@say25 say25 mentioned this pull request May 23, 2018
@@ -162,6 +292,14 @@ function detectMode(request: IHighlightRequest): CodeMirror.Mode<{}> | null {
return null
}

const modeDefinition = mimeModeMap.get(mimeType)

if (!modeDefinition) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* is idempotent and can be called multiple times with no adverse
* effect.
*/
readonly require: () => Promise<void>

This comment was marked as spam.

@nerdneha nerdneha added this to To do in 1.2.next via automation May 26, 2018
@nerdneha nerdneha modified the milestones: 1.2.x, 1.2.1 May 26, 2018
@shiftkey shiftkey moved this from To do to In progress in 1.2.next May 30, 2018
@say25
Copy link
Member

say25 commented May 30, 2018

@niik and @shiftkey any status on this. There are a couple extensions I'd like to add without introducing merge conflicts and what not. No trying to be rude just wondering/asking for a rough timeline.

@shiftkey
Copy link
Member

@say25 I probably won't get a chance to start reviewing it until next week

iAmWillShepherd
iAmWillShepherd previously approved these changes Jun 7, 2018
Copy link
Contributor

@iAmWillShepherd iAmWillShepherd left a comment

Choose a reason for hiding this comment

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

Does this change how community members can add additional languages? If so, could we also get the docs updated to reflect the new way of adding language support?

@niik
Copy link
Member Author

niik commented Jun 8, 2018

@niik As I can understand this will change the way for someone to add support for a new programming language? So the docs need to be updated as well in this PR? 🤔

I don't believe we have any docs that goes into the specifics of how to add a new language. I assume that contributors who've added a theme have probably just copied and pasted a previous mode that was already in the highlighter/index.tsx and tweaked it. If someone would like to contribute more docs around this I'd be 😍 but I'm gonna say that it's out of scope for this particular PR.

@niik
Copy link
Member Author

niik commented Jun 8, 2018

Ready for another look 🙇

@iAmWillShepherd iAmWillShepherd self-assigned this Jun 12, 2018
@iAmWillShepherd iAmWillShepherd merged commit 45c7b55 into master Jun 13, 2018
1.2.next automation moved this from In progress to Done Jun 13, 2018
@iAmWillShepherd iAmWillShepherd deleted the syntactic-fission branch June 13, 2018 17:33
@saul
Copy link
Contributor

saul commented Jun 25, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Aug 24, 2018
@desktop desktop unlocked this conversation Sep 27, 2018
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
No open projects
1.2.next
  
Done
Development

Successfully merging this pull request may close these issues.

None yet