Skip to content

JS Map.msb.u shifts through Base for a bounded U32 bit-width query #1703

Closed as not planned

Description

@oxura

Map.msb.u finds a U32's bit width one shift at a time, bounded by its Nat fuel. It is also the bit-width worker used by Map.diff. The JS host has an exact constant-time clz32 primitive for this.

Repro wrapper:

import Base
def width(fuel: Nat, x: U32) -> Nat:
  Map.msb.u(fuel, x)

Nine warmed alternating rounds of 100,000 exported calls over varying U32s/fuel: median 10.386 ms before, 2.859 ms after; checksum 2,064,398 on both. A real consumer call, Map.diff('x', '0'), improved from 14.364 ms to 11.052 ms for 100,000 calls. Linux x64/Bun 1.3.13, baseline #1682 head 0b82c20. No gain is claimed for a long-prefix Map.diff workload, where its separate String walk dominates.

The lowering is min(fuel, 32 - clz32(x)). Independent bit-length checks include zero fuel, zero value, high-bit U32s, truncated fuel and fuel above 2^32. C keeps its existing Base path.

Patch: oxura@aa331dfd
Included in existing PR #1682.

Activity

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