RFR: 8344668: Unnecessary array allocations and copying in TextLine

Harshitha Onkar honkar at openjdk.org
Wed Dec 4 21:23:40 UTC 2024


On Thu, 21 Nov 2024 00:20:36 GMT, Daniel Gredler <dgredler at openjdk.org> wrote:

> When `LineBreakMeasurer` is used to break text into lines, internally it uses `TextMeasurer` and `TextLine` to do the job. In the common case, `TextLine.getComponents(...)` allocates a `TextLineComponent[]` with room for a single array entry, and passes it to `TextLine.createComponentsOnRun(...)`, which fills the array and returns it, optionally resizing to a larger array if necessary, after which the array is resized back down to actual size (if necessary).
> 
> Unfortunately `TextLine.createComponentsOnRun(...)` is too eager in allocating larger arrays. In the most common case of a single component, this means that a single-element array is allocated, then a 9-element array is allocated (via `expandArray(...)`), and then another single-element array is allocated (to shrink the array back to actual size). Only one array allocation is necessary in this common case, and no array copying is needed.

@gredler There is one other place where `expandArray()` is called similarly -  [TextLine.getComponents()](https://github.com/openjdk/jdk/blob/bb7a66ce7b08a7f50da3194390c1d077a0b1fb81/src/java.desktop/share/classes/java/awt/font/TextLine.java#L1078C1-L1081C22).

-------------

PR Review: https://git.openjdk.org/jdk/pull/22288#pullrequestreview-2479929617


More information about the client-libs-dev mailing list