Repository navigation
fix(type): canonicalize identical callable unions - #1022
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves the union type merging logic by introducing a more comprehensive equality check that accounts for callable types, such as function signatures and doc functions. It also includes a new test case to verify that pcall correctly preserves return types within loops. The review feedback highlights several performance optimization opportunities, including pre-resolving callable types during union construction to minimize redundant lookups and adding a fast-path comparison for identical unions to avoid unnecessary O(N^2) operations.
Deduplicate semantically identical callable members when building unions. Fixes EmmyLuaLs#1020
8200729 to
8d0a469
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces logic to canonicalize callable types within unions, ensuring that equivalent Signature and DocFunction variants are collapsed into a single representation. It also adds test cases to verify pcall return type preservation and the new union collapsing behavior. Feedback was provided regarding the performance of the canonicalize_callable_union function, specifically suggesting that pre-calculating callable shapes for union members would avoid redundant database lookups and allocations during the merging process.
| fn canonicalize_callable_union(db: &DbIndex, ty: LuaType) -> LuaType { | ||
| let LuaType::Union(union) = ty else { | ||
| return ty; | ||
| }; | ||
|
|
||
| let mut types = Vec::new(); | ||
| for member in union.into_vec() { | ||
| let member_callable = as_callable_type(db, &member); | ||
| if types.iter().any(|existing| { | ||
| existing == &member | ||
| || as_callable_type(db, existing) | ||
| .as_ref() | ||
| .zip(member_callable.as_ref()) | ||
| .is_some_and(|(existing, member)| existing == member) | ||
| }) { | ||
| continue; | ||
| } | ||
| types.push(member); | ||
| } | ||
|
|
||
| LuaType::from_vec(types) | ||
| } |
There was a problem hiding this comment.
The canonicalize_callable_union function performs redundant and potentially expensive operations inside its inner loop. Specifically, as_callable_type is called repeatedly for the same existing members as the outer loop progresses. For Signature types, as_callable_type involves database lookups and allocations (via to_doc_func_type).
Pre-calculating the callable shapes for each member and storing them alongside the types would significantly improve performance, especially for larger unions or frequent type operations.
fn canonicalize_callable_union(db: &DbIndex, ty: LuaType) -> LuaType {
let LuaType::Union(union) = ty else {
return ty;
};
let members = union.into_vec();
let mut result_types = Vec::with_capacity(members.len());
let mut callables = Vec::with_capacity(members.len());
for member in members {
let member_callable = as_callable_type(db, &member);
let is_duplicate = result_types.iter().zip(callables.iter()).any(|(existing, e_call)| {
existing == &member
|| (member_callable.is_some() && member_callable == *e_call)
});
if !is_duplicate {
result_types.push(member);
callables.push(member_callable);
}
}
LuaType::from_vec(result_types)
}
Summary
Testing
Fixes #1020