-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[android] improve performance of Entry.MaxLength
#15614
Conversation
if (maxLength == -1) | ||
maxLength = Integer.MAX_VALUE; | ||
|
||
List<InputFilter> currentFilters = new ArrayList<>(Arrays.asList(editText.getFilters())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In C#, we had a null check for getFilters()
(?? new IInputFilter[0]
), but I don't think it can return null:
I can't find any null checks for it in Android's source code:
https://github.com/search?q=repo%3Aaosp-mirror%2Fplatform_frameworks_base%20getFilters&type=code
Editor
is a good example:
if (e.BeforeCount == 0 && e.AfterCount == 0) | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In 85b5699, it doesn't say why this check was added, but it was causing test failures when MaxLength is set to 0.
If you had an Entry
with text, then set MaxLength=0
it wasn't updating Entry.Text
after the platform fired the TextChanged
event.
@@ -358,6 +359,7 @@ public async Task NegativeMaxLengthDoesNotClip() | |||
, Skip = "https://github.com/dotnet/maui/issues/7939" | |||
#endif | |||
)] | |||
[InlineData(0)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case was failing without the above change to ITextInputExtensions
.
/rebase |
Context: dotnet#10713 When investigating a customer sample: * Navigate from a Shell flyout * To a new page with several `Entry` controls When profiling on a Pixel 5, it seems one hot path is `Entry.MaxLength`: 18.52ms (0.22%) microsoft.maui!Microsoft.Maui.Platform.EditTextExtensions.UpdateMaxLength(Android.Widget.EditText,Microsoft.Maui.IEntry) 16.03ms (0.19%) microsoft.maui!Microsoft.Maui.Platform.EditTextExtensions.UpdateMaxLength(Android.Widget.EditText,int) 12.16ms (0.14%) microsoft.maui!Microsoft.Maui.Platform.EditTextExtensions.SetLengthFilter(Android.Widget.EditText,int) * `EditTextExtensions.UpdateMaxLength()` calls * `EditText.Text` getter and setter * `EditTextExtensions.SetLengthFilter()` calls * `EditText.Get/SetFilters()` What happens is we end up marshaling strings and `IInputFilter[]` back and forth between C# and Java for every `Entry` on the page. This seems like a prime candidate to move code from C# to Java. I tried to just port the code as-is without changing logic -- so we *should* get the exact same behavior as before. Since all `Entry`s go through this code path (even ones with a default value for `MaxLength`), this improves the performance of all `Entry` and `SearchBar` on Android. With these changes in place, the calls to `EditTextExtensions.UpdateMaxLength()` are now so fast they are missing from the trace now, saving ~19ms when navigating to this page. One note, is I found this was the recommended way to create a mutable `List<T>` from an array in Java: List<InputFilter> currentFilters = new ArrayList<>(Arrays.asList(editText.getFilters())); https://stackoverflow.com/a/11659198 Makes me appreciate C#! :) Other changes: * All platform to update empty strings In 85b5699, it doesn't say why this check was added, but it was causing test failures when MaxLength is set to 0.
ff65e09
to
7199ff8
Compare
I manually rebased this. I don't know how a bot could resolve a conflict in |
Context: #10713
When investigating a customer sample:
Navigate from a Shell flyout
To a new page with several
Entry
controlsIt feels a bit slow & laggy
When profiling on a Pixel 5, it seems one hot path is
Entry.MaxLength
:EditTextExtensions.UpdateMaxLength()
callsEditText.Text
getter and setterEditTextExtensions.SetLengthFilter()
callsEditText.Get/SetFilters()
What happens is we end up marshaling strings and
IInputFilter[]
back and forth between C# and Java for everyEntry
on the page.This seems like a prime candidate to move code from C# to Java. I tried to just port the code as-is without changing logic -- so we should get the exact same behavior as before.
Since all
Entry
s go through this code path (even ones with a default value forMaxLength
), this improves the performance of allEntry
andSearchBar
on Android. With these changes in place, the calls toEditTextExtensions.UpdateMaxLength()
are now so fast they are missing from the trace now, saving ~19ms when navigating to this page.One note, is I found this was the recommended way to create a mutable
List<T>
from an array in Java:https://stackoverflow.com/a/11659198
Makes me appreciate C#! 😄