Certainly! I can help you optimize your function. The primary area for optimization in your code is the while loop, which incrementally steps through speed values to find the next appropriate speed. This can be inefficient, especially if the speed steps are small or the difference is large.
Assuming that self:speed_step(speed, direction) returns the next speed step in the given direction, we can eliminate the loop by calculating the number of steps directly. Here's the optimized version of your function:
function Speed:on_global_mouse_move()
if not self.dragging then return end
local drag = self.dragging
drag.distance = cursor.x - drag.start_x
drag.speed_distance = (-drag.distance / self.notch_spacing) * self.notch_every
local speed_drag_current = clamp(0.01, drag.start_speed + drag.speed_distance, 100)
local speed_current = state.speed
local drag_dir_up = speed_drag_current > speed_current
-- Calculate the difference in speed
local speed_diff = speed_drag_current - speed_current
-- Determine the number of steps needed
local step_size = self:get_step_size(speed_current)
local steps_needed = math.floor(math.abs(speed_diff) / step_size + 0.5)
-- Calculate the new speed directly
local speed_new = speed_current + (drag_dir_up and 1 or -1) * step_size * steps_needed
speed_new = clamp(0.01, speed_new, 100)
-- Update the speed if it has changed
if speed_new ~= speed_current then
mp.set_property_native('speed', speed_new)
end
end
Explanation:
-
Eliminated the While Loop: By calculating the number of steps required to reach the desired speed, we avoid the need to incrementally step through each possible speed value.
-
Calculating Step Size: The function self:get_step_size(speed_current) should return the step size based on the current speed. If your speed steps are uniform, you can set a constant step size. If they vary, you'll need to implement this function accordingly.
-
Calculating Steps Needed: We compute how many steps are needed to reach the desired speed by dividing the speed difference by the step size and rounding appropriately.
-
Calculating New Speed: We directly calculate the new speed by adding or subtracting the total step change from the current speed.
-
Clamping the New Speed: Ensure that the new speed stays within the allowable range using the clamp function.
Additional Optimizations:
-
Avoid Unnecessary Calculations: Only perform calculations when necessary. For example, if speed_drag_current equals speed_current, you can return early.
-
Optimize clamping: If clamp is used frequently and performance-critical, consider inlining its logic to reduce function call overhead.
Note: You need to implement the self:get_step_size(speed) function based on how your speed steps are defined. If self:speed_step(speed, direction) increases or decreases the speed by a constant or calculated step, you can derive the step size from there.
Example Implementation of get_step_size:
function Speed:get_step_size(speed)
-- Example: speed steps increase logarithmically
return 0.1 * speed
end
Adjust the get_step_size function according to your application's logic.
By calculating the steps directly, this optimization reduces the computational overhead and improves the responsiveness of your application during mouse drag events.