Skip to content

Optimize O(n²) string building and collection allocations#514

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/improve-slow-code-efficiency
Draft

Optimize O(n²) string building and collection allocations#514
Copilot wants to merge 3 commits intomainfrom
copilot/improve-slow-code-efficiency

Conversation

Copy link
Copy Markdown

Copilot AI commented Dec 27, 2025

Identified and fixed performance anti-patterns: quadratic string building in Iban.toPrintableString() and zero-capacity collection initialization causing repeated reallocations.

Changes

Fixed O(n²) → O(n) in Iban.toPrintableString()

  • Replaced StringBuilder.insert() loop with sequential append() operations
  • Each insert() shifts all subsequent characters; avoided by building left-to-right
// Before: O(n²)
for (int i = 0; i < length / 4; i++) {
  printable.insert((i + 1) * 4 + i, ' ');
}

// After: O(n)
for (int i = 0; i < length; i++) {
  if (i > 0 && i % 4 == 0) printable.append(' ');
  printable.append(value.charAt(i));
}

Optimized collection initial capacities

  • Calendar.holidaysWithin/businessDaysWithin: Size hint from ChronoUnit.DAYS.between() (capped at 16)
  • ConfigurableCalendar/CompositeCalendar.getHolidaysFor(): Capacity 4 instead of 0
  • Reduces reallocation overhead during list/set construction
Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 27, 2025
Copilot AI and others added 2 commits December 27, 2025 20:08
…shSet initial capacity

Co-authored-by: marcwrobel <2988912+marcwrobel@users.noreply.github.com>
….getDays

Co-authored-by: marcwrobel <2988912+marcwrobel@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize O(n²) string building and collection allocations Dec 27, 2025
Copilot AI requested a review from marcwrobel December 27, 2025 20:15
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.

2 participants