sync_star_semantics_bench.dart 3.92 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import '../common.dart';

const int _kNumIterations = 1000;
const int _kNumWarmUp = 100;

void main() {
  final List<String> words = 'Lorem Ipsum is simply dummy text of the printing and'
14
    " typesetting industry. Lorem Ipsum has been the industry's"
15 16 17 18 19 20 21
    ' standard dummy text ever since the 1500s, when an unknown'
    ' printer took a galley of type and scrambled it to make a'
    ' type specimen book'.split(' ');
  final List<InlineSpanSemanticsInformation> data = <InlineSpanSemanticsInformation>[];
  for (int i = 0; i < words.length; i++) {
    if (i.isEven) {
      data.add(
22
        InlineSpanSemanticsInformation(words[i]),
23
      );
24
    } else if (i.isEven) {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      data.add(
        InlineSpanSemanticsInformation(words[i], isPlaceholder: true),
      );
    }
  }
  print(words);

  // Warm up lap
  for (int i = 0; i < _kNumWarmUp; i += 1) {
    combineSemanticsInfoSyncStar(data);
    combineSemanticsInfoList(data);
  }

  final Stopwatch watch = Stopwatch();
  watch.start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    consumeSpan(combineSemanticsInfoSyncStar(data));
  }
  final int combineSemanticsInfoSyncStarTime = watch.elapsedMicroseconds;
  watch
    ..reset()
    ..start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    consumeSpan(combineSemanticsInfoList(data));
  }
  final int combineSemanticsInfoListTime = watch.elapsedMicroseconds;
  watch
    ..reset()
    ..start();

  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
  const double scale = 1000.0 / _kNumIterations;
  printer.addResult(
    description: 'combineSemanticsInfoSyncStar',
    value: combineSemanticsInfoSyncStarTime * scale,
    unit: 'ns per iteration',
    name: 'combineSemanticsInfoSyncStar_iteration',
  );
  printer.addResult(
    description: 'combineSemanticsInfoList',
    value: combineSemanticsInfoListTime * scale,
    unit: 'ns per iteration',
    name: 'combineSemanticsInfoList_iteration',
  );
  printer.printToStdout();
}

String consumeSpan(Iterable<InlineSpanSemanticsInformation> items) {
  String result = '';
74
  for (final InlineSpanSemanticsInformation span in items) {
75 76 77 78 79 80 81 82
    result += span.text;
  }
  return result;
}


Iterable<InlineSpanSemanticsInformation> combineSemanticsInfoSyncStar(List<InlineSpanSemanticsInformation> inputs) sync* {
  String workingText = '';
83
  String? workingLabel;
84
  for (final InlineSpanSemanticsInformation info in inputs) {
85
    if (info.requiresOwnNode) {
86 87 88
      yield InlineSpanSemanticsInformation(workingText, semanticsLabel: workingLabel ?? workingText);
      workingText = '';
      workingLabel = null;
89 90 91 92
      yield info;
    } else {
      workingText += info.text;
      workingLabel ??= '';
93 94 95
      final String? infoSemanticsLabel = info.semanticsLabel;
      if (infoSemanticsLabel != null) {
        workingLabel += infoSemanticsLabel;
96 97 98 99 100
      } else {
        workingLabel += info.text;
      }
    }
  }
101
  assert(workingLabel != null);
102 103 104 105
}

Iterable<InlineSpanSemanticsInformation> combineSemanticsInfoList(List<InlineSpanSemanticsInformation> inputs) {
  String workingText = '';
106
  String? workingLabel;
107
  final List<InlineSpanSemanticsInformation> result = <InlineSpanSemanticsInformation>[];
108
  for (final InlineSpanSemanticsInformation info in inputs) {
109
    if (info.requiresOwnNode) {
110 111 112
      result.add(InlineSpanSemanticsInformation(workingText, semanticsLabel: workingLabel ?? workingText));
      workingText = '';
      workingLabel = null;
113 114 115 116
      result.add(info);
    } else {
      workingText += info.text;
      workingLabel ??= '';
117 118 119
      final String? infoSemanticsLabel = info.semanticsLabel;
      if (infoSemanticsLabel != null) {
        workingLabel += infoSemanticsLabel;
120 121 122 123 124
      } else {
        workingLabel += info.text;
      }
    }
  }
125
  assert(workingLabel != null);
126 127
  return result;
}