finalizeScan method

  1. @override
void finalizeScan()
override

Finalise the scan: process accumulated data and create issues.

Called once after the unified tree walk completes.

Implementation

@override
void finalizeScan() {
  _imageFrames.clear();
  _evaluateUses();
  if (_uncachedImages.isEmpty) {
    _pendingHighlights.clear();
    return;
  }
  final totalWasted = _uncachedImages.fold<int>(
    0,
    (sum, img) => sum + img.wastedBytes,
  );
  if (totalWasted < minWastedBytes) {
    _pendingHighlights.clear();
    return;
  }

  final count = _uncachedImages.length;
  final widgetCount = _uncachedImages.fold<int>(
    0,
    (sum, img) => sum + img.widgetCount,
  );
  final worstRatio = _uncachedImages.map((img) => img.ratio).reduce(math.max);
  final severity = totalWasted >= criticalWastedBytes
      ? IssueSeverity.critical
      : IssueSeverity.warning;
  final top = [..._uncachedImages]
    ..sort((a, b) => b.wastedBytes.compareTo(a.wastedBytes));
  final imageList = top
      .take(5)
      .map(
        (img) =>
            '  • ${img.sourceName}: decoded '
            '${img.decodedWidth}×${img.decodedHeight} px, shown at '
            '${_dp(img.displayedWidth)}×${_dp(img.displayedHeight)} dp '
            '@ ${_dp(img.devicePixelRatio)}x'
            '${img.widgetCount > 1 ? '; shown by ${img.widgetCount} widgets' : ''}'
            '\n    in ${img.ancestorChain}',
      )
      .join('\n');

  final (hint, effort) = FixHintBuilder.uncachedImages(count: count);

  _issues.add(
    PerformanceIssue(
      stableId: 'uncached_images',
      severity: severity,
      category: IssueCategory.memory,
      confidence: IssueConfidence.likely,
      title:
          'Oversized Images: $count decoded above display size '
          '(worst ${worstRatio.toStringAsFixed(1)}×, '
          '~${_mb(totalWasted)} MB wasted)',
      detail:
          '$count image${count == 1 ? ' was' : 's were'} decoded at '
          'least ${oversizeRatio.toStringAsFixed(1)} times the physical '
          'pixels their display box needs'
          '${widgetCount > count ? ' (shown by $widgetCount widgets)' : ''}'
          '. This wastes about ${_mb(totalWasted)} MB of decoded memory.'
          '\n\n$imageList',
      fixHint: hint,
      fixEffort: effort,
      observationSource: ObservationSource.structural,
      confidenceReason:
          'Measured decoded size against display size. The widget may '
          'still grow later',
      extraTraceArgs: {
        'imageCount': '$count',
        'widgetCount': '$widgetCount',
        'worstRatio': worstRatio.toStringAsFixed(2),
        'wastedBytes': '$totalWasted',
      },
      measurement: IssueMeasurement(
        value: totalWasted.toDouble(),
        unit: MeasurementUnit.bytes,
        warningAt: minWastedBytes.toDouble(),
        criticalAt: criticalWastedBytes.toDouble(),
      ),
      detectedAt: DateTime.now(),
    ),
  );
  for (final h in _pendingHighlights) {
    _highlights.add(
      WidgetHighlight(
        rect: h.rect,
        renderObject: h.renderObject,
        occurrenceId: h.occurrenceId,
        stableId: 'uncached_images',
        widgetName: 'Image',
        severity: severity,
        detectorName: 'Image',
        detail: h.detail,
      ),
    );
  }
  _pendingHighlights.clear();
}