lib/picky/pool.rb in picky-4.6.0 vs lib/picky/pool.rb in picky-4.6.1
- old
+ new
@@ -4,27 +4,34 @@
# for you.
#
module Pool
class << self
- require 'set'
- @@pools = Set.new
+ def clear
+ require 'set'
+ @pools = Set.new
+ end
+
# Add a Pool to the managed pools.
#
def add klass
- @@pools << klass
+ @pools << klass
end
# Releases all obtained objects.
#
def release_all
- @@pools.each { |pool| pool.release_all }
+ @pools.each { |pool| pool.release_all }
end
end
+ # Reset the pool.
+ #
+ self.clear
+
def self.extended klass
add klass
class << klass
#
@@ -35,12 +42,10 @@
end
# Obtain creates a new reference if there is no free one
# and uses an existing one if there is.
#
- # (Any caches should be cleared using clear TODO in all initializers)
- #
def obtain *args, &block
unless reference = @__free__.shift
reference = allocate
end
reference.send :initialize, *args, &block
@@ -51,18 +56,21 @@
# Releasing an instance adds it to the free pool.
# (And removes it from the used pool)
#
def release instance
@__free__ << instance
- @__used__.delete instance # TODO Optimize
+
+ # Note: This is relatively fast as there are often only
+ # few instances in the used pool.
+ @__used__.delete instance
end
# After you have called release all, you can't
# use any reference that has formerly been obtained
# anymore.
#
def release_all
- @__used__.each { |used| @__free__ << used } # TODO Optimize
+ @__free__ += @__used__
@__used__.clear
end
# How many obtainable objects are there?
#
\ No newline at end of file