From 1fca345a7776e989c56d036165017536ae16d84d Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 20 Jul 2017 16:50:18 -0700 Subject: [PATCH] Benchmark improvements and additions. Document n^2 benchmark findings. Change-Id: I697acad51082151e6614abffe6bebbbe73f31a75 --- .../benchmarks/paragraph_benchmarks.cc | 44 +++++++++++++++++-- engine/src/flutter/src/paragraph.cc | 10 +++++ engine/src/flutter/src/paragraph.h | 8 ++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc index 29589f99e9..e497168b08 100644 --- a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc +++ b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc @@ -232,7 +232,7 @@ BENCHMARK(BM_ParagraphStylesBigO) static void BM_ParagraphMinikinDoLayout(benchmark::State& state) { std::vector text; - for (uint16_t i = 0; i < state.range(0) * 3; ++i) { + for (uint16_t i = 0; i < 16000 * 2; ++i) { text.push_back(i % 5 == 0 ? ' ' : i); } minikin::FontStyle font; @@ -251,8 +251,8 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) { while (state.KeepRunning()) { minikin::Layout layout; - layout.doLayout(text.data(), 0, state.range(0), text.size(), 0, font, paint, - collection); + layout.doLayout(text.data(), 0, state.range(0), state.range(0), 0, font, + paint, collection); } state.SetComplexityN(state.range(0)); } @@ -261,6 +261,44 @@ BENCHMARK(BM_ParagraphMinikinDoLayout) ->Range(1 << 7, 1 << 14) ->Complexity(benchmark::oN); +static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) { + std::vector text; + for (uint16_t i = 0; i < 16000 * 2; ++i) { + text.push_back(i % 5 == 0 ? ' ' : i); + } + minikin::FontStyle font; + txt::TextStyle text_style; + text_style.font_family = "Roboto"; + minikin::MinikinPaint paint; + + font = minikin::FontStyle(4, false); + paint.size = text_style.font_size; + paint.letterSpacing = text_style.letter_spacing; + paint.wordSpacing = text_style.word_spacing; + + auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + + minikin::LineBreaker breaker; + breaker.setLocale(icu::Locale(), nullptr); + breaker.resize(text.size()); + memcpy(breaker.buffer(), text.data(), text.size() * sizeof(text[0])); + breaker.setText(); + + while (state.KeepRunning()) { + for (int i = 0; i < 20; ++i) { + breaker.addStyleRun( + &paint, font_collection.GetMinikinFontCollectionForFamily("Roboto"), + font, state.range(0) / 20 * i, state.range(0) / 20 * (i + 1), false, + 0); + } + } + state.SetComplexityN(state.range(0)); +} +BENCHMARK(BM_ParagraphMinikinAddStyleRun) + ->RangeMultiplier(4) + ->Range(1 << 7, 1 << 14) + ->Complexity(benchmark::oN); + static void BM_ParagraphSkTextBlobAlloc(benchmark::State& state) { SkPaint paint; paint.setAntiAlias(true); diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 58885cc361..e300e58b00 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -127,6 +127,11 @@ void Paragraph::SetText(std::vector text, StyledRuns runs) { breaker_.setText(); } +// NOTE: Minikin LineBreaker addStyleRun() has an O(N^2) (according to +// benchmarks) time complexity where N is the total number of characters. +// However, this is not significant for reasonably sized paragraphs. It is +// currently recommended to break up very long paragraphs (10k+ characters) to +// ensure speedy layout. void Paragraph::AddRunsToLineBreaker( std::unordered_map>& collection_map) { @@ -300,6 +305,11 @@ void Paragraph::Layout(double width, bool force) { const size_t layout_end = std::min(run.end, next_break); bool bidiFlags = paragraph_style_.rtl; + // NOTE: Minikin Layout doLayout() has an O(N^2) (according to + // benchmarks) time complexity where N is the total number of characters. + // However, this is not significant for reasonably sized paragraphs. It is + // currently recommended to break up very long paragraphs (10k+ + // characters) to ensure speedy layout. layout.doLayout(text_.data() + layout_start, 0, layout_end - layout_start, layout_end - layout_start, bidiFlags, font, minikin_paint, font_collection_->GetMinikinFontCollectionForFamily( diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index e148f01992..74d5a7ba40 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -43,6 +43,14 @@ class Paragraph { ~Paragraph(); + // NOTE: Minikin Layout doLayout() and LineBreaker addStyleRun() has an + // O(N^2) (according to benchmarks) time complexity where N is the total + // number of characters. However, this is not significant for reasonably sized + // paragraphs. It is currently recommended to break up very long paragraphs + // (10k+ characters) to ensure speedy layout. + // + // Layout calculates the positioning of all the glyphs. Must call this method + // before Painting and getting any statistics from this class. void Layout(double width, bool force = false); // Paints the Laid out text onto the supplied SkCanvas at (x, y) offset from