Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

remove usage of Dart_New for paragraph/libtxt #16837

Merged
merged 5 commits into from
Feb 28, 2020

Conversation

dnfield
Copy link
Contributor

@dnfield dnfield commented Feb 27, 2020

Part of flutter/flutter#50997

TL;DR is that Dart_New is slow and we should try to avoid it.

After this it should just be images.

lib/ui/text.dart Outdated
@@ -1914,6 +1890,20 @@ class Paragraph extends NativeFieldWrapperClass2 {
void layout(ParagraphConstraints constraints) => _layout(constraints.width);
void _layout(double width) native 'Paragraph_layout';

List<TextBox> _decodeTextBoxes(Float32List encoded) {
final List<TextBox> boxes = List<TextBox>(encoded[0].toInt());
Copy link
Member

Choose a reason for hiding this comment

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

The count of text boxes is redundant with the length of the encoded Float32List

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

lib/ui/text.dart Outdated
List<TextBox> _decodeTextBoxes(Float32List encoded) {
final List<TextBox> boxes = List<TextBox>(encoded[0].toInt());
for (int index = 0; index < boxes.length; index += 1) {
final int position = (index * 5) + 1;
Copy link
Member

Choose a reason for hiding this comment

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

Avoid the cost of the multiply by advancing the index by 5 on each iteration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

lib/ui/text.dart Outdated
final Float64List encoded = _computeLineMetrics();
final List<LineMetrics> metrics = List<LineMetrics>(encoded[0].toInt());
for (int index = 0; index < metrics.length; index += 1) {
final int position = (index * 5) + 1;
Copy link
Member

Choose a reason for hiding this comment

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

Advance the position by 9 instead of 5 for LineMetrics

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and added some tests that didn't exist that would have caught this.

for (unsigned long i = 0; i < metrics.size(); i++) {
auto position = (i * 9) + 1;
const txt::LineMetrics& line = metrics[i];
result[position + 0] = static_cast<float>(line.hard_break);
Copy link
Member

Choose a reason for hiding this comment

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

cast this to double to match the type of result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@dnfield
Copy link
Contributor Author

dnfield commented Feb 28, 2020

Also noticed that we could make LineMetrics const constructable. Not sure how crucial that is since the real use case for it couldn't use it. At any rate I added hashCode and == and toString overrides for LineMetrics, they were helpful to me in writing tests.

@jonahwilliams
Copy link
Member

If web has a lineMetrics you should add the logic there too, plus const construction if you decide on it.

@dnfield
Copy link
Contributor Author

dnfield commented Feb 28, 2020

@jonahwilliams - strangely, the web EngineLineMetrics (implementer of LineMetrics) already has hashCode and equals defined.

I don't want to make this const right now because I'm not sure where it would be useful and it's entirely possible it turns out to be breaking for someone that's constructing it in a non const way where it could have been const. We can do that in a separate PR if desired.

@dnfield
Copy link
Contributor Author

dnfield commented Feb 28, 2020

I'm going to merge this on the idea that the test needs the same logic to pass without these changes - if it's a problem we should fix it separately.

@dnfield dnfield merged commit c2fc31d into flutter:master Feb 28, 2020
@dnfield dnfield deleted the paragraph_faster branch February 28, 2020 08:15
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants