Skip to content

Commit

Permalink
optimize stack monitoring
Browse files Browse the repository at this point in the history
Stepping over large functions is slow due to recent changes,
this optimizes some aspects with a 3-4x speed up.
  • Loading branch information
jbyuki committed Aug 11, 2024
1 parent 7ec237f commit 6837c6c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
37 changes: 33 additions & 4 deletions lua/osv/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ local log_filename

local lock_debug_loop = false

local skip_monitor = false
local skip_monitor_same_depth = false

local head_start_depth = -1

local exit_autocmd

local auto_nvim
Expand Down Expand Up @@ -278,6 +283,9 @@ function M.attach()
M.stop()
end

skip_monitor = false
skip_monitor_same_depth = false

running = true

limit = 0
Expand Down Expand Up @@ -523,13 +531,17 @@ function M.attach()
end
off = off + 1
end
head_start_depth = off - 1

depth = (off - 1) - surface
stack_level = depth

next = true
monitor_stack = true

skip_monitor = false
skip_monitor_same_depth = false

running = true

sendProxyDAP(make_response(request, {}))
Expand Down Expand Up @@ -783,6 +795,9 @@ function M.attach()
step_out = true
monitor_stack = true

skip_monitor = false
skip_monitor_same_depth = false

local depth = 0
local surface = 0
local off = 0
Expand All @@ -807,6 +822,7 @@ function M.attach()
end
off = off + 1
end
head_start_depth = off - 1

depth = (off - 1) - surface
stack_level = depth
Expand Down Expand Up @@ -931,8 +947,13 @@ function M.attach()
M.server_messages = {}


local depth = 0
if monitor_stack then
local depth = -1
if (event == "call" or event == "return") and monitor_stack and next then
skip_monitor_same_depth = true
skip_monitor = false
end

if monitor_stack and not skip_monitor then
local surface = 0
local off = 0
while true do
Expand All @@ -956,8 +977,13 @@ function M.attach()
end
off = off + 1
end
head_start_depth = off - 1

depth = (off - 1) - surface
if next and event == "line" and skip_monitor_same_depth then
skip_monitor = true
end

end

local bps = breakpoints[line]
Expand Down Expand Up @@ -1292,7 +1318,7 @@ function M.attach()

end

elseif event == "line" and next and depth <= stack_level then
elseif event == "line" and next and depth >= 0 and depth <= stack_level then
local msg = make_event("stopped")
msg.body = {
reason = "step",
Expand Down Expand Up @@ -1329,7 +1355,7 @@ function M.attach()
end


elseif event == "line" and step_out and stack_level-1 == depth then
elseif event == "line" and step_out and depth >= 0 and stack_level-1 == depth then
local msg = make_event("stopped")
msg.body = {
reason = "step",
Expand Down Expand Up @@ -1650,6 +1676,9 @@ function M.stop()
nvim_server = nil
end

skip_monitor = false
skip_monitor_same_depth = false

running = true

limit = 0
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/attach.lua.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ debug.sethook(function(event, line)
@handle_new_messages
@clear_messages

local depth = 0
if monitor_stack then
local depth = -1
@speedup_stack_monitor
if monitor_stack and not skip_monitor then
@get_stack_depth_with_debug_getinfo
@disable_monitor_for_speedup
end

@check_if_breakpoint_hit
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/next.lua.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ next = false
monitor_stack = false

@check_if_next+=
elseif event == "line" and next and depth <= stack_level then
elseif event == "line" and next and depth >= 0 and depth <= stack_level then
@send_stopped_event_step
@disable_next

Expand All @@ -48,5 +48,5 @@ while true do
end
off = off + 1
end

@save_for_next_head_start
depth = (off - 1) - surface
2 changes: 1 addition & 1 deletion src/handlers/step_out.lua.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ step_out = false
monitor_stack = false

@check_if_step_out+=
elseif event == "line" and step_out and stack_level-1 == depth then
elseif event == "line" and step_out and depth >= 0 and stack_level-1 == depth then
@send_stopped_event_step
@disable_step_out

Expand Down
45 changes: 45 additions & 0 deletions src/optimizations/optimize_stack_monitor.lua.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
##../lua-debug
@script_variables+=
local skip_monitor = false
local skip_monitor_same_depth = false

@reset_internal_states+=
skip_monitor = false
skip_monitor_same_depth = false

@set_next_variable+=
skip_monitor = false
skip_monitor_same_depth = false

@set_step_out+=
skip_monitor = false
skip_monitor_same_depth = false

@speedup_stack_monitor+=
if (event == "call" or event == "return") and monitor_stack and next then
skip_monitor_same_depth = true
skip_monitor = false
end

@disable_monitor_for_speedup+=
if next and event == "line" and skip_monitor_same_depth then
skip_monitor = true
end

@script_variables+=
local head_start_depth = -1

@save_for_next_head_start+=
head_start_depth = off - 1

@head_start_for_monitoring+=
if head_start_depth >= 0 then
local info = debug.getinfo(head_start_depth, "S")
if info then
local inside_osv = false
@check_if_inside_osv
if inside_osv then
off = head_start_depth+1
end
end
end

0 comments on commit 6837c6c

Please sign in to comment.