Skip to content

array_unique is quadratic (3300x slower than php at 10k elements) #277

Closed

Description

@blacktrs

Problem

array_unique is quadratic. Input of 2 500 strings takes 31 ms; 10 000 strings take 528 ms (x16.7 for 4x the input). php stays linear: 34 us → 157 us for the same inputs, so the native build is about 3 300x slower at 10 000 elements.

Repro

$w = array_map(fn($i) => "w$i", range(0, 10000));
$t = hrtime(true);
array_unique($w);
echo (hrtime(true) - $t) / 1e6, " ms\n";   // php ~0.16 ms, native ~520 ms

Measured with bin/manticore built from f7924542 on arm64 macOS.

Expected

Linear in the number of elements for scalar values: keep a hash set of seen values (array_unique compares (string) representations under the default SORT_STRING flag), with a fallback only for the flags that need pairwise comparison.

Activity

  1. added
    bug
    on Oct 16, 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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions