Skip to content

Conversation

@NYgomets
Copy link
Contributor

Description

This PR optimizes the SortedResourcesFactoryBean.createInstance() method to improve performance and stability.

Motivation

The current implementation has several issues:

  1. Repeated I/O operations: getURL() is called O(n log n) times during sorting due to comparator invocations. For 10 resources, this results in ~35 URL resolutions instead of 10.

  2. Silent exception handling: IOExceptions during sorting are caught and return 0, violating the Comparator contract and potentially causing unpredictable sort behavior.

  3. Unnecessary conversions: Resources go through getResources()Arrays.asList()new ArrayList() transformations.

Changes

  • Cache resource URLs once before sorting
  • Remove intermediate collection conversions
  • Replace silent exception swallowing with clear IllegalStateException
  • Add contextual information to error messages

Performance Impact

  • I/O calls reduced by ~70% (10 resources: 35 calls → 10 calls)
  • Memory allocations reduced (eliminated redundant ArrayList copies)
  • Sorting stability improved (consistent error handling)

Testing

  • Existing tests pass
  • Verified behavior with multiple location patterns

Backward Compatibility

This change maintains the same output behavior while improving performance and error handling. No breaking changes to the public API.

Cache resource URLs before sorting to eliminate repeated I/O calls
during comparator operations. The previous implementation called
getURL() multiple times per resource during sorting (O(n log n)
calls), and silently swallowed IOExceptions by returning 0,
potentially causing unstable sort results.

This change:
- Caches URLs once per resource before sorting (O(n) I/O calls)
- Removes unnecessary ArrayList conversions
- Provides clear exception handling with context
- Improves performance by ~70% for typical use cases

Signed-off-by: Park Juhyeong <wngud5957@naver.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 24, 2025
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 27, 2025
@jhoeller jhoeller self-assigned this Oct 27, 2025
@jhoeller jhoeller added this to the 6.2.13 milestone Oct 27, 2025
@jhoeller jhoeller merged commit 4b7b280 into spring-projects:6.2.x Oct 28, 2025
5 checks passed
@jhoeller
Copy link
Contributor

Thanks for the PR! Even if this class is only used in a specific configuration scenario, the code was worth improving indeed.

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: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants