You are repeatedly rotating the entire Array
in place-place and then calling #first
, this is \$\Theta(N^2)\$ behavior for a single traversal through the proxy list.
Instead you could create a cyclical Enumerator
(think Generators in Python) to hold your shuffled proxies and advance it whenever a proxy is over-used.
This \$\Theta(N)\$ access pattern should reduce memory usage somewhat:
@proxies = Enumerator.new do |fresh_proxies|
# Using #pluck avoids building intermediate objects before projection
Proxy.pluck(:ip).shuffle.cycle.each do |proxy|
fresh_proxies.yield proxy
end
end
Now you can replace accessing #first
with:
def current_proxy
@proxies.peek
end
and your #rotate!
with:
def advance_to_next_proxy!
@proxies.next
end
Docs: