Skip to content

Commit 8461e5f

Browse files
[Autoloop: tsb-perf-evolve] Iteration 70: flat 4-if cache check (property-read before string-compare)
Reverts c069 (method extraction regression) back to c067 inline cold path, then replaces the if/else+ternary hot-path check with a flat 4-if chain. Hypothesis: direct property reads before naPosition string comparison lets the JSC/Bun JIT constant-fold the dominant ascending=true, naPosition=last case to a single property check and return. Run: https://github.com/githubnext/tsb/actions/runs/26858622799 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2015c39 commit 8461e5f

1 file changed

Lines changed: 7 additions & 16 deletions

File tree

src/core/series.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -757,23 +757,14 @@ export class Series<T extends Scalar = Scalar> {
757757

758758
/** Return a new Series sorted by values. */
759759
sortValues(ascending = true, naPosition: "first" | "last" = "last"): Series<T> {
760-
// ── Per-instance cache: named properties for direct access on the hot path ──
761-
// Eliminates the O(n) gather loop, inverse-transform, RangeIndex construction,
762-
// and Object.freeze spreads on all repeat calls with the same parameters.
763-
// The public method is kept tiny (~12 lines) so V8/Bun can inline it at all
764-
// call sites, collapsing the hot (cache-hit) path to a single property read.
765-
if (ascending) {
766-
const hit = naPosition === "last" ? this._svCacheAL : this._svCacheAF;
767-
if (hit !== null) return hit;
768-
} else {
769-
const hit = naPosition === "last" ? this._svCacheDL : this._svCacheDF;
770-
if (hit !== null) return hit;
771-
}
772-
return this._sortValuesCold(ascending, naPosition);
773-
}
760+
// ── Per-instance cache: flat 4-if chain reads property first, then validates
761+
// naPosition — lets the JIT constant-fold the dominant (ascending=true,
762+
// naPosition="last") case to a single property read and return.
763+
if (ascending && this._svCacheAL !== null && naPosition === "last") return this._svCacheAL;
764+
if (ascending && this._svCacheAF !== null && naPosition === "first") return this._svCacheAF;
765+
if (!ascending && this._svCacheDL !== null && naPosition === "last") return this._svCacheDL;
766+
if (!ascending && this._svCacheDF !== null && naPosition === "first") return this._svCacheDF;
774767

775-
/** Cold path: full LSD radix sort, gather loop, cache store. Only called once per (ascending, naPosition) pair. */
776-
private _sortValuesCold(ascending: boolean, naPosition: "first" | "last"): Series<T> {
777768
const n = this._values.length;
778769
const vals = this._values;
779770

0 commit comments

Comments
 (0)