Skip to content

String/array functions 5-150x slower than php (linear): strrev, base64, in_array, ucwords, nl2br #278

Open

Description

@blacktrs

Problem

A sweep of ~40 string/array functions at 20 000 and 80 000 byte inputs (native bin/manticore from f7924542 against php 8.5, arm64 macOS) found everything linear except array_unique (#277). Several functions are linear but far behind php, which points at a per-byte loop where php uses a bulk routine, the same shape as the str_repeat fix in #274 (one memcpy per repetition):

function native php ratio
strrev 310 us 2 us 155x
base64_encode+decode 1128 us 15 us 75x
in_array (miss, 2.5k strings) 384 us 13 us 30x
ucwords 1373 us 56 us 25x
nl2br 964 us 50 us 19x
addslashes 56 us 4 us 14x
bin2hex+hex2bin 1842 us 193 us 9.5x
array_slice / array_splice / array_merge 99 / 204 / 147 us 15 / 40 / 29 us 5-7x
strtolower, trim, range, array_reverse 4-6x
sort (10k strings) 4660 us 1183 us 3.9x

Native is already faster for usort (5x), htmlspecialchars (5x), str_concat_loop and array_unshift loops.

Proposal

Profile each top row for its loop shape and replace per-byte / per-element helpers with bulk or word-wise ones. Start from the ratio: strrev, base64, in_array, ucwords, nl2br. Each is small and ships alone; keep the sweep script with the first fix so the table can be re-measured.

Acceptance criteria

  • Every function above within 2x of php at 80 000 bytes, or a note saying why not.
  • A case per function with sizes around the word / block boundaries (0, 1, 7, 8, 9, 15, 16, 17), as str_repeat_sizes does.

Activity

  1. added
    enhancement
    on Oct 16, 2026
  2. blacktrs commented on Oct 16, 2026

    @blacktrs
    ContributorAuthor

    Most rows landed in #274 (strrev, ucwords, nl2br, trim, base64, bin2hex/hex2bin, str_pad, strtr, serialize, in_array, array_slice/merge/reverse, range, sort for int and string lists, str_repeat, array_unique). Still open: array_flip and array_splice (#285), strtolower (3x, scalar loop LLVM does not vectorize), base64 (5-8x, php uses SIMD), shuffled sort (1.7x), in_array over a bare untyped array (10x). addslashes did not reproduce as slow; the real bug was NUL handling (#279).

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions