Skip to content

Commit

Permalink
PERF: Avoid allocating array in `MiniScheduler::Manager.discover_queu…
Browse files Browse the repository at this point in the history
…es` (#55)

`map(&:queue).to_set` is inefficient because it creates an array before
converting the array to a set. Instead, we can just iterate and add the
values to the set to avoid allocating the array completely. This is
beneficial when there are many `MiniScheduler::Schedule` objects.
  • Loading branch information
tgxworld authored Aug 6, 2024
1 parent d92f17b commit 5aecec3
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/mini_scheduler/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ def initialize(manager)
end
end
end

@keep_alive_thread =
Thread.new do
while !@stopped
@mutex.synchronize { keep_alive }
sleep(@manager.keep_alive_duration / 2)
end
end

ensure_worker_threads
end

Expand Down Expand Up @@ -69,6 +71,7 @@ def ensure_worker_threads
def worker_loop
set_current_worker_thread_id!
keep_alive(current_worker_thread_id)

while !@stopped
begin
process_queue
Expand All @@ -77,6 +80,7 @@ def worker_loop
ex,
message: "Error during MiniScheduler worker_loop",
)

break # Data could be in a bad state - stop the thread
end
end
Expand Down Expand Up @@ -142,6 +146,7 @@ def process_queue
error = "#{e.class}: #{e.message} #{e.backtrace.join("\n")}"
failed = true
end

duration = ((Time.now.to_f - start) * 1000).to_i
info.prev_duration = duration
info.prev_result = failed ? "FAILED" : "OK"
Expand All @@ -158,6 +163,7 @@ def process_queue
attempts(3) { @mutex.synchronize { info.write! } }
ensure
@running = false

if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection_handler.clear_active_connections!
end
Expand Down Expand Up @@ -366,7 +372,9 @@ def lock
end

def self.discover_queues
ObjectSpace.each_object(MiniScheduler::Schedule).map(&:queue).to_set
queues = Set.new
ObjectSpace.each_object(MiniScheduler::Schedule).each { |schedule| queues << schedule.queue }
queues
end

def self.discover_schedules
Expand Down

0 comments on commit 5aecec3

Please sign in to comment.