Skip to content

Optimize Fw::StringUtils::string_length with SWAR algorithm #4788

@vsoulgard

Description

@vsoulgard
F´ Version 4.x
Affected Component Fw/Types

Feature Description

I propose optimizing Fw::StringUtils::string_length by replacing the naive byte-by-byte iteration with a SWAR (SIMD Within A Register) algorithm using word-sized integer registers.

Rationale

A SWAR-based approach can significantly improve performance and CPU efficiency during string length calculation, especially for long strings.

Proposed Solution

Implement a SWAR-based algorithm that loads data in word-sized chunks (size_t) and uses bitwise operations to detect the NUL char in parallel within the register.

Requirements

To ensure safety and correctness, the solution must meet the following criteria:

  1. We need to handle unaligned pointers safely to avoid hardware faults on architectures that do not support unaligned access. Additionally, prevent reading across memory page boundaries.
  2. AddressSanitizer may flag the over-read as an error if passed buffer_length is bigger than expected. We need to disable ASan for this specific function.
  3. Add new unit tests for edge cases (unaligned pointer, different lengths).
  4. Benchmark existing vs. new implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    CCB

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions