Skip to content

Conversation

kamilkrzywanski
Copy link
Contributor

Refactor: use predefined empty arrays instead of creating new ones in places identified during runtime where unnecessary objects were frequently created.

Signed-off-by: Kamil Krzywański <[email protected]>
Signed-off-by: Kamil Krzywanski <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 17, 2025
Copy link
Contributor

@donalmurtagh donalmurtagh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point in this myself. I doubt it will ever make any noticeable difference to performance and the empty array declarations are avoidable clutter.

@kamilkrzywanski
Copy link
Contributor Author

@donalmurtagh Your suggested approach will produce a ClassCastException.
Here is a benchmark to compare these two approaches. I know it’s not a game-changer, but it seems to be a good practice, too.

ToArrayBenchmark.toArrayWithEmptyConst                     thrpt   20  67323.479 ± 515.563  ops/ms
ToArrayBenchmark.toArrayWithEmptyConst:gc.alloc.rate       thrpt   20   3081.736 ±  23.608  MB/sec
ToArrayBenchmark.toArrayWithEmptyConst:gc.alloc.rate.norm  thrpt   20     48.000 ±   0.001    B/op
ToArrayBenchmark.toArrayWithEmptyConst:gc.count            thrpt   20   1268.000            counts
ToArrayBenchmark.toArrayWithEmptyConst:gc.time             thrpt   20    882.000                ms
ToArrayBenchmark.toArrayWithNew                            thrpt   20  62202.810 ± 102.498  ops/ms
ToArrayBenchmark.toArrayWithNew:gc.alloc.rate              thrpt   20   3796.461 ±   6.257  MB/sec
ToArrayBenchmark.toArrayWithNew:gc.alloc.rate.norm         thrpt   20     64.000 ±   0.001    B/op
ToArrayBenchmark.toArrayWithNew:gc.count                   thrpt   20   1671.000            counts
ToArrayBenchmark.toArrayWithNew:gc.time                    thrpt   20   1090.000                ms

@sbrannen
Copy link
Member

We do in fact tend to cache empty arrays in static fields if the empty arrays are reused.

For example, if you search the code base for [] EMPTY_ you'll see that we have over 45 such constants in use in production code (i.e., excluding tests and test fixtures).

In other words, there is precedent for this form of micro optimization within Spring Framework.

@sbrannen sbrannen added type: task A general task in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 18, 2025
@sbrannen sbrannen changed the title Refactor: use predefined empty arrays instead of creating new ones Use empty array constants instead of repeatedly creating new ones Oct 18, 2025
@sbrannen sbrannen self-assigned this Oct 18, 2025
@sbrannen sbrannen added this to the 7.0.0-RC2 milestone Oct 18, 2025
@sbrannen sbrannen closed this in 9483670 Oct 18, 2025
sbrannen added a commit that referenced this pull request Oct 18, 2025
@sbrannen
Copy link
Member

This has been merged into main in 9483670 and revised in 0fbebd8.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: task A general task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants