Skip to content

fix: batch large member unions - #1077

Merged
CppCXY merged 1 commit into
EmmyLuaLs:mainfrom
lewis6991:fix/issue-1075-stuck-loading
May 13, 2026
Merged

CppCXY merged 1 commit into
EmmyLuaLs:mainfrom
lewis6991:fix/issue-1075-stuck-loading

Conversation

@lewis6991

Copy link
Copy Markdown
Collaborator

Summary

  • batch repeated member union folds through TypeOps::union_all
  • keep the batch helper semantically equivalent to repeated Union.apply
  • add regression coverage for the large dynamic-key table lookup from Stuck Loading #1075

Fixes #1075

Validation

  • cargo test -p emmylua_code_analysis test_union_all_keeps_union_semantics -- --nocapture
  • cargo test -p emmylua_code_analysis test_issue_1075_large_table_dynamic_string_key -- --nocapture
  • cargo test -p emmylua_code_analysis

@github-actions

Copy link
Copy Markdown

Code Review Summary

Performance Improvements ✅

The changes introduce a batch union operation (TypeOps::union_all) that replaces repeated pairwise Union.apply calls. This is a significant performance optimization for large tables (e.g., 256+ members) by reducing redundant type normalization and canonicalization.

Potential Issues

  1. can_use_structural_union function is overly conservative (union_type.rs:75-115)

    • The function returns false for many valid structural unions, forcing fallback to pairwise folding
    • Missing cases: Nil, Unknown, Global, Thread, Userdata, LightUserdata, Variadic, Def, Array, Map, Set, Tuple, MultiLineIntersection
    • Recommendation: Add more type variants or consider a simpler heuristic (e.g., only reject when Ref, Union, or callable types are present)
  2. Potential behavior change in infer_matching_member_key_type (infer_index.rs:478)

    • Changed from TypeOps::Union.apply to LuaType::from_vec for the final result
    • from_vec may not perform the same canonicalization as union_type (e.g., merging integer | number → number)
    • Recommendation: Use TypeOps::union_all instead of LuaType::from_vec for consistency
  3. Missing Nil handling in can_use_structural_union (union_type.rs)

    • Nil is not tracked, but Nil | T should be handled structurally
    • Recommendation: Add Nil to the tracked types or explicitly handle it
  4. Empty batch returns Never (union_type.rs:22-24)

    • This matches previous behavior but may surprise callers expecting Unknown or Nil
    • Recommendation: Document this behavior clearly in the function docstring

Code Quality Observations

  1. Unused import removed (intersect_type.rs:3) ✅ Good cleanup

  2. deref().clone() removed (multiple files) ✅ Good simplification

  3. Test coverage (test.rs:204-256) ✅ Good addition testing edge cases (empty, duplicates, callable normalization)

  4. Issue 1075 regression test (member_infer_test.rs:39-77) ✅ Good reproduction of the large table dynamic key issue

Security Concerns

None identified. The changes are purely type system optimizations.

Overall Assessment

The refactoring is well-motivated and correctly preserves semantics in most cases. The can_use_structural_union function needs refinement to maximize the performance benefit. The infer_matching_member_key_type change should use union_all for consistency with other call sites.

@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 introduces a centralized TypeOps::union_all utility to handle union operations across the codebase, replacing manual iterative TypeOps::Union.apply calls. This change improves performance and consistency in type inference, particularly for large tables and complex union types. It also includes a new test case for large table dynamic string keys to ensure stability. I have provided feedback on improving the efficiency of the union_all implementation, correcting a missing flag in the structural union check, and ensuring consistent use of the new utility in member inference.

Broad dynamic keys can match hundreds of table members. Collect matching
member types and fold the union once instead of repeatedly unioning a
growing result.

Keep the batch helper on the same semantic path as repeated `Union.apply`,
including callable canonicalization, and use the structural fast path only
when no pairwise union rule can change the result.

Fixes EmmyLuaLs#1075

Assisted-by: Codex
@lewis6991
lewis6991 force-pushed the fix/issue-1075-stuck-loading branch from 734af10 to e6f1167 Compare May 13, 2026 10:43
@CppCXY

CppCXY commented May 13, 2026

Copy link
Copy Markdown
Member

This is a script someone shared with me. I tested it with different versions and found that even after your fix, its loading performance is only one-third of what it was in version 0.21.0.(0.22.0 will stuck loading)
all scripts: scripts.zip

@CppCXY

CppCXY commented May 13, 2026

Copy link
Copy Markdown
Member

The script above has too much content and isn't suitable for testing. I tested the script from the original issue, and the performance ratio is still about the same — roughly a threefold difference.

@xuhuanzy

Copy link
Copy Markdown
Member

We may need to introduce a performance monitoring system, similar to Flame Graph, that can precisely observe the call duration of each function.

@lewis6991

lewis6991 commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

This PR should specifically fix the slowdown from the scripts in #1075 , it goes from hanging to 1.6s. I can look at https://github.com/user-attachments/files/27706685/scripts.zip (assuming it's different), but that may be slow for a different reason.

As we improve flow and type-checking to infer more types, slowdown is expected since there will be significantly less any/unknown which act as cut points in the typechecker.

@CppCXY
CppCXY merged commit 8c01eec into EmmyLuaLs:main May 13, 2026
16 checks passed
@lewis6991
lewis6991 deleted the fix/issue-1075-stuck-loading branch May 13, 2026 15:34
@lewis6991

Copy link
Copy Markdown
Collaborator Author

This is a script someone shared with me. I tested it with different versions and found that even after your fix, its loading performance is only one-third of what it was in version 0.21.0.(0.22.0 will stuck loading) all scripts: scripts.zip

#1079

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.

Stuck Loading

3 participants