Skip to content

Regex match performance: ~4x slower than native Perl #1904

Closed

Description

@fglock

Problem

The regex benchmark (dev/bench/benchmark_regex.pl) running $i =~ /42/ in a tight loop shows PerlOnJava at ~10,453 iterations/s vs native Perl at ~39,683/s — a 3.8x performance gap.

Analysis

Async-profiler reveals the Joni regex engine itself accounts for only ~2% of CPU. The overhead is dominated by per-match ceremony in the runtime plumbing:

  • ThreadLocal operations: ~21 per match (save/restore package, debug mode, warning state, source lifecycle)
  • Object allocations: ~10 per match (RuntimeList, Region, Matcher, ResolvedRegex)
  • Redundant calls: resolveRegex called twice, emitWarningsOnUse reads 4 ThreadLocals even when the warnings list is empty

Benchmark

# Native Perl
timethis 100000: @ 39683/s

# PerlOnJava (before)
timethis 100000: @ 10453/s

# PerlOnJava (after PR)
timethis 100000: @ 17430/s  (+67%)

Remaining gap

A ~2.3x gap remains vs native Perl. Further gains would require deeper changes:

  • Integer-to-string caching (dual PV/IV slot in RuntimeScalar)
  • Moving bytecode-emitted warning state from ThreadLocal to PerlRuntime fields
  • Joni Matcher reuse/reset API

These are deferred to future work.

Activity

  1. fglock commented on Oct 27, 2026

    @fglock
    OwnerAuthor

    The 2x speedup is delivered in PR #1905 (rounds 1-3). Follow-up issue #1907 tracks closing the remaining 1.8x gap to native Perl.

  2. fglock commented on Oct 27, 2026

    @fglock
    OwnerAuthor

    Resolved by PR #1905 — regex hot-path optimizations achieving ~2.1x throughput improvement (10,453/s → 22,100/s). Remaining ~1.8x gap to native Perl parity tracked in #1907.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions