Skip to content

Fix O(n²) performance in WrapperProperties$OrderedProperties#12542

Draft
gnodet wants to merge 1 commit into
perf/reduce-immutable-model-allocationsfrom
perf/wrapper-properties-on2
Draft

Fix O(n²) performance in WrapperProperties$OrderedProperties#12542
gnodet wants to merge 1 commit into
perf/reduce-immutable-model-allocationsfrom
perf/wrapper-properties-on2

Conversation

@gnodet

@gnodet gnodet commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace CopyOnWriteArrayList with LinkedHashMap for key ordering in OrderedProperties
  • CopyOnWriteArrayList.contains() is O(n), making every put() call O(n) and putAll() O(n²)
  • LinkedHashMap provides O(1) put/contains/remove while preserving insertion order
  • Eliminates custom KeySet/EntrySet inner classes — LinkedHashMap views handle this natively

JFR profiling shows WrapperProperties consuming ~13% of CPU during mvn validate on Apache Camel (676 modules), primarily from the O(n²) putAll path triggered by keySet()/entrySet()/values() which each create a new OrderedProperties copy.

Stacked on #12540

Test plan

  • mvn test -pl compat/maven-model — all tests pass
  • mvn test -pl impl/maven-impl — 466 tests pass
  • mvn test -pl compat/maven-model-builder — 166 tests pass

🤖 Generated with Claude Code

Replace CopyOnWriteArrayList with LinkedHashMap for key ordering in
OrderedProperties. CopyOnWriteArrayList.contains() is O(n), making
every put() call O(n) and putAll() O(n²). LinkedHashMap provides O(1)
put/contains/remove while preserving insertion order.

This also simplifies the implementation: LinkedHashMap's entrySet(),
keySet(), and values() already provide ordered views, eliminating the
need for custom AbstractSet inner classes.

JFR profiling shows WrapperProperties consuming ~13% of CPU during
'mvn validate' on large projects (676 modules), primarily from the
O(n²) putAll path triggered by keySet()/entrySet()/values() which
each create a new OrderedProperties copy.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant