Skip to content

Commit ec0f3e1

Browse files
mbaumanKristofferC
authored andcommitted
Re-enable tab completion of kwargs for large method tables (#58012)
while testing to ensure that ~~absurdly large method tables~~ tab completing over an abstract function call doesn't tank the performance of the REPL Fixes #57836 (cherry picked from commit 6f12957)
1 parent d764f2d commit ec0f3e1

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,13 @@ function complete_keyword_argument(partial, last_idx, context_module)
949949
kwargs_flag == 2 && return fail # one of the previous kwargs is invalid
950950

951951
methods = Completion[]
952-
complete_methods!(methods, funct, Any[Vararg{Any}], kwargs_ex, MAX_METHOD_COMPLETIONS, kwargs_flag == 1)
952+
# Limit kwarg completions to cases when function is concretely known; looking up
953+
# matching methods for abstract functions — particularly `Any` or `Function` — can
954+
# take many seconds to run over the thousands of possible methods. Note that
955+
# isabstracttype would return naively return true for common constructor calls
956+
# like Array, but the REPL's introspection here may know their Type{T}.
957+
isconcretetype(funct) || return false
958+
complete_methods!(methods, funct, Any[Vararg{Any}], kwargs_ex, -1, kwargs_flag == 1)
953959
# TODO: use args_ex instead of Any[Vararg{Any}] and only provide kwarg completion for
954960
# method calls compatible with the current arguments.
955961

stdlib/REPL/test/replcompletions.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,3 +2004,32 @@ let s = "using ...Issue52922.Inn"
20042004
@test res
20052005
@test "Inner2" in c
20062006
end
2007+
2008+
function g54131 end
2009+
for i in 1:498
2010+
@eval g54131(::Val{$i}) = i
2011+
end
2012+
g54131(::Val{499}; kwarg=true) = 499*kwarg
2013+
struct F54131; end
2014+
Base.getproperty(::F54131, ::Symbol) = Any[cos, sin, g54131][rand(1:3)]
2015+
f54131 = F54131()
2016+
@testset "performance of kwarg completion with large method tables" begin
2017+
# The goal here is to simply ensure we aren't hitting catestrophically bad
2018+
# behaviors when shift isn't pressed. The difference between good and bad
2019+
# is on the order of tens of milliseconds vs tens of seconds; using 1 sec as
2020+
# a very rough canary that is hopefully robust even in the noisy CI coalmines
2021+
s = "g54131(kwa"
2022+
a, b, c = completions(s, lastindex(s), @__MODULE__, #= shift =# false)
2023+
@test REPLCompletions.KeywordArgumentCompletion("kwarg") in a
2024+
@test (@elapsed completions(s, lastindex(s), @__MODULE__, false)) < 1
2025+
2026+
s = "f54131.x("
2027+
a, b, c = completions(s, lastindex(s), @__MODULE__, false)
2028+
@test only(a) isa REPLCompletions.TextCompletion
2029+
@test (@elapsed completions(s, lastindex(s), @__MODULE__, false)) < 1
2030+
2031+
s = "f54131.x(kwa"
2032+
a, b, c = completions(s, lastindex(s), @__MODULE__, false)
2033+
@test_broken REPLCompletions.KeywordArgumentCompletion("kwarg") in a
2034+
@test (@elapsed completions(s, lastindex(s), @__MODULE__, false)) < 1
2035+
end

0 commit comments

Comments
 (0)