RuPol

@@@@@@@@@@@@@@@@@'''','@@;,:+'''''',       :;,,,,''''',,,,    `:',,,,               ,, ,, ,        ,'',    .,,,,               `,,,,  
@@@@@@@@@@@@@@@@@@@@@@@+',,.  :''@''':   .`,.@@@;    , ,,,,,,,,:,,,,,,,,,,  `,,,,,                 ,',     ;';,,            ,,     ,,,
@@@@@@@@@@@@@@@@@@@@@@+':,,,      ''''',,,:;,,      ,',,,,,, ,.,                                   ,',,     .''''.        .,,,.      ,
@@@@@@@@@@@@@@@@@@@@@'''''+@#'''''''''''''';,,    ,,,,,,,,,,`,,,,                                  ,',       ''''''',    ,,;:,'.   ,,,
@@@@@@@@@@@@@@@@@'@',':;@@@#''''@@@@''''''';,. ,',,,,,`.,,,,,,,,,,,                         `,,,.  ,,, ,    '''@@'''@@` ,,,,,'';,,,,,,
@@@@@@@@@@@#;    '''''':      `''@@@@@@'''''''''''',,,''''''''',,,,,,,                 ,,,,,,,,,,,,,',,,   .'''+@#''',,,,,,;'''''';,''
@@@@@@@@@#',.    ''''':,      `,'@@@@@+'@@#'''''''''''''''''';,,,,,,,,,,,,,.            .,,,,,,,,,,,'',,`          ,,,,,`.;'';:''';,,'
@@@@@@@@''';,    ''''''',     :''@@@@+''''''''''''''''''''';,,,,,,,,,,,,,,,,,,           .,,,,,',,,,,'':,,,              ;'''';.   '' 
@@@@@@@@+,,,,,''@''''',,     ,,''@@@'''+'''''''''''''''':,,,,,,,,,,,,,,,,,,, `:;       ,   .,,,,,,,,,,,,,` ,,,,';.    ,'''''''   :''@'
@@@@@@@':`,,'+@@@'''':      `:''@@@'@@@''@@+@''''''''''',,,,,,,,,,,            ,`,'     .,,,,,,,,,,,,,'':   .,,.     ,'''''''  `'',@@@
@@@@@@@+',   ,'@'''',`    `'@@@@@@@'''#@@@@@@''',,,''''',,,,,,,,,,,         `.`,,,  ''`       `,,,,,'''':, .,       '''''''   `'';.:@@
@@@@@@@@@#. ,`'@@',,    .'''''''  ,''@@@@@@'''''',,,    ;':,,,.          ,    `,,,,,,,,#@+       ,, ,'''''`         ,'''':  ;'';,,, ''
@@@@@@@@@@@@@@@@@',   ;'''';   ,,'''@@@@@@@@@@#''''''':,,`,'',          .,,,,,,,,,,,,,,,;#@+         ,''.       ''''' '',:,`          
@@@@@@@@@@@@@@@@'',  ,,,''.  ,''''@@@@+''''@@@@@@@@@@+''@+':';,,,,     .,,, ,,,,,,,,,,,,,''@@,,      ,':,,, ,'''';, '''',,`           
@@@@@@@@@@@@@@@@'',   .;'. ,:'@@@@@@@@''''''+@@@''@@@+@@@.,@#'',,,,,,,,,;'';,.       ,,''''#@;, ,     ':,,,,          ,,,,`      ,,,,,
@@@@@@@@@@@@@@@@@@''';'',   :''@@@@@@'''''''''+@@@@@':,;'#+`''';,,,,'''''''''',,,, ,,  .,,'#',,,,,,, '''',,,          , ,:`     ,,,,,'
@@@@@@@@@@@@@@@@@@@@@@@:;  `:''@@@@@@''''''';,:',,,,,,,,''''''',  ''',,'#@@@@@@@@@@''''':;'',,,,,,,,,@@@+'':`        ,,,,:,,`     ,'''
@@@@@@@@@@@@@@@@@@@@@@@':', `,+@@@@@@+''''''',,,,,,,,,,,''''';,,,,''''''@':@@@@@'@@@@@@''':,,,,',,,'@''#@@@@+''',,,,,,,'''':`   `:''@'
@@@@@@@@@@@@@@@@@@@@@+#@@@;,'@'  ,,@@@''''''',,,,,,,,,,'''''',,,,'''',,,,,''+@@@@@@'''''   .,,,,,,'''''''''''@#;,,,,'''''''',:'';, `''
@@@@@@@@@@@@@@@@@@@@@@@@@@@#@@#''','@@@'''''',,,,,,,,'''''';, ,:'''',,,,,,,,,,,,,,,,,,:         `+'',,'''''''+@@#'''''@@@@@@@@@@@''''@
@@@@@@@@@@@@@@@@@@@@@@@@+@@@@@@''',,'@@'''''',,,,,,@''''''',,,,'''',,,,,,,,,,,,,,,,,,,        `'@@@@''''#@+'''@@'+@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@'@@@'''  ;@'''''';,,,,','''''@+''@@@@'',,,,,,,,,,,''''''''       `''@@''''@'',   `': ,''@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@''''#@@@@@#+@@@@@'', :''''';,''''''':,,,,,,,,:'',,,,,,,;'''''''''''@,`,     :''''''''':`  ',   ,'@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@''',,'''''''@@@@@@':,,,,,,,,,,,,,,''''''''''''''@@',`.,`  `,,'''''',;':,     '@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@''@@+':,,:'+@@@@@@@@@@@@@@@@#,,,,'''''''''''''''''@@@@+:.`,,     ,''@@+'':,    :@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'''@@'',,,:@@@@''''@@@@@@@@@@@#''''''''''''''''''@@@@@@+',  `,.   '@@''''`  ,@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@''@@+''',,:@@@@@'''''''''''@@#''''''''''''''''@@@@@@@@@@@@';,,.,,'@@'''''##@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@#+@@#,,''''@@''',,,:+@@@@@@@@@@@@@@#'''''''''''''''@@@@@@@''@@@@@@@@#;,,,'''''':   ,@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@# @@'''#@+';,,,,'''''''''''''''''''''''''#@@@@@@@'',,,,,`  ,@@@'';,,,@@@+.`   ,:+@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@''@'''''@+'';,,,,'''''';'':''''''''@@@@@@@@@@@@''''',,      :''',, @@@@@@+ .''''''@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@'''''@@@@@@@@@@@''''''''''''';,,,,,'''''''''+@@@@@@@@@@@@@@@@@@'''''',,,,,,,';  :@@@@@@@@@@@@@@@'@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@#'@@@@@@''''@@@@@@@'@'''''''''''''',,,,,#@@@@@@@@@@''''@@@@@@@@@@;,',,, ,, ,'':   +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@'@'''''''''''''',,,,;#@@@@@#''''''@@@@@@@@@@@@@# ,       '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@#+@@  ;@@@@@@@@@@@#''@''''''''''''''',,,,,'''''''''''''@@@@@@@@@@@@@@@@' @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@'''''''''''''@@'@@@@@@@@@'''''''''''''''''',,,,;'''''''''''''@@@@+'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@+''''''''''''''''@@`@@@@@@@@@@@''''''''''''''',,,,,,''''''''''@','@@'@@@''''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@''''''''''''''''''''''@@@@@@@@@@@''''''''''''''',,,,,,'`   . ' `; ',@+#@@@#'+''''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'''''''''''''''''''''''@@#+@@@@@@@@''''''''''''',,,,,,,'`      `  ,''`;'#@@@@@@'''''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''#@@@@@@@@@@@''''''''''',,,,,,,,,;@  :@. ','@'' @..@@@@@@@'''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'''''''''''''''''''''''''+@@@@@@@@@@''''''''''''',,,,,,,: +  ;        @. ,''''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''+@@@@@@@@@''''''''''',,,,;''#+`:;  .   ' @,#@`.''''''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'''''''''''''''''''''''''''''+@@@@@@@@+''''''''''''''''''':'';   , ,@  ;   :''''''''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

RuPol is a glamorous mixin for instance pooling your Ruby classes.

It eases the pain of garbarge collection for classes that are instantiated many times, and then tossed away like runway trash. Instances are cached on the class in a pool (array, in less glamorous terms), and can be recycled at will. Of course, there is no pain without gain, and models will trade collection costs for memory usages. The Swimsuit mixin edition overrides new and destroy, for a virtually pain free instance swimming experience.

Runway not included.

Usage

The RuPol module can just be included into your class:

class MyModel 
  include RuPol

  def self.process
    instance = rehydrate || new
    # .... do something with the instance
    instance.recycle
  end
end

Instance convenience methods added to the class will be:

Class methods added for convenience include:

Including the RuPol module itself means that you do all the heavy lifting of using the pool. If you want coding life a little simpler, then use the RuPol::Swimsuit edition module instead. It overrides new and destroy, so that you can just use the pool in the normal lifecycle of an object.

class MyLazyModel
  include RuPol::Swimsuit
end

instance = MyLazyModel.new    # this could be a recycled or a new instance depending on what's in your pool
instance.destroy   # goes back into the pool, and if your superclass defines #destroy, that gets called first!

The Swimsuit edition provides a clear method too so that your large objects get cleaned out before storage. Plus all the benefits of more rigorous pool usage.

Contributing to ru_pol

Copyright © 2011 Kane Baccigalupi. See LICENSE.txt for further details.