Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve XML performance #139

Merged
merged 1 commit into from
Feb 12, 2025
Merged

Conversation

kitadai31
Copy link

@kitadai31 kitadai31 commented Feb 11, 2025

Improve the performance of XML operations by not calling node.getChildNodes() multiple times in a loop.

The result of node.getChildNodes() should be stored in a variable before starting a for loop.

I compared the performance before PR and after PR, using a Snapdragon 662+4GB RAM phone and a Snapdragon 410+2GB RAM phone
This resulted in significant improvements for some XML files, especially those with many child nodes.

https://github.com/inotia00/revanced-patches/blob/b1776cf00729216c001a8dc6720468c08666936d/patches/src/main/kotlin/app/revanced/patches/youtube/layout/theme/ThemePatch.kt#L79-L90
[res/values/colors.xml]
SD662: 637 ms -> 3 ms
SD410: 2971 ms -> 6 ms

https://github.com/inotia00/revanced-patches/blob/b1776cf00729216c001a8dc6720468c08666936d/patches/src/main/kotlin/app/revanced/patches/youtube/utils/settings/SettingsPatch.kt#L227-L244
[res/values/styles.xml]
SD662: 1107 ms -> 10 ms
SD410: 6401 ms -> 35 ms

https://github.com/inotia00/revanced-patches/blob/b1776cf00729216c001a8dc6720468c08666936d/patches/src/main/kotlin/app/revanced/patches/youtube/general/snackbar/SnackBarComponentsPatch.kt#L350-L361
[res/values/dimens.xml]
SD662: 4837 ms -> 11 ms
SD410: 24929 ms -> 13 ms

In addition, I did some refactoring:

  • Usually, to get the root node of a document, use document.getDocumentElement()
  • Avoid iterating one XML twice
  • break a for loop if possible

I tested this change with 19.44.36, 19.16.36, and 18.29.36 and confirmed that this changes don't change the result resources.
I compared the patched-temporary-files dir of current patches and my PR branch patches using WinMerge and those were identical.

@inotia00
Copy link
Owner

This PR includes a small refactoring for ResourcePatch.

Can this PR includes a class java.nio.file.Files check for Android 5 - 7?

@kitadai31
Copy link
Author

kitadai31 commented Feb 11, 2025

No, I was just going to push a second PR that is for A5-7 support

@inotia00
Copy link
Owner

Thanks

@inotia00 inotia00 merged commit 325b43e into inotia00:dev Feb 12, 2025
@kitadai31 kitadai31 deleted the xml-performance branch February 12, 2025 06:09
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