Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Use React components for lines and line numbers - #2325

Closed
nathansobo wants to merge 5 commits into
masterfrom
ns-react-use-components
Closed

nathansobo wants to merge 5 commits into
masterfrom
ns-react-use-components

Conversation

@nathansobo

Copy link
Copy Markdown
Contributor

This PR switches back to React components for lines and line numbers. There is a ~14% performance penalty at larger scroll sizes, but the code is a lot easier to reason about. I would love input from @petehunt or another member of the React team regarding improving the performance.

As you can see below, the overhead appears to be significant over the current manual DOM manipulation approach. The first part of the timeline is an accelerating scroll in two different versions of the editor, the first full react from this PR, the second with manual DOM manipulation from master. Note the larger amount of yellow JS time in the first timeline.

@petehunt

Copy link
Copy Markdown

I'll patch this in and take a look ASAP (maybe tonight, but more likely Thursday). A flame chart from Chrome's CPU profiler will help us figure out where the time is spent. I am curious how much time is spent in updateDOMAttrs() in ReactDOMComponent vs the provided shouldComponentUpdate() method. If shouldComponentUpdate() is significant than maybe the sometimes-discussed shouldChildUpdate() hook should be implemented.

Also adding in the dream team of @yungsters @jordwalke @sebmarkbage @zpao @spicyj @vjeux @syranide

@nathansobo

Copy link
Copy Markdown
Contributor Author

@petehunt Sorry, I've discovered an implementation error in my example. I should have been more careful. It's not clear that there's an issue with this new implementation now that this problem is fixed. I'll update soon.

@petehunt

Copy link
Copy Markdown

No problem. But for my info, how did you run this test case? I was able to build atom but not get the init to do anything. Also is there some sort of incremental build if I wanted to make changes to the core? Couldn't find it in the docs.

@nathansobo

Copy link
Copy Markdown
Contributor Author

Here are new timeline comparisons with the fix on React. It's still a bit slower but nowhere near as bad. I have to go to an appointment. I'll update afterward with info on building. So many thanks for taking an interest in helping with this.

react__large_scroll__mean_frame_time

manual__large_scroll__mean_frame_time

react__medium_scroll__mean_frame_time

manual__medium_scroll__mean_frame_time

@nathansobo

Copy link
Copy Markdown
Contributor Author

Sorry for the delay on this. I want to regroup based on my discovery of that error and present you with better information than I did this time around. Please forgive the noise.

@nathansobo

Copy link
Copy Markdown
Contributor Author

@petehunt So fixing the bug definitely improved things, but using React components is still a bit slower, especially at faster scroll rates. It would be fantastic to have you review my implementation and make sure there's nothing else I could do to improve things. It would also be cool for you to run the benchmark yourself as you tried to do to get a sense of how we're putting pressure on React.

Dev Mode

If I understand what you mean by "incremental build" correctly, you can run Atom in "dev mode", which will load all source code directly from a development directory so you won't need to build. Currently, you can run Atom in dev mode by placing the repository in ~/github/atom and opening a new window with cmd-shift-O (instead of cmd-o). You'll see a red box at the left side of the status bar to indicate you're in dev mode. You'll need to enable the React editor by opening config cmd-, and checking Use React Editor.

Running the benchmark

I've added the benchmark code directly into the source, you can run it from master and this branch by opening the command palette via cmd-shift-P and running Benchmark: Scroll. It will record a timeline while the dev tools are open. The benchmark is implemented at the bottom of editor-component.coffee. You can change it, for example doing a console.profile instead of a console.timeline by directly modifying this source in dev mode.

I've been running these tests on huge.js in the atom/benchmarks folder. To reproduce, you can open a new window in dev mode with cmd-shift-O and navigate to your Atom repository, then open huge.js with cmd-t. Make sure you wait for the file to fully tokenize before you start the test or the results will be skewed.

Thanks so much for taking a look at this.

@nathansobo

Copy link
Copy Markdown
Contributor Author

I forgot to add, you may want to comment out our NODE_ENV = production assignment here: https://github.com/atom/atom/blob/master/src/atom.coffee#L161 to see the ReactDefaultPerf results after the scroll benchmark.

@nathansobo

Copy link
Copy Markdown
Contributor Author

Here's an updated comparison. I modified the benchmark to scroll at a constant rate of 500px every 10ms for 2s. The version with React components from this branch is about 14% slower at this speed.

500px_scroll_timeline

At 2000 px every 10ms, they both get worse, but the version with React components is still about 14% slower.

2000px_scroll_timeline

@nathansobo

Copy link
Copy Markdown
Contributor Author

Here's a bit more info. It's hard to know what to make of these flame charts because the events they record are about the same length even though as you see in the timelines, the manual updates are faster. I've tried to outline the basic stages of what's going on in an update. The main difference is that the manual graph has a long DOM update phase and an extremely short "receive component" phase. It's interesting that React is able to update the DOM so much faster, but has more overhead for the receive component portion.

screenshot_2014-05-22_17_05_12

screenshot_2014-05-22_17_06_43

@nathansobo nathansobo changed the title For discussion: Overhead of React components for lines and line numbers Use React components for lines and line numbers May 23, 2014
@syranide

Copy link
Copy Markdown

@nathansobo When I sum up your flamegraphs it appears that React is basically equally fast, but are some unmarked stuff at the edges I guess that might be it.

@spicyj @vjeux Posted a gist yesterday which seemed to indicate that "dumb React-components" have significantly higher overhead over just plain inlining. So there's hopefully some performance to be squeezed out there. But it made me a bit curious, have you tried inlining LineNumberComponent and see how that affects performance? It's possible that the added cost of updating all line numbers is offset by the lack of React overhead. Another possibility would be to group line numbers 5 and 5 or something similar, which might offset the overhead.

@nathansobo

Copy link
Copy Markdown
Contributor Author

@syranide Yeah the flame graphs are really confusing because they appear to take the same time, but that conflicts with the timeline info which shows React components being slower. That's a really interesting idea about inlining the line numbers. I will give that a try and post the results.

@nathansobo

Copy link
Copy Markdown
Contributor Author

@syranide I tried to inline everything with some caching for the generation of the innerHTML of the lines, which would be expensive to repeat. It's here: https://github.com/atom/atom/tree/ns-react-inline-lines.

Here is a timeline of all 3 versions scrolling 500px/10ms for 1s and 2000px/10ms for 1s. It didn't seem to make a substantial difference.

screenshot_2014-05-23_15_40_12

@syranide

Copy link
Copy Markdown

@nathansobo Seeing as it at least seems to measurably improve performance (even though it technically shouldn't), if it doesn't complicate things unnecessarily for you. It seems to me that if you put the line numbers 5 and 5 (or so) in a component, that you should be able to get it down further, less overhead from React classes than the initial setup and less overhead from redundant descriptors from the inline setup (if that is an issue, which it should be).

@nathansobo

Copy link
Copy Markdown
Contributor Author

@syranide Yeah, you're right that it is a bit faster, but I'm not sure it's enough of an improvement to switch to it over the manual DOM manipulation approach. I'm tempted to use components for the simplicity of the code, but a fast user experience may trump clean code in this case. After playing with grouping lines a couple weeks ago, I'm not sure it's worth it. It seemed to increase layout time and was also pretty unwieldy to deal with when there were changes to the buffer.

@syranide

Copy link
Copy Markdown

@nathansobo I guess the simplest implementation would be Gutter -> LineNumberGroup -> LinerNumberComponent. That is, just group your existing components. Alternatively, inline LineNumberComponent inside LineNumberGroup and allow each group to grow and shrink to accommodate the requested number of lines (i.e, not only in batches of 5). But you're probably right in that it won't significantly improve performance.

I'm working on #1570 and you might see a measurable improvement from it whenever it lands (and subsequent PRs), but I doubt it (at least short-term).

@probablycorey

Copy link
Copy Markdown

This branch is pretty out of date, should we close this @nathansobo?

@nathansobo

Copy link
Copy Markdown
Contributor Author

Yeah I'm resigned to manual DOM manipulation for now.

@nathansobo nathansobo closed this Jul 13, 2014
@kevinsawicki
kevinsawicki deleted the ns-react-use-components branch August 14, 2014 23:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants