From 85bd669eac867b32d774f3ed7ba305e48776b803 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 20 Jul 2017 12:19:57 -0700 Subject: [PATCH] Benchmark rework to better reflect real usage and slight performance improvements. Change-Id: I3979c725ff9058fc208626ffb5fce95c2e03ac19 --- .../benchmarks/paragraph_benchmarks.cc | 114 +++++++++--------- engine/src/flutter/src/paragraph.cc | 8 +- engine/src/flutter/src/paragraph.h | 5 + 3 files changed, 70 insertions(+), 57 deletions(-) diff --git a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc index ca547764b77..4286ac90199 100644 --- a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc +++ b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc @@ -44,14 +44,14 @@ static void BM_ParagraphShortLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + builder.PushStyle(text_style); + builder.AddText(u16_text); + builder.Pop(); + auto paragraph = builder.Build(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); - - builder.PushStyle(text_style); - builder.AddText(u16_text); - builder.Pop(); - auto paragraph = builder.Build(); - + paragraph->SetDirty(); paragraph->Layout(300, true); } } @@ -85,14 +85,14 @@ static void BM_ParagraphLongLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + builder.PushStyle(text_style); + builder.AddText(u16_text); + builder.Pop(); + auto paragraph = builder.Build(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); - - builder.PushStyle(text_style); - builder.AddText(u16_text); - builder.Pop(); - auto paragraph = builder.Build(); - + paragraph->SetDirty(); paragraph->Layout(300, true); } } @@ -127,14 +127,14 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + builder.PushStyle(text_style); + builder.AddText(u16_text); + builder.Pop(); + auto paragraph = builder.Build(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); - - builder.PushStyle(text_style); - builder.AddText(u16_text); - builder.Pop(); - auto paragraph = builder.Build(); - + paragraph->SetDirty(); paragraph->Layout(300, true); } } @@ -151,48 +151,51 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + for (int i = 0; i < 1000; ++i) { + builder.PushStyle(text_style); + builder.AddText(u16_text); + } + auto paragraph = builder.Build(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); - for (int i = 0; i < 1000; ++i) { - builder.PushStyle(text_style); - builder.AddText(u16_text); - } - auto paragraph = builder.Build(); + paragraph->SetDirty(); paragraph->Layout(300, true); } } BENCHMARK(BM_ParagraphManyStylesLayout); static void BM_ParagraphTextBigO(benchmark::State& state) { - std::string text(state.range(0), '-'); - auto icu_text = icu::UnicodeString::fromUTF8(text); - std::u16string u16_text(icu_text.getBuffer(), - icu_text.getBuffer() + icu_text.length()); + std::vector text; + for (uint16_t i = 0; i < state.range(0); ++i) { + text.push_back(i % 5 == 0 ? ' ' : i); + } + std::u16string u16_text(text.data(), text.data() + text.size()); txt::ParagraphStyle paragraph_style; txt::TextStyle text_style; text_style.color = SK_ColorBLACK; auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + builder.PushStyle(text_style); + builder.AddText(u16_text); + builder.Pop(); + auto paragraph = builder.Build(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); - - builder.PushStyle(text_style); - builder.AddText(u16_text); - builder.Pop(); - auto paragraph = builder.Build(); - + paragraph->SetDirty(); paragraph->Layout(300, true); } state.SetComplexityN(state.range(0)); } BENCHMARK(BM_ParagraphTextBigO) - ->RangeMultiplier(10) + ->RangeMultiplier(4) ->Range(1 << 6, 1 << 14) - ->Complexity(benchmark::oNSquared); + ->Complexity(benchmark::oN); static void BM_ParagraphStylesBigO(benchmark::State& state) { - const char* text = "A short sentence. "; + const char* text = "vry shrt "; auto icu_text = icu::UnicodeString::fromUTF8(text); std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); @@ -202,22 +205,23 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); - for (int i = 0; i < state.range(0); ++i) { - builder.PushStyle(text_style); - builder.AddText(u16_text); - } - auto paragraph = builder.Build(); + for (int i = 0; i < state.range(0); ++i) { + builder.PushStyle(text_style); + builder.AddText(u16_text); + } + auto paragraph = builder.Build(); + while (state.KeepRunning()) { + paragraph->SetDirty(); paragraph->Layout(300, true); } state.SetComplexityN(state.range(0)); } BENCHMARK(BM_ParagraphStylesBigO) - ->RangeMultiplier(20) - ->Range(1 << 4, 1 << 12) - ->Complexity(benchmark::oNSquared); + ->RangeMultiplier(4) + ->Range(1 << 3, 1 << 12) + ->Complexity(benchmark::oN); // ----------------------------------------------------------------------------- // @@ -228,8 +232,8 @@ BENCHMARK(BM_ParagraphStylesBigO) static void BM_ParagraphMinikinDoLayout(benchmark::State& state) { std::vector text; - for (uint16_t i = 0; i < state.range(0); ++i) { - text.push_back(i); + for (uint16_t i = 0; i < state.range(0) * 3; ++i) { + text.push_back(i % 5 == 0 ? ' ' : i); } minikin::FontStyle font; txt::TextStyle text_style; @@ -247,13 +251,13 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) { while (state.KeepRunning()) { minikin::Layout layout; - layout.doLayout(text.data(), 0, 50, text.size(), 0, font, paint, + layout.doLayout(text.data(), 0, state.range(0), text.size(), 0, font, paint, collection); } state.SetComplexityN(state.range(0)); } BENCHMARK(BM_ParagraphMinikinDoLayout) - ->RangeMultiplier(10) + ->RangeMultiplier(4) ->Range(1 << 7, 1 << 14) ->Complexity(benchmark::oN); diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index d7cd417c5f6..4cb609530cb 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -299,12 +299,12 @@ void Paragraph::Layout(double width, bool force) { : breaks[break_index]; const size_t layout_end = std::min(run.end, next_break); - int bidiFlags = 0; + bool bidiFlags = paragraph_style_.rtl; layout.doLayout(text_.data(), layout_start, layout_end - layout_start, text_.size(), bidiFlags, font, minikin_paint, font_collection_->GetMinikinFontCollectionForFamily( run.style.font_family)); - FillWhitespaceSet(run.start, run.end, + FillWhitespaceSet(layout_start, layout_end, minikin::getHbFontLocked(layout.getFont(0))); const size_t glyph_count = layout.nGlyphs(); @@ -848,4 +848,8 @@ bool Paragraph::DidExceedMaxLines() const { return false; } +void Paragraph::SetDirty(bool dirty) { + needs_layout_ = dirty; +} + } // namespace txt diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index bc2281ca214..e148f01992e 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -88,6 +88,11 @@ class Paragraph { bool DidExceedMaxLines() const; + // Sets the needs_layout_ to dirty. When Layout() is called, a new Layout will + // be performed when this is set to true. Can also be used to prevent a new + // Layout from being calculated by setting to false. + void SetDirty(bool dirty = true); + private: friend class ParagraphBuilder; FRIEND_TEST(RenderTest, SimpleParagraph);