Skip to content

Avoid scanning the entire input to detect parseInt hexadecimal prefixes #198

Closed

Description

@dimensionscape

hxParseIntChars in src/hx/StdLibs.cpp calls is_hex_string(str, strlen(str)) after skipping whitespace. is_hex_string only checks the first few characters and whether the string is at least three or four characters long. Computing the full length scans an otherwise irrelevant suffix before integer parsing starts.

I reproduced this against checkout 03a4e8d on Linux x86_64 through WSL, Haxe 4.3.3, HXCPP_M64 and hxcpp_smart_strings. A native probe called __hxcpp_parse_int 500 times with preconstructed byte strings:

  • "7x": 0.006 ms, checksum 3500.
  • "7x" followed by 1048576 a characters: 3.768 ms, checksum 3500.

Both inputs stop parsing at x and return 7. Construction was outside the timed region. These single-run diagnostic measurements show unnecessary suffix-dependent work, not an estimated speedup for ordinary short numeric strings.

Detect the optional sign and hexadecimal prefix with bounded checks instead of strlen. Ensure each character exists before inspecting the next one; empty strings, a sign alone, "0", "0x" and signed incomplete prefixes must retain their current behavior. Avoid replacing a safe length check with an out-of-bounds short-string read.

The same helper serves substring integer parsing, so preserve that API's bounds and terminator behavior. Benchmark normal decimal numbers, hex, whitespace, short invalid inputs and short numeric prefixes with long suffixes. Keep overflow and saturation behavior unchanged.

Related: #151 for the performance suite. This specifically removes a full-input scan that is unnecessary for prefix detection.

Activity

  1. added a commit that references this issue on Oct 20, 2026
    fdf4961
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions