We recently completed an intense multi week consulting engagement where one of our largest customers invited us to help them improve performance of their application, one that had our Flexicious Ultimate DataGrid as the core of their product.

Their initial pain point was the amount of time it took for the grid to initialize, and then to scroll. We had made tremendous improvements in performance over the past few releases, but even with the last official release, they were seeing times > 10 seconds to scroll through a few hundred records (using the scroll bar, keeping the mouse down on the thumb track towards the bottom).

Here were the causes, and the work we did to alleviate them:

1)      Use of ITem Renderers : Most experienced Flex developers know that item renderers abuse can lead to performance problems. The default cell class used by our Flexicious Products (as well as the Adobe SDK datagrids) is a very lightweight class that inherits from the Flash Sprite object [FlexSprite]. When you use item renderers, you will see something like

<fx:Component>

<s:GridItemRenderer>
<s:Label color="blue" fontWeight="bold" text="{data.Name}"/>

<s:CheckBox/>                

</s:GridItemRenderer>

</fx:Component>

 

      In a vast majority of the cases, this will be fine – but – where this starts to cause a problem is this – imagine you have a grid with 20 columns – one that takes up a large amount of your screen (as many grids do) – so you are displaying lets say 50 rows on the screen – and you use item renderers like this on 5 columns – suddenly, you go from 1000 lightweight sprites to 1000 lightweight sprites + 250 VGroups+ 250 checkboxes+ 250 Labels. Now, the numbers aren’t so bad, until you really think about what a GridItemRenderer, or Label/CheckBox are. If you look at the inheritance hierarchy of a GridItemRenderer Inheritance Group Inheritance GroupBase Inheritance UIComponent Inheritance FlexSprite Inheritance, you can see that just using a GridItemRenderer adds the weight of UIComponent, GroupBase, Group on top of the cell. Even worse, is that with Flex 4 [See the Flex 4 version problems below] – because of Skinning, each component is actually 2 UIComponents – one for the actual component, and another UIComponent for the Skin. In fact, this can be so bad that simply adding ONE renderer to a large screen grid [Not just Flexicious Grid, even the SDK grids] will make it slightly sluggish, and the problem will worsen with more renderers.  To demonstrate this problem, we took a basic Spark DataGrid – added 20 columns to it, and scrolled – below are the results without any renderers-

And here are results with just 3 renderers:

 

You can see that just adding 3 out of 20 columns to have renderers makes the grid render 3 times slower!! And the renderers don’t even do anything

<columns:ExtendedGridColumn dataField="Name">

<columns:itemRenderer>

<fx:Component>

<s:GridItemRenderer>
<s:Label color="blue" fontWeight="bold" text="{data.Name}"/>

</s:GridItemRenderer>

</fx:Component>

</columns:itemRenderer>

</columns:ExtendedGridColumn >

<columns:ExtendedGridColumn dataField="Name">

<columns:itemRenderer>

<fx:Component>

<s:GridItemRenderer>                                 

<s:CheckBox/>                

</s:GridItemRenderer>

</fx:Component>

</columns:itemRenderer>

</columns:ExtendedGridColumn >

<columns:ExtendedGridColumn dataField="Name">

<columns:itemRenderer>

<fx:Component>

<s:GridItemRenderer>

<s:Label color="blue" fontWeight="bold" text="{data.Name}"/>

</s:GridItemRenderer>

</fx:Component>

</columns:itemRenderer>

</columns:ExtendedGridColumn>

 

            Remediation: Use Data Cell Renderer & Custom Background Draw Functions:

A little known feature of the product is the ability to define custom logic in the cell WITHOUT the use of itemRenderers – the dataCellRenderer. Conceptually, they are quite similar to itemRenderers – except itemRenderers sit INSIDE the cell, but dataCellRenderers ARE the cell. For use cases where you just want to change backgrounds, borders, colors of the text, or other attributes of the cell, all you have to do is to extend the lightweight base class FlexDataGridCell3 [or in the case of 4.0 you could use FDC5 but that’s for a later post]. What this does, is that it let use just use the TextField primitive that sits inside the cell. Below is an example of how this works, and a screenshot of the result – basically you have complete control over the cell without the use of item renderers. Use of this technique alone was responsible for 70 percent of the gains we were able to achieve.

 

 

2)    Use of Flexicious properties that automatically use item renderer equivalents – This turned out to be an interesting find. As you are aware, one of our strongest selling points is the massive number of features we ship with – but as you can imagine, every time you use a feature, the grid has to do more work. The more features you use, more work has to be done. But, what we found surprising was how often these features are used, and how heavy some of these can be in “perfect storm” type situations. In this case, the customer turned on the “truncateToFit” flag on all columns. When you do this, we basically swap out our lightweight cell, that does not have truncation support and use the Flex SDK text instead which has built in truncation support. We did not expect this flag to be used on every single column. [This has been changed in the 4.0 release – we have an optimized routine for this now]- The act of truncation in itself is expensive – the code has to measure text on basis of font sizes, figure out where to put the ellipsis, make adjustments for spaces, etc. Another culprit was the enableIcon property – the basic lightweight cell has no support for icons [Edit – this has also been changed for 4.0 – we have added basic icon support to the lightweight cell] – so we plug in the heavier UIComponent based cell that gets you a lot of these features. The problem is that you cannot “pick and choose” what you want from UIComponent – you have to either take the whole thing or nothing at all. So even if we need a small piece for styling/positioning, we would need to inherit from UIComponent with all of its weight – UIComponent also implements interfaces that the SDK forces us to implement for styling/rendering etc. Bottomline, all of these features turned on explained why even when the customer removed all of their renderers, the grid was still slow – the grid was still using the equivalent of renderers because of the features that were turned on. As of this writing unless you are using the upcoming 4.0 build, the following features prevent us from using the lightweight cells:

                                                              i.      truncateToFit [Fixed in 4.0]

                                                            ii.      selectable [Fixed in 4.0]

                                                          iii.      useHandCursor [Fixed in 4.0]

                                                          iv.      useUnderLine [Fixed in 4.0]

                                                            v.      wordWrap [Fixed in 4.0]

                                                          vi.      itemRenderer [C   onsider dataCellRenderer]

                                                        vii.      enableExpandCollapseIcon

                                                      viii.      enableIcon [Fixed in 4.0 – as long as icon is an embedded asset] 

Remediation:  to most of these requirements is to upgrade to 4.0, which adds these features to the lightweight cell in an optimized manner.

 

3)      As a corollary to #2, here are a few more properties that might affect performance, and what you can do to help:

a.       columnWidthMode = fitToContent. When you do this, the grid has to go through all the data, measure the text for each row, figure out which is the largest and set the columns width. If you are using an older version of our product, there was no limit on how many items we would go through. Later versions added columnWidthModeFitToContentSampleSize, so this should only be a problem if you have not upgraded in a while.

                                                              i.      Remediation: Upgrade to the latest version or do not use this property for larger datasets.

b.      filterComboBoxDataProviderBuildFromGrid – If you use multiselectcombobox or combobox as a filter control, the grid has a property called filterComboBoxBuildFromDataProvider. Majority of you set this to true. That is OK for smaller data sets. But for thousands of rows, the grid has to iterate through each row to identify unique values to display in the combobox.

                                                              i.      Remediation: Use filterComboBoxDataProvider to manually provide this list, preferably generate it on the server using a “select distinct”. If you are using server paging, you’d have to do this regardless because the grid wont have all the data to get the list.

c.       footerOperation: If you have a large number of numeric columns that each do their own footerOperation which is not a count – you should consider caching these calculations. footerLabelFunctions are called everytime filters are run – and there is an iteration loop for each column with a footer operation.

                                                              i.      Remediation: On dataProvider set and filter change, loop through the data provider once, performing all your calculations and then use the cached calculation result in footerLabelFunction.

d.      initialSortField: Sorting in Actionscript can be dreadfully slow for large datasets (especially case insensitive string sorting).

                                                              i.      Remediation: Instead of setting initialSortField, try to add the order by clause to your SQL statement on the server itself. Use Flash builder profiler to see how much time you are spending in Array.sort. 

 

4)      TLF (FTE) based text: This one surprised us. As most of you are aware, in Flex 4, Adobe introduced a new Text Layout Framework (TLF) based on the Flash Player Flash Text Engine (FTE). While this was a great addition for pixel perfect typography, it came at a cost in terms of performance. Here is a blog with some more issues with TLF - http://blog.gskinner.com/archives/2010/07/some_thoughts_o.html.

a.       Remediation - if you are using enableFTEDataRenderer, consider not using it, and just use embeddedFonts with mx.

5)      The version of the SDK – It is well known among the Flex community that Flex 4 components are much slower than Flex 3 components. Here are a few blog posts talking about this very issue : http://blogs.adobe.com/aharui/2011/04/migratory-foul-performance-problems-migrating-from-flex-3-x-to-flex-4-x.html & http://jackviers.blogspot.com/2011/03/flex-3-vs-flex-4a-performance.html Another issue, is that most Spark components do not implement IDataRenderer like most MX controls do – so you HAVE to wrap them inside a <MXDataGridItemRenderer> or <MXAdvancedDataGridItemRenderer> or <s:GridItemRenderer> before you can use them in our SDK extensions [Not Ultimate]. This in itself makes them twice as heavy – because the above three classes themselves inherit from Group. On top of that, Spark components each come with their own skin – so when you use a Button in Flex 4 based Flexicious Grid, you are now making it multiple times heavier than the same thing would have been in Flex 3..

a.       Remediation : Consider using dataCellRenderers, that still lets you use Flash player primitives for many real life business case scenarios.

6)      Not turning on enableEagerDraw – This is applicable for grids that wait for data. If you know what columns you are going to have (i.e. you are not building columns from the result of a server call) and you are not building a lot of other stuff along with the gird - you should set enableEagerDraw to true. By default, the grid will not do anything until the dataProvider is set. By setting columns, and setting enableEagerDraw to true, you are essentially breaking the grids’ initial render into 2 phases, the chrome build phase, and the data build phase. If you specify columns and set enableEagerDraw to true, the grid will draw the header, footer, filter, pager and then wait until you set the dataprovider to build the body. In this case, the grid was idling wasting time until the data came back and then doing all the work. We changed it so that it sent the server request, and while the server was processing, because of enableEagerDraw, the grid would draw the entire chrome and then build the body once the data came back.

a.       Remediation : For grids that wait for data and columns are known upfront, set enableEagerDraw to true. You can even set showSpinnerOnFilterPageSort to true and the grid will show a loading spinner while it waits for data.

7)      An older version of the Flexicious product: This particular customer was on 3.xx version of our product. 4.0 makes significant internal improvements, particularly in terms of caching information used to paint cells. It also adds more features to the lightweight class that can be turned on or off without having to inherit from UIComponent. In addition, 4.0 introduces the concept of “background sprites”. For grids that do not draw jagged cells, it uses a single sprite to draw the border and background for all cells.

a.       Remediation: Please upgrade to the latest 4.0 release.

 

There were certain factors that were specific to this application that made the problem worse:

1)      The grid took up over 90% of the applications screen real estate – nothing can be done about this, it’s just the way the application is laid out and the users wanted this.

2)      This was an internal application used by a team of folks who had large monitors with high resolution

3)      This application consumed live updates, and needed the grid to rebuild multiple times in response to rapid inserts and updates. We were able to avoid causing the rebuild and instead used the subscription information to internally manipulate the grid’s index.

4)      We isolated some memory leaks – specifically certain animations that were never destroyed even though were not visible – taking up valuable processing cycles per frame.

 

In conclusion,

Performance is one of those things that are so unique to each situation. While we can provide some general guidelines, it is incredibly difficult to optimize everything in every situation. As library/framework developers, we are always torn between two often competing aspects – convenience (ease of use) and performance. There is a famous quote in computer science – “Premature optimization is the root of all evil”. Optimizing performance is more of an art than a science. It involves careful analysis of what efforts will get you the most bang for your buck, it involves discussions with stakeholders as to what features are absolutely critical vs. a “nice to have”. After a certain number of iterations of improving performance, you hit the law of diminishing returns, where the effort involved does not warrant the miniscule gains, so this has to be a collaborative exercise to achieve the most optimal results. We routinely engage with customers in initiatives like this, and are able to help them get to a state where they are able to deliver value to the business in a way that is immediately noticeable. Please contact us to explore how we can help you as well!

 

Update: 4.0 is available now!!!

Most of the changes for 4.0 are focused on performance. To that end, the only major API change is ExtendedUIUtils.

ExtendedUIUtils.enableV4Optimizations

 

  • This flag is set to true - You can use this flag to turn off the optimizations in case they are interfering with anything. But please reach out to us with a small test case.
  • We have introduced a new flag, enableTFDataRenderer - this is currently in beta state. It does not work for nested grids and it does not work with row span and colspan. 
  • But for pure flat grids, it should increase performance even further.  
  • The following enhancements were made to FlexDataGridCell3 [which is the lightweight class introduced in 3.x]
    • Support added for truncateToFit
    • Support added for enableIcon
    • Support added for useHandCursor
    • Support added for wordWrap
    • Support added for selectable
    • Support added for useUnderline
    • If you had any of the above flags set to true, upgrading to 4.0 should get you a noticable increase in performance.
  • Bug Fix - cache hit misses for false values has been fixed, improving performance
  • Bug Fix - column drag drop interfering with Drag Drop Grouping issue fixed
  • Bug Fix - gotoItem did not work if you go to last row
  • Responsive behavior added 
  • Bug Fix: The datagrid is 1px bigger than the height specified by the component attribute . 
  • Bug Fix: in the ExtendedAdvancedDataGrid where you have locked columns in the grid and type into a textinput filter, the filter loses focus after the filter has been applied, had been fixed. 
  • Fix for building TransposedColumnItemRenderer in Flex 3.x
  • Fixed memory leak related to cleaning up open items when data provider changes
  • Changed signature of setEditedItemPosition to make it easier
  • Fixed issue with drag and drop enabled grids having scrolling issues with column resizing
  • Changes so that responsive behavior only kicks in once per resize
  • Added documentation for responsive behaviors/ResponsiveBehavior.js
  • Better text rendering for alive pdf printer for multiline text.
  • Updated build scripts to include new files for Responsive code
  • Some changes for native pdf/AlivePdfPrinter.as
  • Bug Fix: We have a column that can have the values as BLANK (empty string ” “), CAT, and DOG. When I select to filter on BLANK fields, only my column values that contain “ “ will filter. This part works just fine. I’m not sure why, but when I load the preference string into the grid, it does not display my saved filter to be BLANK like it should. It defaults to ALL being selected (BLANK, CAT, DOG).
  • Bug Fix: Custom filter render, com.mb.filter.DynamicFilterControl. The filter works but when we save the preference and the reapply it does not work.
  • Added support for enableBackground (for grids that dont paint a background) - set this to false if you dont have a background.
  • Bug Fix : We have server side persisted preferences.  When I go from a grouped saved preference to a non-grouped filtered preference I loose all data.This only happens in a scenario of grouped to filtered.  If I go from a grouped to sorted or some other simple preference it works correctly.
  • Bug Fix: We have a column that can have the values as BLANK (empty string ” “), CAT, and DOG. when I load the preference string into the grid, it does not display my saved filter to be BLANK like it should. 
  • Bug Fix: Fixed issue where Print and PDF of grids with horizontal scroll policy auto but columns not wide enough causing last column to stretch cause weird appearance in the print view.
  • Bug Fix: Description:  We are no longer able to move columns whenever the Drag Bar Grouping Feature is enabled on the report.
  • Bug Fix: When we upgraded to the newest Flexicious, we no longer had the ability to persist the numeric filters when grouping preferences are set.
  • Bug Fix: Added column glyph when you are dragging columns

            Following new properties were added:

            /**

             * Flag used add column group separator line - after the last visible column of each column group a new line will be drawn to demarcate the end of that column group

             */ 

            public var enableColumnGroupSeparators:Boolean = true;

           

            /**

             * Flag used to turn on right click row selection.

             */        

            public var enableRightClickSelectRow:Boolean=true;

            /**

             * When you scroll, the grid will destroy the current item editor. If you set this to false, it wont.

             */        

 

            public var destroyEditorOnMouseDownOnGrid:Boolean=true;