perf: fast-path scalar arithmetic, comparisons, and common builtin operations

Merged
GA
19d ago
+125-14

Summary

Extracted from #1035 as requested in review comments to separate unrelated performance improvements from class inheritance.

This PR introduces targeted fast-paths for common scalar and builtin operations without altering Python semantics or resource tracking:

  • Scalar Arithmetic & String Concat (crates/monty/src/bytecode/vm/binary.rs): Direct fast-path integer arithmetic (+, -, *, %) and string concatenation (+, +=) bypassing generic method dispatch.
  • Scalar Comparisons (crates/monty/src/bytecode/vm/compare.rs): Fast-paths for (int, int) and (bool, bool) in equality and ordering comparisons (==, !=, <, <=, >, >=).
  • sorted() Builtin (crates/monty/src/builtins/sorted.rs): Direct-clone items from existing list and tuple instances with preflight and deadline checks, bypassing iterator allocation.
  • str.replace & str.encode (crates/monty/src/types/str.rs): Fast-path 2-argument interned string replacements and common default/interned encodings (utf-8, ascii) avoiding FromArgs parsing.
  • strftime & timedelta.days (crates/monty/src/types/date.rs, crates/monty/src/types/datetime.rs, crates/monty/src/types/timedelta.rs): Fast-paths for standard formats (%Y-%m, %Y-%m-%d, etc.) on naive instances and direct day calculation in timedelta.days. Includes integration tests in crates/monty/tests/datetime_format.rs.

Summary by cubic

Adds fast-paths for common scalar comparisons and for sorted() on lists and tuples without changing Python semantics or resource tracking.

  • Short-circuits ==, !=, <, <=, >, >= on ints and bools before generic method dispatch.
  • sorted() clones items directly from existing lists and tuples while preserving the same time and memory limit checks.
  • Extends resource-limit tests to cover the new sorted() copy path and isolates the deadline check to the sorting comparison loop.