reapplyCss() Is called too many times when adding a node hierarchy to the Scene

Scott Palmer swpalmer at
Sat Mar 12 01:06:40 UTC 2016


Please let me know if you find any potential workarounds.

In my application I had purposely constructed the entire hierarchy before adding it to the Scene to avoid triggering multiple layout passes, but I think that may have also made this CSS issue a bit worse.  I did get a ~20% improvement in response time when updating my UI though.  In my UI it is possible that on the order of thousands of controls are added or removed from the Scene in response to the user selecting an item. (It’s not usually that many, but sometimes it is.)  The delay before the UI updates can be several seconds.



> On Mar 11, 2016, at 5:33 PM, David Grieve <david.grieve at> wrote:
> There is (was?) some code in Node or Parent that was supposed to prevent this.
> Namely, I thought I had it so CSS was applied from the parent before applying to the children.
> It may also depend on how you add the nodes to the scene.
> On 3/11/16 5:18 PM, Scott Palmer wrote:
>> I think I've discovered a significant performance issue with CSS
>> processing...
>> Adding a Node hierarchy to a Scene leads to
>> javafx.scene.node.setScenes(Scene, SubScene) being called.  This leads a
>> walk down the Node hierarchy like so:
>> setScenes
>>  -> invalidatedScenes
>>      -> scenesChanged
>>          -> setScenes    // on child Nodes -- recursion
>>      -> impl_reapplyCSS()
>>          ->reapplyCss()
>>             loops over children calling reapplyCss() on each - recursive
>> The thing to note there is:
>> * invalidatedScenes decides if it is going to call impl_reapplyCSS()
>> * invalidatedScenes calls scenesChanged which calls setScenes on all
>> children which leads to invalidateScenes being called recursively on the
>> node hierarchy
>> * invalidatedScenes calls impl_reapplyCSS()
>> As the setScene call walks down the node hierarchy it gets to a leaf node
>> and then calls impl_reapplyCSS() which will call reapplyCss() recursively.
>> Since when reapplyCss() returns the cssFlag for the node will be set back
>> to UPDATE, so when the parent calls impl_reapplyCSS()  the CSS is
>> recursively applied to the Nodes that just had CSS applied!
>> Am I missing something?
>> I made a quick test case where I add a VBox containing another VBox
>> containing a Rectangle to the Scene when I press a button.  I can confirm
>> that reapplyCss() is called THREE times for the Rectangle, twice for the
>> "middle" VBox, and once on the "top" VBox.
>> Obviously the deeper the hierarchy the worse this gets.
>> Scott

More information about the openjfx-dev mailing list