Replace TEXT_TO_DESCRIPTION HashMap with primitive int[]/Label[] arrays and binary search#6722
Draft
Replace TEXT_TO_DESCRIPTION HashMap with primitive int[]/Label[] arrays and binary search#6722
Conversation
…ys and binary search Co-authored-by: nicolas-raoul <99590+nicolas-raoul@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Replace HashMap with primitive-array implementation in Label.java
Replace TEXT_TO_DESCRIPTION HashMap with primitive int[]/Label[] arrays and binary search
Mar 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Label.TEXT_TO_DESCRIPTIONwas a public mutableHashMap<String, Label>that allocated a"Q" + idstring per entry on everyinit()call and used hash-based lookup. Replace it with compact parallel primitive arrays andArrays.binarySearch.Changes
Label.javaTEXT_TO_DESCRIPTION(HashMap,Mapimports)private static volatile int[] QIDS/Label[] QID_LABELSinit()is nowsynchronizedand idempotent; builds sorted, deduplicated arrays using a stable sort — matching the originalHashMap.putlast-write-wins semantics for QIDs shared across multiple label categoriesparseQidNoAlloc(String)— parses"Q16970"→16970without substring allocation; useslongaccumulator to catch overflowfromQidInt(int)—Arrays.binarySearchlookup by integer QIDfromText(String)— delegates toparseQidNoAlloc+fromQidInt; invalid input returnsUNKNOWNvia caughtNumberFormatExceptionNearbyParentFragment.ktif (Label.TEXT_TO_DESCRIPTION.isEmpty()) Label.init(...)with unconditionalLabel.init(...)— safe sinceinitis now idempotentLabelTest.kttestLabelIconFromQidInt()covering the newfromQidInt(int)path alongside the existingfromTexttestOriginal prompt
Problem: Replace the current HashMap<String, Label> TEXT_TO_DESCRIPTION lookup in Label.java with a memory-efficient primitive-array implementation that uses sorted int[] of QIDs and a parallel Label[] for values. The project already guarantees that all resource integer-arrays are present, non-empty, have matching sizes and are globally sorted, so the implementation should assume that and avoid defensive null/size checks or runtime sorting. Create a draft pull request on commons-app/apps-android-commons targeting the main branch implementing the change and updating usages and tests.
Changes required (detailed and actionable):
Files to modify:
Tests: update LabelTest assertions to cover both fromText("Q16970") and fromQidInt(16970). Ensure tests compile under Robolectric.
Notes:
The following is the prior conversation context from the user's chat exploration (may be truncated):
Assistant: GitHub Copilot Chat Assistant here. Quick clarifying questions so I create exactly what you want:
This pull request was created from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.