perf: fast path len identifier calls#136
Conversation
Greptile SummaryThis PR adds a targeted fast path for
Confidence Score: 5/5Safe to merge; the fast path is strictly additive and falls through to the existing normal dispatch on any non-matching case. The fast path's len_of_value is byte-for-byte identical to the existing builtin's match arms. Shadowing, type errors, and parent-scope traversal are all covered by new unit tests. The iterative get rewrite is tested with a 64-level depth chain. No behavioral changes are possible for callers that do not hit the narrow len(identifier) pattern. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Expression::Call evaluated"] --> B{"function is Identifier?"}
B -- No --> Z["Normal call dispatch"]
B -- Yes --> C{"name is len AND 1 argument?"}
C -- No --> D{"name is old?"}
D -- Yes --> OLD["old() postcondition handling"]
D -- No --> Z
C -- Yes --> E{"argument is Identifier?"}
E -- No --> Z
E -- Yes --> F["borrow env, get len"]
F --> G{"resolves to NativeFunction named len?"}
G -- No --> Z
G -- Yes --> H["borrow env, len_of_binding"]
H --> I{"binding found?"}
I -- No --> Z
I -- Yes --> J["len_of_value without clone"]
J --> K{"value type?"}
K -- String --> L["Int s.len()"]
K -- Array --> M["Int a.len()"]
K -- Map --> N["Int m.len()"]
K -- Other --> O["TypeError"]
Z --> P["eval function expr, clone arg, NativeFunction dispatch"]
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A["Expression::Call evaluated"] --> B{"function is Identifier?"}
B -- No --> Z["Normal call dispatch"]
B -- Yes --> C{"name is len AND 1 argument?"}
C -- No --> D{"name is old?"}
D -- Yes --> OLD["old() postcondition handling"]
D -- No --> Z
C -- Yes --> E{"argument is Identifier?"}
E -- No --> Z
E -- Yes --> F["borrow env, get len"]
F --> G{"resolves to NativeFunction named len?"}
G -- No --> Z
G -- Yes --> H["borrow env, len_of_binding"]
H --> I{"binding found?"}
I -- No --> Z
I -- Yes --> J["len_of_value without clone"]
J --> K{"value type?"}
K -- String --> L["Int s.len()"]
K -- Array --> M["Int a.len()"]
K -- Map --> N["Int m.len()"]
K -- Other --> O["TypeError"]
Z --> P["eval function expr, clone arg, NativeFunction dispatch"]
Reviews (2): Last reviewed commit: "test: cover len fast path parent scopes" | Re-trigger Greptile |
Summary
len(identifier)so large arrays/maps are not cloned just to compute length.lenshadowing behavior intact; user-definedlenstill wins./native/callsbenchmark fixture and records the DD-061 PR 5 result/recommendation update.Benchmark
Local 3s/3-run
wrkpass at 16 connections / 2 threads, comparingorigin/mainplus the new benchmark fixture to this branch's dev-release binary:/native/calls:349.95->1574.70RPS (+350.0%)Verification
cargo fmtcargo build --profile dev-releasecargo test./target/dev-release/ntnt docs --generatecargo fmt -- --checkgit diff --check./target/dev-release/ntnt lint examples/perf/server.tnt— 0 errors / 0 warnings; existing benchmark-file contract suggestions onlycargo test fast_path --lib,cargo test environment_get --lib