Skip to content

Medium: FixExponentNotation byte-by-byte scan with per-number ParseFloat #25

Description

@metis-sprock

Description

FixExponentNotation processes data character by character using append-based byte accumulation, with frequent slice reallocation. Each candidate number triggers a strconv.ParseFloat call. The overall approach is 2-3x slower than necessary.

Evidence

File: pkg/codec/echoreplay.go:333-411

func FixExponentNotation(data []byte) []byte {
    var result []byte
    // byte-by-byte iteration
    for i := 0; i < len(data); i++ {
        // character-by-character append to result
        result = append(result, data[i])
        // ...
        // ParseFloat on every candidate number
        f, err := strconv.ParseFloat(numStr, 64)
        formatted := strconv.FormatFloat(f, 'f', -1, 64)
        // ...
    }
}

Each append may trigger reallocation. Each number candidate triggers float parsing even when no exponent notation is present.

Impact

Conversion performance bottleneck. This function runs on every frame during legacy format conversion. The byte-by-byte approach with frequent reallocation makes it a measurable hot spot on large replay files.

Suggested Fix

  1. Pre-allocate result with make([]byte, 0, len(data))
  2. Use regex to find exponent-notation numbers, only parse those
  3. Copy non-matching regions in bulk with copy() or append(result, data[start:end]...)

🤖 Filed by Metis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions