-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Severe performance issues during search #390
Comments
@Blankwonder Thanks for opening the issue (and for being a sponsor!). I want to start by emphasizing that the Runestone Text Editor app uses the exact same version of the Runestone framework as this repository, so replicating the app's behavior should be possible. That said, I noticed a reference to UITextSearchingHelper in your call stack, which suggests you're using the Find & Replace functionality enabled via isFindInteractionEnabled on the text view. The Runestone app doesn't use this property. Instead, it relies on a custom search implementation built around the TextView's search(for:) function. I can't rule out the possibility of a performance issue with |
Yes, I used the native |
May I kindly inquire if there is any need for additional materials, such as sample files or projects, that might assist us further? Additionally, in the event of a confirmed performance issue, I was wondering if there might be any upcoming plans for optimization. Please take all the time you need; there’s absolutely no rush on my end. I’m willing to wait for any updates if there’s a plan in place to address the issue soon. Alternatively, if immediate optimization isn’t feasible, I could consider developing my own search interface using Thanks for your attention and assistance. |
I conducted some research myself, and the root of the problem lies in that for each match, UITextSearching calls the I made adjustments to the relevant code by caching changes to highlightedRanges before func performTextSearch(queryString: String, options: UITextSearchOptions, resultAggregator: UITextSearchAggregator<AnyHashable?>) {
_searching = true
performTextSearch(for: queryString, options: options) { searchResults in
for searchResult in searchResults {
let textRange = IndexedRange(searchResult.range)
resultAggregator.foundRange(textRange, searchString: queryString, document: nil)
}
resultAggregator.finishedSearching()
DispatchQueue.main.async {
self._searching = false
self.applyHighlightedRanges()
}
}
}
func decorate(foundTextRange: UITextRange, document: AnyHashable??, usingStyle style: UITextSearchFoundTextStyle) {
guard let foundTextRange = foundTextRange as? IndexedRange else {
return
}
_highlightedRanges.removeAll { $0.range == foundTextRange.range }
if let highlightedRange = _textView.theme.highlightedRange(forFoundTextRange: foundTextRange.range, ofStyle: style) {
_highlightedRanges.append(highlightedRange)
}
if (!_searching) {
self.applyHighlightedRanges()
}
}
func applyHighlightedRanges() {
_textView.highlightedRanges = _highlightedRanges;
}
func clearAllDecoratedFoundText() {
_highlightedRanges = []
_textView.highlightedRanges = []
} I'm not sure if this method is the optimal solution to the problem. If needed, I can create a corresponding code pull request. |
What happened?
When a user enters search content, if the input is very short (e.g., 1 or 2 characters), it results in an enormous number of search results, causing excessively high CPU usage. This leads to the app freezing for several seconds or even tens of seconds.
The edited file is only a few dozen KB, but there may be 100,000+ hits in the search results. Instruments show the call stack.
However, I tested the same file with Runestone.app and it seems that no similar issues occurred. Is it because the open-source version itself is not optimized in this regard, or is there a problem with the way I'm using it?
Thank you.
What are the steps to reproduce?
Use the search tool and enter only one character.
What is the expected behavior?
The text was updated successfully, but these errors were encountered: