Generate direct string lengths and unit-step slices - #7058
Conversation
Greptile SummaryThis PR optimizes generated JavaScript for StringVar length and ordinary slicing while preserving UTF-16 semantics and Var metadata.
Confidence Score: 4/5The PR appears safe to merge, with a non-blocking gap where literal NumberVar unit steps miss the intended optimization. Direct string length and slicing preserve existing semantics and metadata, while the sole accepted issue is limited to avoidable allocation for a supported statically known step representation. Files Needing Attention: packages/reflex-base/src/reflex_base/vars/sequence.py; tests/units/reflex_base/vars/test_sequence.py
|
| Filename | Overview |
|---|---|
| packages/reflex-base/src/reflex_base/vars/sequence.py | Adds direct string length and unit-slice operations with correct metadata and UTF-16 behavior, but does not recognize literal NumberVar steps equal to one. |
| tests/units/reflex_base/vars/test_sequence.py | Adds strong metadata, fallback-path, and JavaScript semantic coverage, but omits the supported literal-NumberVar unit-step case. |
| tests/benchmarks/test_string_operations.py | Adds focused CodSpeed benchmarks for Python-side generation of string length and direct slice expressions. |
| tests/units/test_var.py | Updates established expression assertions to the new direct JavaScript operations. |
| packages/reflex-base/news/+direct-string-operations.performance.md | Concisely documents the downstream string-operation performance improvement. |
Reviews (1): Last reviewed commit: "perf: generate direct string lengths and..." | Re-trigger Greptile
| if i.step is None or (isinstance(i.step, int) and i.step == 1): | ||
| return _string_slice_operation( |
There was a problem hiding this comment.
A unit step represented as a constant LiteralNumberVar, such as LiteralNumberVar.create(1), is supported by the existing slice machinery but fails the isinstance(i.step, int) check. It therefore still generates the allocation-heavy split("").slice(...).join("") path, leaving the intended constant-unit-step optimization incomplete. Recognize integer literal Vars whose value is 1 and add a regression case alongside the Python int case.
Merging this PR will not alter performance
Performance Changes
Comparing Footnotes
|
String Var lengths and ordinary slices currently split the entire string into an array first. Generate direct
.lengthand.slice(start, stop)calls for omitted or constant integer unit steps, avoiding whole-string array allocation. Preserve current UTF-16 code-unit behavior and source/boundary Var metadata; other steps keep the existing array path.Validation:
pyright reflex testspassed.No new public API. Based directly on main, independent of the other performance changes.