chantastic.fm

18: Prepack and the Future of JavaScript Performance with Nikolai Tillmann

Episode 18 · August 28, 2018 · 00:23:07

Hosted by chantastic · With Nikolai Tillmann

Download MP3 · Jump to transcript

Chantastic talks with Nikolai about Prepack — a tool for making JavaScript code run faster. They discuss the goals and challenges before Prepack, why it makes global JavaScript faster, and how it could dramatically improve time to interactive performance in large React apps.

Featuring

Links

Transcript

[00:00:06] Sponsor: This episode of React Podcast is brought to you by ReactTraining.com. In-person, hands-on training for development teams from React community leaders and experts. Visit ReactTraining.com to learn more.

[00:00:20] Michael Chan: This is React Podcast, and I'm Chantastic. Today I have the distinct privilege of being able to talk with Nikolai Tillmann. He's an engineer at Facebook on the developer infrastructure team. Today we're specifically talking about Prepack, a new project that does some pretty awesome optimizations to your JavaScript. Before we talk about Prepack and the things you're working on at Facebook, tell me a little bit about yourself. How you got into uh programming and engineering.

[00:00:59] Nikolai Tillmann: So before Facebook, I joined Facebook almost 2 years ago, I was uh at Microsoft since 2001. So um I was always interested in in compiler technologies from the first time when I started programming in in my teens. So at Microsoft, I was for more than a decade in Microsoft Research in in a team that was working on different kinds of program analysis tools, program verification tools, program testing tools. And it's it's kind of interesting that since a long time ago, I've kind of kind of worked on technologies that are kind of related to what what Prepack is now. One project I was working on at Microsoft Research was called PEX, Program Exploration, which eventually shipped as IntelliTest in Visual Studio. If you right-click on a function, In in Visual Studio, that is in C#, there's a button IntelliTest. It generates, when you click on it, a test suite of of different inputs that exercise all of the different corner cases in the code. So that uses a technology called symbolic execution and a constraint solver that figures out what values makes a program do different things. So that was a really fun project I started a long time ago. I went looking at this kind of technology and and we found different uses. So another project at Microsoft Research was building a tracing JIT for .NET, which used a similar idea of symbolic execution and constraint solving. But instead of finding test cases, it used it to to figure out if any branches in the code or in in a trace, a path, are infeasible, and then we can remove them and the program will run faster. So that's the basic idea behind the tracing JIT, and and we took that to an extreme. That's what we did in uh Microsoft Research. That's why it's called research. Not very useful in practice, but really nice exploration.

[00:02:57] Michael Chan: So it sounds like a lot of your experience has been in this type of uh optimization then?

[00:03:02] Nikolai Tillmann: Yes, and generally analyzing programs.

[00:03:04] Michael Chan: So tell me a little bit about the team you're on um at Facebook, the uh developer infrastructure team.

[00:03:11] Nikolai Tillmann: Yeah, um, so there are many different aspects to this division in Facebook. So um, where I'm in, it's it's a group called Programming Languages and Runtimes. So Facebook really has a surprising number, at least I found it surprising when I joined, of language and runtime projects going on. So the obvious ones are um Hack and HHVM, which is Facebook's typed version of PHP. So so one other part of this um programming languages and runtimes team is concerned with how to best use JavaScript in our Facebook mobile apps. So there's obviously uh React Native as a very popular framework, and there are some other similar techniques. And running them on mobile devices really brings its own unique set of challenges, particularly when it comes to performance and memory overhead. So so we have an entire team, and I'm part of that, just trying to make JavaScript development for mobile really, really awesome.

[00:04:11] Michael Chan: Now, I know that ReasonML is kind of gaining a lot of popularity. Does that kind of fit into that same jurisdiction of developer infrastructure and language development?

[00:04:21] Nikolai Tillmann: It's definitely one of those fun, exciting language projects at Facebook, and there are many more of it. Um, so many of these projects really span different divisions. So When it comes to React and Reason and JavaScript on mobile, a big factor is is um the UI aspect of these frameworks. So all of these projects typically span um between our product groups that are more UI-focused and um our infrastructure teams, which is really, really good at um building compilers and efficient runtimes.

[00:04:58] Michael Chan: So now, after your time working in languages like uh C#, sharp, I mean, did you imagine that you'd be spending so much time trying to optimize JavaScript?

[00:05:06] Nikolai Tillmann: That's why I came to Facebook. Okay. Yeah, so the prospect of of um working on something like prepack, I found it really exciting, especially being a Facebook engineer. It's it's really everything at Facebook is very um data-driven, and it's it's about shipping and and getting feedback from production. And and that was really Very exciting prospect.

[00:05:32] Michael Chan: So tell me a little bit about Prepack. What's the the elevator pitch for Prepack?

[00:05:38] Nikolai Tillmann: So Prepack is a project that makes your JavaScript code run faster. Prepack takes JavaScript code, deeply analyzes it, and produces a new version of the JavaScript code that is much more efficient. So there are many frameworks out there rewriting JavaScript code for different purposes. So there's Babel, which has lots of plugins to lower JavaScript code to run in older browsers. Um, there's Uglify, Minifier, whose job in life it is to make JavaScript code syntactically really, really small. And so, Prepack is kind of similar in spirit, except that its purpose in life is to make code really, really fast. Um, the way it does it is is a little bit surprising. Prepack, in a sense, actually runs the JavaScript code on on your laptop or your build server. And prepack is looking at the final heap that comes out of whatever computations are going on. And then prepack walks that tree and generates new code that reconstructs that heap, but without any computations, no intermediate objects or or other computations.

[00:06:49] Michael Chan: Interesting. Um, where does this fit in into those existing tools? So you so you mentioned Babel, a lot of people are using Babel. So if you're using Webpack, which seems to be a pretty popular modular bundler, um does this happen inside of um inside like the Babel phase, inside the Webpack phase, or is this something that happens after all of that?

[00:07:10] Nikolai Tillmann: So it would typically run after, but this is um debatable, and it it really depends on also where packages are moving. So uh one way of Applying prepack is to first use a bundler, you get a giant JavaScript file, and then you give that to prepack. Prepack will run what we call the global code in in that bundle and and rewrite the entire bundle. So prepack runs the global code of the bundle and and rewrites it. Um so that is kind of the default mode of prepack and how the project really got started, optimizing the global code in a bundle. But prepack can do more. Prepack can optimize any individual functions uh in the bundle if if if you want to. So for example, render functions in React can also be optimized. By default, prepack only analyzes the global code where it has perfect knowledge, but then you can tell prepack to also analyze additional functions. So there's a built-in primitive You give it a function name, and when prepack runs the global code, it will discover that and then also look at that function that you named. And there's something similar for React elements. Prepack would assume that these functions do not depend on any other program state, and then prepack can can analyze and optimize these functions in in addition to the global code. And it it can do that at different levels. So if you think of render functions, render functions call other render functions and so on. And then prepack can look at all of these functions and inline them all if it wants to, or possibly not inline some if that that would lead to some kind of code explosion.

[00:09:02] Michael Chan: So ideally, this would be kind of the answer to a lot of the performance concerns around doing certain types of styles in React where you have these anonymous functions, which render things out, like that renderProps pattern.

[00:09:15] Nikolai Tillmann: Exactly. So prepack gets rid of any kind of intermediate computation. And if if you drive this to an extreme, then um prepack will will see how everything gets converted to HTML elements. And then at the very end, when everything has been inlined, you you basically get some precomputed HTML code. Now, something where prepac really gets interesting is when things are not all static, because that's kind of boring, right? I mean, the Fibonacci example is very static. You're asking for Fibonacci of 15. Um, but but in real programs or React programs, there's user data. There's other data, like I could call dateNow, and what value is that? We don't know until we run it later, and and you probably don't want to pick up the current time on on your laptop when you build your code. So when that happens, prepack uh has a built-in symbolic execution engine, and it's able to track how these unknown values are used by the program. And then instead of being able to um statically compute that 3 4 is 7, prepack would build up um expressions that prepack will embed in the generated program to compute those environment-dependent values later at runtime.

[00:10:39] Michael Chan: So even things that are dynamic, that are gonna depend on user data and things that are happening on the user's computer, that stuff can still be optimized, uh even though at the at the build time you have no idea what those values are gonna be.

[00:10:51] Nikolai Tillmann: Right. So so prepack figures out everything that is statically known. And folds all that together, separating it from what's only going to be known dynamically.

[00:11:02] Michael Chan: That's amazing. That sounds uh that sounds pretty uh pretty complicated.

[00:11:06] Nikolai Tillmann: It's quite fun, yes.

[00:11:09] Michael Chan: So um right at the top of the the the project page, uh there's a kind of a little asterisk and it says uh Prepack is still in early development stage and not ready for production use just yet. Please try it out, give feedback, and help fix bugs. What are some of the unknowns at this point? It sounds like you have a very clear direction in what optimizations are made, but um what are some of the things that are still unknown that are being actively developed to get that production ready?

[00:11:39] Nikolai Tillmann: So what what we focused on first is analyzing global code, which is all of the code that runs initially, not yet render functions, those are essentially callbacks, but all of the code that Um, installs polyfills and and defines some kind of module system and any other kind of initializations you do. So prepack works really well on that, and and I think you could start using it for production. Um, the main challenge is that even for that global code, you need to tell prepack a little bit about the environment where this code runs in. So in a framework like React Native, the environment is actually somewhat well known. You know what kind of JavaScript engine is is running on the phone. It's it's probably not the Internet Explorer.

[00:12:29] Michael Chan: Mm-hm.

[00:12:30] Nikolai Tillmann: So teaching prepack about the environment there is is some work. We have done that internally at Facebook for our targets.

[00:12:37] Michael Chan: Mm-hm.

[00:12:37] Nikolai Tillmann: Where where it gets more tricky is when it comes to the web, where you have various kinds of JavaScript engines and browsers, and and you don't know where this code is running in. Uh, what polyfills kick in. And then there are a lot of unknowns for prepack, which may make it a bit difficult to configure. So there's that kind of split between mobile and and the web. For for global code, it works pretty well. What we are still actively developing is basically techniques like folding render functions for React. This is a whole new feature area that we only started working on um half a year ago or so. And this really takes prepack to a next level where prepack has to do its analysis in kind of a recursive fashion, right, because you have recursive render functions. And if prepack before was already a bit uh rocket science, then now it becomes so in a recursive sense. So we are working out the kinks and and making everything recursive in that sense.

[00:13:41] Michael Chan: So it sounds like React and prepack are pretty closely aligned, at least in values. Is that by design, or is that just kind of the current needs that you have right now that you're trying to solve for?

[00:13:53] Nikolai Tillmann: That's a good question. So the React team and the prepack team, we are closely working together, so of course it will work great for React. Um, it turns out that there are more React-like frameworks internally at Facebook. So it's actually not just React, but it's really useful for for many other things as well. And If we get to implement everything that we want, then then Prepack really becomes a very powerful program analysis tool. It will work great for React, but it will also work great for any other kind of JavaScript code that you want to optimize. So our ultimate vision there is that anyone writing JavaScript code one day, when it's all fully implemented, should be optimizing their code through Prepack just as you run Uglify. Before you ship it to your customer.

[00:14:45] Michael Chan: I know that there's different types of um performance metrics that are important to different companies and developers. Uh what are the biggest ones that Prepack is focusing on?

[00:14:57] Nikolai Tillmann: So at Facebook, a really important metric to us is time to first interaction. There are a few names for that in different contexts, but it's it's all about how quickly can an experience render and become interactive for a user.

[00:15:12] Michael Chan: Mm-hmm.

[00:15:13] Nikolai Tillmann: That is is very important for people to decide how much time they want to spend in a feature. If the feature doesn't even open up, then it's pointless. So, prepack is really, really good at optimizing initialization code and code that basically has to run once to bring up an initial experience. If if you later have some tight loop that you might have already hand-optimized, then there's not much prepack can do. But but if you have a lot of code that needs to start up, maybe combining a few libraries that you picked up from random places and with some accidental complexity, just just because every little piece that you don't even know the details about needs to get initialized, then that's really where prepack shines.

[00:16:03] Michael Chan: One thing that I really value about the idea of React is that kind of early on I was really struck by the fact that it was doing a lot of performance optimization for me. You know, where typically I would try to add some type of event listener on the document, now React was kind of doing that for me. It was just binding one onclick handler for the document and then managing that for me for all of the click events that I was doing. Maybe this is a step too far, but it seems like Prepack is kind of doing a similar thing, bringing those um ideal situations to the build phase of the application to serve that time to first um interaction. Does that seem like a a decent corollary?

[00:16:42] Nikolai Tillmann: Yeah, I think that's a good comparison. Prepack automates at build time certain program optimization tasks that you could do by hand, but your time is better spent working with high-level abstractions and and have some tool come in and optimize away the overhead of doing it all again and again yourself.

[00:17:06] Michael Chan: That sounds incredibly valuable to me. You know, a lot of times I might not know how to get um something optimized the right way that I need for that time to first interaction, um but knowing that prepack could do that, it would allow me to focus. Um I'm a designer by trade, to focus on the more um important parts in dealing with my customer, um and knowing that they're gonna get a great experience, even on those um really deeply technical parts.

[00:17:30] Nikolai Tillmann: Exactly. Yeah, I've I've seen at Facebook um many people doing some kind of micro-optimization by hand. I mean, very data-driven. You you look at what takes time, you look at the code, and then basically you manually inline because Um, no JIT will be able to have certain domain knowledge to do that in in a browser. And even if, then the JIT probably has to run that code 100 times before it would even attempt to optimize it. Yeah, I've seen many people do this by hand and improve metrics, and and Prepack truly can do some of these tasks automatically. So ideally, if we succeed, then it it really change the way people can write code, I I want you to to to use abstractions. The more abstractions you use, the better for everyone, including for for the Prefect team.

[00:18:21] Michael Chan: Cool. So what does adoption look like at this point? Are you using it for code in the production Facebook app?

[00:18:28] Nikolai Tillmann: Um, it's being used. We look at it in in different applications in different ways. What what I talked about, to uh fold render functions, that's still under active development. And and that is not yet in any production setting.

[00:18:43] Michael Chan: Cool. Well, I I am for one eager to uh to play around with that. What's the best way to kind of get some experience with the uh project? I think that you'd mentioned that that there's a a REPL available.

[00:18:55] Nikolai Tillmann: Right. Um, if you go to our website, prepack.io, uh this has some background information and and there's the REPL, where you can just type in JavaScript code. And since prepack is implemented in JavaScript, all of the program optimizations happen in your browser and you you get shown the optimized code uh on the right.

[00:19:16] Michael Chan: Oh, awesome.

[00:19:17] Nikolai Tillmann: So I think that's a great way to kind of get an idea of what prepack can do and also where it fails. Prewpack itself is open source on on GitHub. So if you find some aspects interesting, I would love to see you as a contributor and and try yourself out to see if if you can work on an optimizing compiler for JavaScript.

[00:19:38] Michael Chan: Yeah, I I I hope that a number of our listeners are uh interested in this and can help out. Are there good first issues, and is there a direction for feedback that you're uh you're looking for at this phase in the project?

[00:19:50] Nikolai Tillmann: So we always have some um somewhat easy starter tasks that one can do to to help the project and and move prepack forward. To to really use prepack in a production environment, you you have to model your environment for prepack and That's a bit of work, but if if you have a JavaScript function that is doing some computation that you want to have optimized, you you can try that out, just run it through prepack, and if you run into any issues, then we we would be very interested in in hearing about your feedback. If you find any interesting examples of what prepack can optimize, we would love to include that in our list of examples. We we've put out a few, and then internally at Facebook, we we have some very substantial JavaScript programs that we run through prepack.

[00:20:41] Michael Chan: Awesome. So what are some projects that you're really excited about right now?

[00:20:46] Nikolai Tillmann: So I've been really deeply involved in in in prepack and didn't have the time to look too much around. I'm excited about all projects that treat performance um not just as a second thought, but but really are built from the beginning with performance in mind, like the uh Inferno project and other JavaScript UI frameworks that that employ some kind of ahead-of-time compilation. So I think that's that's really what it will take to make JavaScript UI code um awesome.

[00:21:18] Michael Chan: Awesome. Uh is there anyone that you want to give a shout out to in terms of um working on the prepack team that that we weren't able to talk to today?

[00:21:26] Nikolai Tillmann: Well, a big shout out to the entire prepack team. I'm just one person on it. So certainly, Sebastian Markbåge, who is full of ideas and basically started the whole prepack project. Dominic Genovese right now is is pushing ahead with the whole React compiler idea and and folding React elements. He's he's amazing. And um there are many more people actively contributing. Herman Wenter is building the abstract interpreter for Prepack in JavaScript. And and Chris Blappert in in Seattle is um has also been working very hard for a while to uh bring Prepack to production in Facebook and build out abstract interpretation facilities.

[00:22:14] Michael Chan: Awesome. So how can everyone follow you and um the developments of the project?

[00:22:19] Nikolai Tillmann: So everything we do is is public on GitHub. GitHub is the truth. So if if you stay up to date. On GitHub, that's the best way.

[00:22:27] Michael Chan: Cool. Well, thank you so much for your time today, Nikolaj. I really appreciate you coming on the show and uh telling us um what we have to look forward to in uh in Prepack, uh how it's being developed, uh who's involved, and what some of the future goals are. I really appreciate all that you shared with us, and uh I uh look forward to seeing what you make.

[00:22:47] Nikolai Tillmann: Thank you. It was great to be on the show.