Skip to content

Use tailMap for O(matches) wildcard key iteration instead of O(totalKeys)#52

Merged
NassimBtk merged 3 commits intofeature/issue-48-support-iterating-over-object-children-using-wildcardfrom
copilot/sub-pr-49
Mar 20, 2026
Merged

Use tailMap for O(matches) wildcard key iteration instead of O(totalKeys)#52
NassimBtk merged 3 commits intofeature/issue-48-support-iterating-over-object-children-using-wildcardfrom
copilot/sub-pr-49

Conversation

Copy link

Copilot AI commented Mar 20, 2026

Wildcard expansion was scanning the entire map.keySet() for every existingEntry, making each lookup O(totalKeys) regardless of how many keys actually matched.

Changes

  • tailMap-bounded iteration: Replace map.keySet() with map.tailMap(prefix, true).keySet() so the scan starts at the first key that could match the prefix rather than at the beginning of the map.
  • Early-exit break: Stop iterating as soon as a key no longer matches the prefix (case-insensitive regionMatches check), avoiding traversal of unrelated keys.
// Before
for (String key : map.keySet()) {
    if (key.length() > prefix.length() && key.regionMatches(true, 0, prefix, 0, prefix.length())) {
        ...
    }
}

// After
for (String key : map.tailMap(prefix, true).keySet()) {
    if (!key.regionMatches(true, 0, prefix, 0, prefix.length())) {
        break;
    }
    if (key.length() > prefix.length()) {
        ...
    }
}

Because the TreeMap uses String.CASE_INSENSITIVE_ORDER, tailMap uses the same comparator — all case variants of a given prefix are grouped consecutively, so the break is safe and no matches are skipped.


💡 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.

…eys)

Co-authored-by: NassimBtk <14110109+NassimBtk@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 20, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-3f672d5de33c41b7/dbs/java/working/files-to-index2041057654805951792.list --sourceArchiveDir=/tmp/codeql-scratch-3f672d5de33c41b7/dbs/java/src --outputDir=/tmp/codeql-scratch-3f672d5de33c41b7/dbs/java/trap/java (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] [WIP] Address feedback on wildcard iteration performance improvement Use tailMap for O(matches) wildcard key iteration instead of O(totalKeys) Mar 20, 2026
Copilot AI requested a review from NassimBtk March 20, 2026 11:11
@NassimBtk NassimBtk marked this pull request as ready for review March 20, 2026 11:12
@NassimBtk NassimBtk merged commit 172ee43 into feature/issue-48-support-iterating-over-object-children-using-wildcard Mar 20, 2026
@NassimBtk NassimBtk deleted the copilot/sub-pr-49 branch March 20, 2026 11:12
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