code-size-factors.py 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 74 75 76 77 78 79
#!/usr/bin/env python3
# vim:fenc=utf-8:ts=2:sw=2:softtabstop=2:expandtab:
# Copyright 2022 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import sys
import re

liftoff_regex = re.compile('^Compiled function .* using Liftoff, '
                           '.*bodysize ([0-9]+) codesize ([0-9]+)$')
turbofan_regex = re.compile('^Compiled function .* using TurboFan, '
                            '.*bodysize ([0-9]+) codesize ([0-9]+) ')
wasm2js_regex = re.compile('^Compiled WasmToJS wrapper .* '
                           'codesize ([0-9]+)$')


def main():
  print('Reading --trace-wasm-compilation-times lines from stdin...')
  liftoff_values = []
  turbofan_values = []
  wasm2js_values = []
  for line in sys.stdin:
    match(line, liftoff_regex, liftoff_values)
    match(line, turbofan_regex, turbofan_values)
    match_wasm2js(line, wasm2js_values)

  evaluate('Liftoff', liftoff_values)
  evaluate('TurboFan', turbofan_values)
  evaluate_wasm2js(wasm2js_values)


def match(line, regex, array):
  m = regex.match(line)
  if m:
    array.append([int(m.group(1)), int(m.group(2))])


def match_wasm2js(line, array):
  m = wasm2js_regex.match(line)
  if m:
    array.append(int(m.group(1)))


def evaluate(name, values):
  n = len(values)
  if n == 0:
    print(f'No values for {name}')
    return

  print(f'Computing base and factor for {name} based on {n} values')
  sum_xy = sum(x * y for [x, y] in values)
  sum_x = sum(x for [x, y] in values)
  sum_y = sum(y for [x, y] in values)
  sum_xx = sum(x * x for [x, y] in values)

  factor = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - sum_x * sum_x)
  base = (sum_y - factor * sum_x) / n

  print(f'--> [{name}] Trend line: base: {base:.2f}, factor {factor:.2f}')

  min_y = min(y for [x, y] in values)

  simple_factor = (sum_y - n * min_y) / sum_x
  print(f'--> [{name}] Simple analysis: Min {min_y}, '
        f'factor {simple_factor:.2f}')


def evaluate_wasm2js(values):
  n = len(values)
  if n == 0:
    print('No wasm2js wrappers')
    return

  print(f'--> [Wasm2js wrappers] {n} compiled, size min {min(values)}, '
        f'max {max(values)}, avg {(sum(values) / n):.2f}')


main()