Skip to content

perf: recover scripts.zip load regression - #1079

Merged
CppCXY merged 4 commits into
EmmyLuaLs:mainfrom
lewis6991:perf/issue-1075-followups
May 14, 2026
Merged

CppCXY merged 4 commits into
EmmyLuaLs:mainfrom
lewis6991:perf/issue-1075-followups

Conversation

@lewis6991

@lewis6991 lewis6991 commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The scripts.zip workload regressed heavily after 0.21.0. On current upstream/main, loading the extracted scripts/ workspace is about 3.28x slower than 0.21.0.

This PR targets the hot paths from that workload without changing the broader inference result model:

  • avoid repeated full-flow owner inference for indexed assignments
  • skip assignment antecedent queries when the RHS type will replace the old flow type anyway
  • short-circuit direct index replay dependencies when the direct replay succeeds
  • short-circuit identical intersections before expanding union intersections

Performance

Benchmark command shape:

target/debug/emmylua_perf compare --baseline upstream/main --head HEAD --project /tmp/emmylua-scripts/scripts --iterations 5 --no-fetch
target/debug/emmylua_perf compare --baseline 0.21.0 --head HEAD --project /tmp/emmylua-scripts/scripts --iterations 5 --no-fetch

The project is the extracted scripts/ directory from https://github.com/user-attachments/files/27706685/scripts.zip. Times are mean ± 95% CI from release emmylua_ls builds.

Against current upstream/main:

upstream/main (8c01eeca):        total 9.009s ± 0.850s, index 8.708s ± 0.915s, rss 397.5 MB ± 2.6 MB
perf/issue-1075-followups clean: total 2.073s ± 0.103s, index 1.800s ± 0.013s, rss 391.1 MB ± 5.4 MB

That is a 77.0% total load-time improvement and a 79.3% index-time improvement, or about 4.35x faster total load time on this workload.

Relative to 0.21.0:

0.21.0:                          total 2.744s ± 0.076s, index 2.452s ± 0.032s, rss 377.4 MB ± 3.6 MB
perf/issue-1075-followups clean: total 2.117s ± 0.109s, index 1.830s ± 0.037s, rss 392.4 MB ± 4.6 MB

This closes the previous 0.21.0 gap for this workload. The branch is about 22.8% faster than 0.21.0 in total load time and about 25.4% faster in index time. RSS is listed for completeness; against current upstream/main, this branch is lower RSS in this run.

Tests

cargo test -p emmylua_code_analysis
cargo build --release -p emmylua_ls
target/debug/emmylua_perf compare --baseline upstream/main --head HEAD --project /tmp/emmylua-scripts/scripts --iterations 5 --no-fetch
target/debug/emmylua_perf compare --baseline 0.21.0 --head HEAD --project /tmp/emmylua-scripts/scripts --iterations 5 --no-fetch

lewis6991 added 3 commits May 13, 2026 15:58
Use no-flow inference when indexed assignment analysis only needs the
member owner, and avoid expanding non-variadic unions when selecting
the first result slot.

Assisted-by: Codex
Finish broad non-unknown assignment RHS types directly instead of querying
the previous flow type and then discarding it.

Assisted-by: Codex
When flow replay can resolve the direct IndexRef dependency, the
replay overlay already lets no-flow inference return that expression
without walking its prefix/key dependencies. Keep those fallback
dependencies queued only for direct misses.

Assisted-by: Codex
@github-actions

Copy link
Copy Markdown

Code Review Summary

Issues Found:

  1. Potential Logic Error in stats.rs (line 208-214):

    • The fallback logic Ok(None) => analyzer.infer_expr(&prefix_expr) could cause infinite recursion if infer_expr_no_flow consistently returns None while infer_expr also fails silently. Consider adding a recursion guard or max retry count.
  2. Missing Error Handling in resolve.rs (line 51):

    • The try_infer_expr_no_flow call uses ? which propagates errors, but the comment says "fall back so we keep the retry." If try_infer_expr_no_flow returns Err, the function will exit early without attempting the fallback. This contradicts the stated intent.
  3. Performance Concern in predicates.rs (line 209):

    • The new union_contains_multi_return function is called multiple times in get_result_slot_type. For large union types, this could be inefficient. Consider caching the result or restructuring the logic.
  4. Potential Index Out of Bounds in get_type_at_flow.rs (line 215):

    • dependency_query.next_dependency_idx_on_success could be Some with a value greater than dependency_queries.len() if the direct_query_idx logic adds a query but the prefix/key queries don't add enough entries. This would cause a panic when accessing dependency_queries later.
  5. Inconsistent Comment Style (multiple files):

    • Comments use different styles (//, // with space, //!). Standardize to improve readability.

Recommendations:

  1. In stats.rs: Add a recursion depth counter or limit retries to prevent infinite loops:

    fn set_index_expr_owner(analyzer: &mut LuaAnalyzer, var_expr: LuaVarExpr, depth: u8) -> Option<...> {
        if depth > MAX_RETRY_DEPTH { return None; }
        // ... existing logic with depth+1 on fallback
    }
  2. In resolve.rs: Change the error handling to attempt fallback on errors:

    let prefix_type = match try_infer_expr_no_flow(db, cache, prefix_expr.clone()) {
        Ok(Some(prefix_type)) => prefix_type,
        _ => infer_expr(db, cache, prefix_expr.clone())?,
    };
  3. In predicates.rs: Cache the result of union_contains_multi_return or compute it once:

    let has_multi_return = union_contains_multi_return(union);
    if idx == 0 && !has_multi_return { ... }
  4. In get_type_at_flow.rs: Add bounds checking before using next_dependency_idx_on_success:

    let next_idx = next_dependency_idx_on_success
        .filter(|&idx| idx < dependency_queries.len())
        .unwrap_or(self.next_dependency_idx + 1);
  5. Add unit tests for edge cases:

    • Test when try_infer_expr_no_flow returns None and infer_expr also fails
    • Test deeply nested unresolved prefixes
    • Test union types with mixed multi-return and non-multi-return members
  6. Documentation: Add doc comments to union_contains_multi_return explaining its purpose and behavior.

Positive Aspects:

  • The test case test_nested_unresolved_prefix_keeps_member_owner_retry is well-written and covers the intended fix.
  • The performance optimization in get_type_at_flow.rs (early return for non-unknown types) is good.
  • The refactoring of union_contains_multi_return improves code clarity.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves Lua type inference by refining how unresolved prefixes and index expressions are handled. Key changes include implementing a fallback mechanism in set_index_expr_owner and try_resolve_member to ensure retries for nested unresolved prefixes, optimizing flow replay queries with short-circuiting for resolved index expressions, and improving the handling of union types in multi-return scenarios. Additionally, it optimizes assignment processing in the flow type engine by avoiding unnecessary antecedent queries for broad RHS types. A new test case was added to verify the fix for nested unresolved prefixes. I have no feedback to provide.

Avoid routing `A & A` through the union intersection arms. This preserves the existing result while skipping pairwise union work in flow narrowing.

Assisted-by: Codex
@CppCXY
CppCXY merged commit f360ff2 into EmmyLuaLs:main May 14, 2026
16 checks passed
@lewis6991
lewis6991 deleted the perf/issue-1075-followups branch May 14, 2026 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants