Rendering Performance

I am currently testing on older devices and noticed that forced layout reflows cause significant performance bottlenecks, especially on forms with a large number of components.

To mitigate this issue, I overrode the form's Init procedure to call BeginUpdate, with the corresponding EndUpdate placed in the OnShow event. However, this approach proved insufficient because components inheriting from TComponent perform operations that force a reflow directly within their visual property handlers, regardless of the BeginUpdate/EndUpdate block.

Specifically, routines such as AlignControl, DoRealign, Realign, and SetParent and many others, trigger reflows, particularly when they:

  • Read properties like getBoundingClientRect, offsetHeight, or offsetWidth;

  • Assign or read dimensions programmatically.

A concrete and particularly indicative example is TCustomLabel.UpdateElementVisual (in WEBLib.StdCtrls). It reads offsetHeight/offsetWidth on every label refresh (e.g., when changing color, font, alignment, etc.), forcing a synchronous reflow on every single call. Looking at the implementation, these two values are used exclusively to populate FOldHeight/FOldWidth, which are only read by GetHeight/GetWidth as a fallback during design-time (csDesigning) when the real measurement is 0. At runtime, outside the designer, this reading has no observable effect: it is pure overhead, repeated for every label on every update. On forms with many labels (e.g., data grids), this translates to a large number of reflows that could easily be avoided with a simple conditional guard (if csDesigning in ComponentState then ...) around the read operation.

I suspect similar patterns (DOM measurements executed unconditionally but only required in specific scenarios, e.g., design-time, autosize, or non-CSS layout) exist in other routines across the framework.

In an application where responsive layout is primarily handled via CSS, many of these operations are redundant. However, omitting them entirely cannot be generalized, as some projects rely on dynamic layout via code where these reflows are actually necessary.

Therefore, I propose making this behavior configurable, for instance, via a global property at the Application or Form level:

  • CSS-Driven Layout: Layout management is fully delegated to CSS. Lightweight property assignments (Visible, Enabled, ElementClassName, ElementID, etc.) remain active, while internal operations that force reflows (reading offsetHeight/offsetWidth/getBoundingClientRect, automatic realignments, etc.) are skipped.

  • Code-Driven Layout (Current Default Behavior for Backward Compatibility): Layout continues to be managed internally by component code as it is today.

Using a crude monkey patch, I modified the routines mentioned above and tested our projects. Functionality and visual appearance remained, apparently, completely intact, while performance improved dramatically. Attached are the screenshots showing the in-browser performance analysis results before and after the changes:

BEFORE PATCHING

AFTER PATCHING

The project I'm currently working on HAS to run smoothly on older devices so having an official implementation of this feature/fix would be of immense value.

If you have the Align property on controls set to alNone for having these use the CSS layout, I woudl expect that to not have calculation impact as these controls are all skipped?

That makes sense, but while alignment procedures are skipped for alNone controls, other routines still trigger reflows.

I assumed that properties like epIgnore or epRelative would also be factored into these layout checks alongside Alignment. Extending the check to include ElementPosition would be a great improvement: it allows us to use Alignments to keep intricate forms organized at design-time without impacting runtime performance, or even to build responsive design-time components using that exact logic.

We'll consider adding an extra check for ElementPosition property