Skip to content

Optimize ResStringPool - #4138

Merged
IgorEisberg merged 6 commits into
iBotPeaches:mainfrom
illogical-robot:fix/resstringpool-memory-usage
May 8, 2026
Merged

IgorEisberg merged 6 commits into
iBotPeaches:mainfrom
illogical-robot:fix/resstringpool-memory-usage

Conversation

@X1nto

@X1nto X1nto commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Here are the major changes:

  1. I migrated getUtf8 and getUtf16 to use packed longs instead of int array. This gets rid of extra allocations. Fun fact: this optimization is also used by Jetpack Compose.
  2. Cache added for index to string and string to index lookups. index to string lookups are stored in a simple String[] array with separate sentinel string as a marker for malformed values. String to index lookups are stored in a HashMap and are lazy-initialized for the first time when findString() function is used.
  3. UTF-8 decoding was replaced with new String(mStrings, offset, length, StandardCharsets.UTF_8) because String has a fast path for decoding UTF-8 strings. UTF-8 malformed checks were also moved to an if statement. new String() uses CodingErrorAction.REPLACE and checking it is trivial. I also moved bound checks to an if case at the top of the function to avoid paying for thrown exceptions.

I also thought of inlining getStyle() inside getText() to avoid extra allocations, but from what I tested, not many APKs benefit from it. Maybe the APKs I tested weren't good candidates to benchmark this. Anyway, it's worth noting that getStyle() returning an array isn't the best possible behavior.

I'm a bit skeptical about keeping index-to-string and string-to-index caches in memory, but seeing how getString() is used, it seems like getString() may be called for the same indexes multiple times. Correct me if I'm wrong, in which case I'll get rid of the cache.

Here's before and after in memory usage for the Samsung APK I use for benchmarks:

Before After
image image

@IgorEisberg

Copy link
Copy Markdown
Collaborator

Just a few tweaks regarding consistency with the rest of the source (like unneeded whitespace).
Also, PackedValue implies an object, something that is instantiated, but it isn't. It's just a utility class with static helpers.
Those helpers are so basic that having them in a separate class is really unnecessary.

Other than that, this looks like a fine PR.

@X1nto

X1nto commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Also, PackedValue implies an object, something that is instantiated, but it isn't. It's just a utility class with static helpers. Those helpers are so basic that having them in a separate class is really unnecessary.

Thought about this, I guess it was more about "what if this might be needed elsewhere", plus naming them makes it more clear what they do. I suppose I can inline them, no problem.

@X1nto

X1nto commented May 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Would it be better to move packing functions to ResStringPool or should I just completely inline them?

@IgorEisberg

Copy link
Copy Markdown
Collaborator

Thought about this, I guess it was more about "what if this might be needed elsewhere", plus naming them makes it more clear what they do. I suppose I can inline them, no problem.

It's in the slippery slope territory. It only handles packing 2 integers into 1 long.
If it's a general-purpose utility class, then what about packing 2 shorts into 1 integer, or 4 bytes into 1 integer, or 2 bytes into 1 short, and so on... can get really verbose without any purpose at the moment. Also the name PackedValue doesn't imply a utility class. In fact, I thought it was along the lines of android.util.TypedValue, which is a container.

Would it be better to just move packing functions to ResStringPool or should I just completely inline them?

I guess it's a style question that doesn't have an objective answer.
My take: It's better to inline it than to have functions like int unpackSecond(long value) { return (int) value; }.

@IgorEisberg

Copy link
Copy Markdown
Collaborator

(long) 4 and (long) 2 can be simplified to 4L and 2L, otherwise this looks fine.
If you handle the other change suggestions, I'll submit the review.

@X1nto

X1nto commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

(long) 4 and (long) 2 can be simplified to 4L and 2L, otherwise this looks fine.

Oh yeah, I keep forgetting Java has L suffixes too.

If you handle the other change suggestions, I'll submit the review.

Sorry, not following, what other change suggestions?

@IgorEisberg

Copy link
Copy Markdown
Collaborator

Sorry, not following, what other change suggestions?

Forgot to submit the suggestions lmao.

X1nto and others added 2 commits May 8, 2026 20:35
Co-authored-by: Igor Eisberg <8811086+IgorEisberg@users.noreply.github.com>
@IgorEisberg
IgorEisberg self-requested a review May 8, 2026 17:20

@IgorEisberg IgorEisberg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. Tested myself on a large scale to be extra safe.
The failed check can be ignored, GitHub Actions has a stroke.

@IgorEisberg
IgorEisberg merged commit 8dd6061 into iBotPeaches:main May 8, 2026
21 of 33 checks passed
@iBotPeaches iBotPeaches added this to the v3.0.3 milestone May 8, 2026
iBotPeaches added a commit to iBotPeaches/apktool.org that referenced this pull request May 8, 2026
Added details about performance improvements and library upgrades.
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.

3 participants