Skip to content

Instantly share code, notes, and snippets.

@wisetara
Last active February 27, 2018 17:40
Show Gist options
  • Save wisetara/7b0010642e7d057f2e85b24fbc664347 to your computer and use it in GitHub Desktop.
Save wisetara/7b0010642e7d057f2e85b24fbc664347 to your computer and use it in GitHub Desktop.
Let's speed up RSpec, yo. This Singleton version totally based on @joevandyk and @triskweline's work
class DeferredGarbageCollection
include Singleton
# One can set a different time period for the garbage collection, but 15 seemed good.
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 15.0).to_f
def initialize
@last_gc_run = Time.zone.now
end
def self.start
instance.start
end
def start
GC.disable if DEFERRED_GC_THRESHOLD > 0
end
def self.reconsider
instance.reconsider
end
# Checks if last run time exceeds the value of the time threshold and enables Garbage
# Collection for a short cleanup. After that Garbage Collection is deferred again.
def reconsider
return unless DEFERRED_GC_THRESHOLD > 0 && Time.zone.now - @last_gc_run >= DEFERRED_GC_THRESHOLD
GC.enable
GC.start
GC.disable
@last_gc_run = Time.zone.now
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment