Friday, August 21, 2020

Performance improvements using Objects and array inlining

Both objects and arrays can be inlined. Object inlining has been recognized for a while and array inlining expands the concepts of inlining to arrays. When we have a group of objects and arrays, that refer to each other, the execution environment has to load them in memory for their access. This is a waste if the objects and arrays can be collocated instead, where they are placed consecutively in memory. Then the memory loads can be replaced by address arithmetic which reduces the costs of field and array accesses.

The benefit with arrays is that the size of this data structure and the number of elements is known beforehand even though it might vary from array to array. The fields referring to those arrays have to be changed whenever the array gets relocated. This can happen all under the hood without the developer having to declare any additional syntax in the language.

A code pattern that detects these changes and allows the optimized access of such array fields would then be a significant innovation to boost the performance of the program. Some have already been published.  Researchers claim that the inlining of array element objects into an array is not possible without a global data flow analysis. This pattern can be integrated into the array bounds check during compilation. A dynamic approach for array inlining can be included into the compilation with little or no analysis overhead.

Collections are frequently used in classes and they are part of standard library.  This additional layer between the business object and the array causes memory loads. Inlining reduces this overhead by placing the objects and arrays together in the heap and by replacing the memory accesses with offset address arithmetic. Object and array have headers and these are used for inlining. Dynamic arrays are given additional header fields and values. This might increase heap usage but it definitely reduces the execution time.

For example, a  Polygon class can be defined as having a java.util.ArrayList of objects  which corresponds to a dynamic list of Points. When the new elements are added and the size of the array does not suffice, the array is resized. The array elements reference the points that store the coordinates. There are two lookups for access to a point, first the loading of the field points and second the loading of the elementData. These two loads and one array access are unnecessary. Inlining combines the objects into a larger group such that a point can be loaded with a single access. 

The points reference is initialized once and the elementData can be resized many times. An intelligent technique can handle this inlining which goes above and beyond the regular inlining of fixed value objects.



No comments:

Post a Comment