From b26307132d8cdf36f1fe32094333b9f9b9ad0f91 Mon Sep 17 00:00:00 2001 From: "Flavio S. Glock" Date: Sat, 19 Sep 2026 04:12:12 +0200 Subject: [PATCH 1/2] perf: avoid redundant unsigned bitwise masks Reuse values already within the unsigned 64-bit range instead of rebuilding BigInteger backing arrays to apply an identical mask. Negative and out-of-range values retain the modular masking path. Generated with Codex (https://openai.com/codex) Co-Authored-By: Codex --- .../runtime/operators/BitwiseOperators.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/perlonjava/runtime/operators/BitwiseOperators.java b/src/main/java/org/perlonjava/runtime/operators/BitwiseOperators.java index 381da5eee..fe1aa4f65 100644 --- a/src/main/java/org/perlonjava/runtime/operators/BitwiseOperators.java +++ b/src/main/java/org/perlonjava/runtime/operators/BitwiseOperators.java @@ -17,10 +17,20 @@ public class BitwiseOperators { private static final BigInteger UV_MASK = BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE); private static BigInteger unsignedValue(RuntimeScalar scalar) { - return scalar.getBigint().and(UV_MASK); + BigInteger value = scalar.getBigint(); + // Numeric bitwise operations are modulo 2^64. Their own results are + // already stored in that range, so avoid rebuilding BigInteger's + // backing array just to apply the same mask on the next operation. + if (value.signum() >= 0 && value.bitLength() <= 64) { + return value; + } + return value.and(UV_MASK); } private static RuntimeScalar unsignedResult(BigInteger value) { + if (value.signum() >= 0 && value.bitLength() <= 64) { + return new RuntimeScalar(value); + } return new RuntimeScalar(value.and(UV_MASK)); } From 0afd9877206665eb1ddc16b3d99a8c5c1498bc15 Mon Sep 17 00:00:00 2001 From: "Flavio S. Glock" Date: Sat, 19 Sep 2026 05:32:29 +0200 Subject: [PATCH 2/2] docs: record unsigned bitwise performance improvement Generated with Codex (https://openai.com/codex) Co-Authored-By: Codex --- docs/about/changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/about/changelog.md b/docs/about/changelog.md index 2a6e9ee11..71186a314 100644 --- a/docs/about/changelog.md +++ b/docs/about/changelog.md @@ -6,6 +6,9 @@ priorities and future plans. ## Work in progress +- Avoid redundant modular masking of already unsigned 64-bit bitwise values, + improving general bitwise-heavy workloads such as Life. + - Restore `local` compatibility for tied hash and array elements, sparse arrays, magic stashes, implicit `$_` foreach aliases (including early return), and localized regex captures on both execution backends.