Skip to content

perf(grid): eliminate string allocations in GridSelectionCalcEngine via zero-allocation formatted number scanner #1520

Open

Description

@ZhuchkaTriplesix

Summary

In lib/features/workspace/grid_selection_calc_engine.dart (lines 125–147):

When a user drags a selection box across the data grid, compute(List<String> values) recalculates statistics (Sum, Avg, Median, Min, Max) for every pointer move:

for (final raw in values) {
  final trimmed = raw.trim(); // Allocation 1
  if (trimmed == 'NULL' || trimmed.isEmpty) { ... }
  distinctSet.add(trimmed);

  final normalized = trimmed.replaceAll(',', ''); // Allocation 2
  final parsed = double.tryParse(normalized);
  ...
}

When selecting 5,000 cells:

  • Every mouse move triggers 10,000 String allocations (trim() + replaceAll(',', '')).
  • This causes stuttering and lag in the selection rectangle and delayed status bar statistics updates.

Architectural Solution: Zero-Allocation Micro-Scanner

Implement a zero-allocation number parser that handles formatted numbers directly:

static double? fastParseFormattedNumber(String s) {
  // Scans s.codeUnitAt(i), skipping leading/trailing whitespace and thousands commas (0x2C)
  // Parses sign, digits, decimal point, and exponent directly to double without allocations.
}

Acceptance Criteria

  • Statistical calculations perform zero replaceAll(',', '') and zero intermediate trim() allocations.
  • Drag-selection remains responsive at 120 FPS on selections of 5,000+ cells.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low priority / Polish & Enhancementsdata-gridInteractive data grid, cell editor, filtering, groupingsfrontendTheme parser epic label: frontendperformanceTheme parser epic label: performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions