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

Make EditText.highlightText faster by adding spans directly #28

Merged
merged 2 commits into from
Mar 16, 2024

Conversation

tom93
Copy link
Contributor

@tom93 tom93 commented Jan 26, 2024

This makes searching in medium/large texts (e.g. files opened in the File Editor) faster by orders of magnitude, especially when there are many matches. Tested on Android 9 and 14 with a 100KB text file (but the speed difference should also be noticeable on smaller files).

From the commit message:

The previous code called setText() after adding each span, which is very slow when there is a lot of text. It is enough to call setText() once at the end if any matches were found.

But we can do even better: we can add the highlighting spans directly to the existing Editable of the EditText, and skip setText() entirely, thus avoiding any copying of the text.

The documentation of EditText.getText() says "The content of the return value should not be modified. If you want a modifiable one, you should make your own copy first." (inherited from TextView), but adding spans to the return value appears to work fine.

The previous code called setText() after adding each span, which is
very slow when there is a lot of text. It is enough to call setText()
once at the end if any matches were found.

But we can do even better: we can add the highlighting spans directly
to the existing Editable of the EditText, and skip setText() entirely,
thus avoiding any copying of the text.

The documentation of EditText.getText() says "The content of the
return value should not be modified. If you want a modifiable one, you
should make your own copy first." (inherited from TextView), but
adding spans to the return value appears to work fine.
@naveensingh
Copy link
Member

I tried using a 200KB text file and got a crash, I guess the File editor could use some work.

@tom93
Copy link
Contributor Author

tom93 commented Mar 16, 2024

Ha ha, I can open a 1MB text file on an Android-x86 9 VM with 2GB RAM, but it does increase resident RAM usage by a whopping 200MB (217MB when idle, 418MB with file open).

Thanks for merging! (Edit: I misread the "Merge" commit above -- the PR wasn't merged yet at the time.)

@naveensingh
Copy link
Member

Seems to work fine, thanks!

@naveensingh naveensingh merged commit 96cdf2c into FossifyOrg:master Mar 16, 2024
@tom93
Copy link
Contributor Author

tom93 commented Mar 17, 2024

Thanks! (I misread the earlier merge commit and said my first thanks too soon. I swear I wasn't trying to be passive-aggressive!)

@tom93 tom93 deleted the make-highlightText-fast branch March 17, 2024 03:51
@naveensingh
Copy link
Member

@tom93 I figured, don't worry about it :)

@naveensingh
Copy link
Member

I can open a 1MB text file on an Android-x86 9 VM with 2GB RAM

By the way, when fixing bugs or implementing features, please try to test things on real devices with average specs or emulators (if possible).

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