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

Even faster flexbox #122

Merged
merged 11 commits into from
Sep 10, 2015
Merged

Even faster flexbox #122

merged 11 commits into from
Sep 10, 2015

Conversation

lucasr
Copy link
Contributor

@lucasr lucasr commented Sep 10, 2015

This branch has a series of changes on top #121 that makes css-layout an extra 15% faster in my benchmarks on Android.

Besides a couple of micro-optimizations, this branch implements an optimization for simple stacked layouts (vertical/horizontal layouts with non-flexible children aligned with STRETCH/FLEX_START) which are very common in most UIs. This change allows us to skip Loop C and D (main and cross axis, respectively) partially and even completely in many cases.

Method invocations are not entirely free on Android. Change the
generated Java code to use the same array-based approach used in
JS and C to compute dimensions, positions, etc instead of relying
too heavily on method invovations. As a bonus, the Java transpiler
becomes a lot simpler because the code is more analogous to the C
counterpart.

In my local benchmarks this change gives us a major performance
boost on Android (between 15% and 30%) depending on the device
and the runtime (Dalvik|Art).
Store immutable values from the node being laid out to avoid unnecessary
method invocations during layout calculation. This gives us a 3%-5%
performance boost in my benchmarks on Android.
There's no need to set the trailing position on left-to-right layouts
as the nodes will already have what we need (x, y, width, and height).
This means we still have an extra cost for reversed layout directions
but they are not as common as LTR ones.
There's no need to go through all children before starting the main line loop
as we'll visit all children in the former loop anyway. This diff merges the
pre-fill loop into the main line one to avoid an extraneous traversal on the
node's children.
There's no need to go through all absolute children at the end of the
layout calculation if the node at hand doesn't have any. This also
ensures only absolutely positioned children are traversed in the final
loop.
We were traversing all children to only perform calculations/changes to
flexible children in order to avoid new allocations during layout. This
diff ensures we only visit flexible children during layout calculations
if any are present. We accomplish this by keeping a private linked list
of flexible children.
No need to check for RELATIVE position when position type is ABSOLUTE
and dimension is set.
Remaining space and between space only need to be updated when
justifyContent is not FLEX_START.
The correct check is on the layout state, no the style.
Change the initial line loop to opportunistically position children in
the in container with simple stacking params i.e. vertical/horizontal
stacking on non-flexible STRETCH/FLEX_START aligned. This allows us to
skip the main and cross axis loops (Loop C and D, respectively)
partially and even completely in many common scenarios.

In my benchamrks, this gives us about ~15% performance win in many
setups.
vjeux added a commit that referenced this pull request Sep 10, 2015
@vjeux vjeux merged commit 2120285 into facebook:master Sep 10, 2015
@lucasr lucasr mentioned this pull request Sep 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants