Skip to content

perf(database): eliminate redundant string and list allocations in postgresResultCellToDisplayString #1521

Open

Description

@ZhuchkaTriplesix

Summary

In lib/core/database/postgres_result_cells.dart (lines 53–85):

  1. Date-Only Formatting:
    if (value is DateTime) {
      if (_isDateOnly(typeOid, dataTypeName)) {
        return value.toIso8601String().split('T').first;
      }
      return value.toIso8601String();
    }
    Calling toIso8601String().split('T').first allocates a full 24-character string, a 2-element list, and the first element. On 100,000 date cells, this creates 200,000 transient objects.
  2. Bytea / Binary Hex Encoding:
    String postgresByteaHexDisplay(Uint8List bytes) {
      final out = StringBuffer(r'\x');
      for (final b in bytes) {
        out.write(b.toRadixString(16).padLeft(2, '0'));
      }
      return out.toString();
    }
    b.toRadixString(16).padLeft(2, '0') allocates 2 new String objects per byte. For a 10 KB binary cell, this creates 20,000 String allocations for a single cell!

Proposed Solution

  1. Replace toIso8601String().split('T').first with a dedicated date-only formatter:
    static String _formatDateOnly(DateTime d) =>
        '${d.year.toString().padLeft(4, '0')}-${d.month.toString().padLeft(2, '0')}-${d.day.toString().padLeft(2, '0')}';
  2. Use a precomputed 256-entry static lookup table for byte-to-hex conversion:
    const _hexTable = ['00', '01', ..., 'fe', 'ff'];
    out.write(_hexTable[b]); // O(1) array lookup with zero string allocations

Acceptance Criteria

  • Date-only cells do not call split('T').
  • Bytea hex formatting performs zero toRadixString or padLeft allocations.
  • Full parity with existing result cell display tests in test/core/database/postgres_result_cells_test.dart.

Activity

  1. added
    performanceTheme parser epic label: performance
    coreCore library logic and services
    backendBackend database driver execution and queries
    P3Low priority / Polish & Enhancements
    on Oct 12, 2026
  2. added this to the 0.5.0 milestone on Oct 12, 2026
  3. added a commit that references this issue on Oct 15, 2026
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 & EnhancementsbackendBackend database driver execution and queriescoreCore library logic and servicesperformanceTheme parser epic label: performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions