| Index: pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart
|
| diff --git a/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart b/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart
|
| index ee909234851c7681b04721fa3fdc39eb122ef938..5a030f74771ea093d0c5666d540913b796c183a0 100644
|
| --- a/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart
|
| +++ b/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart
|
| @@ -364,25 +364,86 @@ dgsendRepl(obj, typeArgs, method, @rest args) =>
|
| class _MethodStats {
|
| final String typeName;
|
| final String frame;
|
| - int count;
|
| + double count;
|
|
|
| _MethodStats(this.typeName, this.frame) {
|
| - count = 0;
|
| + count = 0.0;
|
| }
|
| }
|
|
|
| +class _CallMethodRecord {
|
| + var jsError;
|
| + var type;
|
| +
|
| + _CallMethodRecord(this.jsError, this.type);
|
| +}
|
| +
|
| Map<String, _MethodStats> _callMethodStats = new Map();
|
|
|
| +// Randomly sampled list of call records
|
| +List<_CallMethodRecord> _callMethodRecords = new List();
|
| +int totalCallRecords = 0;
|
| +int _callRecordSampleSize = 5000;
|
| +num _minCount = 0;
|
| +
|
| +/// Mapping from raw stack frames to source mapped stack frames to speedup
|
| +/// lookup of source map frames.
|
| +Map<String, String> _frameMappingCache = new Map();
|
| +
|
| List<List<Object>> getDynamicStats() {
|
| List<List<Object>> ret = [];
|
| + // Process the accumulated method stats. This may be quite slow as processing
|
| + // stack traces is expensive. If there are performance blockers, we should
|
| + // switch to a sampling approach that caps the number of _callMethodRecords
|
| + // and uses random sampling to decide whether to add each additional record
|
| + // to the sample. Main change required is that we need to still show the total
|
| + // raw number of dynamic calls so that the magnitude of the dynamic call
|
| + // performance hit is clear to users.
|
| +
|
| + // Minimum number of samples to consider a profile entry relevant.
|
| + // This could be set a lot higher. We set this value so users are not
|
| + // confused into thinking that a dynamic call that occurred once but was
|
| + // randomly included in the sample is relevant.
|
| + if (_callMethodRecords.length > 0) {
|
| + // Ratio between total record count and sampled records count.
|
| + var recordRatio = totalCallRecords / _callMethodRecords.length;
|
| + _minCount = recordRatio * 1.99;
|
| + for (var record in _callMethodRecords) {
|
| + var stackStr = JS('String', '#.stack', record.jsError);
|
| + // Truncate the stacktrace as we are only interested in the top 3-4 frames.
|
| + var frames = stackStr.split('\n');
|
| + var src = '';
|
| + for (int i = 2; i < frames.length; ++i) {
|
| + var frame = frames[i];
|
| + var mappedFrame = _frameMappingCache.putIfAbsent(frame, () {
|
| + return stackTraceMapper('\n${frame}');
|
| + });
|
| + if (!mappedFrame.contains(
|
| + 'dart:_internal/js_runtime/lib/ddc_runtime/operations.dart')) {
|
| + src = mappedFrame;
|
| + break;
|
| + }
|
| + }
|
|
|
| + var actualTypeName = typeName(record.type);
|
| + _callMethodStats
|
| + .putIfAbsent("$actualTypeName <$src>",
|
| + () => new _MethodStats(actualTypeName, src))
|
| + .count += recordRatio;
|
| + }
|
| + }
|
| + // filter out
|
| + _callMethodRecords.clear();
|
| + totalCallRecords = 0;
|
| var keys = _callMethodStats.keys.toList();
|
|
|
| keys.sort(
|
| (a, b) => _callMethodStats[b].count.compareTo(_callMethodStats[a].count));
|
| for (var key in keys) {
|
| var stats = _callMethodStats[key];
|
| - ret.add([stats.typeName, stats.frame, stats.count]);
|
| + if (_minCount < stats.count) {
|
| + ret.add([stats.typeName, stats.frame, stats.count.round()]);
|
| + }
|
| }
|
|
|
| return ret;
|
| @@ -390,30 +451,28 @@ List<List<Object>> getDynamicStats() {
|
|
|
| clearDynamicStats() {
|
| _callMethodStats.clear();
|
| + _callMethodRecords.clear();
|
| }
|
|
|
| bool trackProfile = JS('bool', 'dart.global.trackDdcProfile');
|
|
|
| _trackCall(obj) {
|
| if (JS('bool', '!#', trackProfile)) return;
|
| -
|
| - var actual = getReifiedType(obj);
|
| - String stackStr = JS('String', "new Error().stack");
|
| - var stack = stackStr.split('\n at ');
|
| - var src = '';
|
| - for (int i = 2; i < stack.length; ++i) {
|
| - var frame = stack[i];
|
| - if (!frame.contains('dart_sdk.js')) {
|
| - src = frame;
|
| - break;
|
| - }
|
| + int index = -1;
|
| + totalCallRecords++;
|
| + if (_callMethodRecords.length == _callRecordSampleSize) {
|
| + // Unfortunately we can't use the excellent Random.nextInt method defined
|
| + // by Dart from within this library.
|
| + index = JS('int', 'Math.floor(Math.random() * #)', totalCallRecords);
|
| + if (index >= _callMethodRecords.length) return; // don't sample
|
| + }
|
| + var record =
|
| + new _CallMethodRecord(JS('', 'new Error()'), getReifiedType(obj));
|
| + if (index == -1) {
|
| + _callMethodRecords.add(record);
|
| + } else {
|
| + _callMethodRecords[index] = record;
|
| }
|
| -
|
| - var actualTypeName = typeName(actual);
|
| - _callMethodStats
|
| - .putIfAbsent(
|
| - "$actualTypeName <$src>", () => new _MethodStats(actualTypeName, src))
|
| - .count++;
|
| }
|
|
|
| /// Shared code for dsend, dindex, and dsetindex.
|
|
|