Comparing Alias Methods

Discussion in 'Learning Ruby and RGSSx' started by ??????, Jan 18, 2015.

    Tags:
  1. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,266
    Likes Received:
    2,316
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    So, recently I had a mess around with alternative alias methods...

    Generally, most people just do something like this...

    alias :method_name_new :method_namedef method_name  method_name_newendWhilst this is fine for most cases, after much underground digging  I found that this is not an ideal solution. This is due to creating a 'memory leak' by having multiple 'almost identical' methods defined.

    After perusing many websites I came across some helpful information for an alternative way to perform an alias...

    method_alias = instance_method:)method_name)method__proc = Proc.new{ method_alias.bind(self).call }send:)define_method, :method_name, method__proc)At first, I thought, hey this is pretty cool. I can alias methods without leaving any 'spare methods' laying around. However, after performing some basic benchmarking tests (due to something FenixFyeX mentioned about performance)  discovered that using such methods to perform an alias (example 2) is significantly slower.

    Here is my test code..

    # Alias Speed Test..module TesT  class << self    def method_a    end     alias :method_a_alias :method_a    def method_a      method_a_alias    end     def method_b    end        method_b_alias = instance_method:)method_    method_b_proc = Proc.new{ method_b_alias.bind(self).call }    send:)define_method, :method_b, method_b_proc)  endendBenchmark.ips do |x|  x.report('one') { TesT.method_a }  x.report('two') { TesT.method_b }  x.compare!end# Results ::# Calculating -------------------------------------#                 one    53.845k i/100ms#                 two    40.283k i/100ms#-------------------------------------------------#                 one      2.588M (± 2.7%) i/s -     12.977M#                 two      1.000M (±10.8%) i/s -      4.794M# Comparison:#                 one:  2588272.8 i/s#                 two:  1000330.8 i/s - 2.59x slowerAs you can see, using the second alias technique is so much slower when being called, which makes me wonder, which method I should use?

    Personally, I strive for efficiency, which leads me to want to use method_a, but the fact that it is leaving the aforementioned 'spare methods' hanging around, is it worth it?

    Also, does anyone have an even better (and faster) alternative?
     
    Last edited by a moderator: Jan 18, 2015
    #1
  2. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    924
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    I wouldn't really call it a memory leak. Memory leaks are when unreferenced data still exits in memory, but the Ruby garbage collector generally doesn't allow that. In the case of an alias, you make a copy of the method, then add some stuff to it, but you still end up calling the orinlginal, otherwise you would want to overwrite it. Bad programming like that is a fault in the programmer.

    Even if it were considered a memory leak, it wouldn't even be a major problem. It would "leak" once each time an alias is made, but not at any other time. Therefore I don't think its really that big of a deal, and that method 1 is faster and easier to use.
     
    #2
    Tsukihime likes this.
  3. Another Fen

    Another Fen Veteran Veteran

    Messages:
    536
    Likes Received:
    248
    First Language:
    German
    Mmm, just out of curiosity, since I don't really know how method management is achieved in ruby:
    Does adding another method name to the method table use up significantly more memory than storing the unbound method and proc?
     
    Last edited by a moderator: Jan 19, 2015
    #3
  4. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,266
    Likes Received:
    2,316
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    Not fully sure on that myself, but it would make sense.

    I mean, if I store some data in a variable I would assume that takes less memory compared to a defined method. (which is essentially a variable name that retains additional functionality) - using that logic, seems safe to say a variable takes less space.

    Other than that, I'm not sure.

    Personally I had always deemed 'alias :thistothat :that' sufficient, it was only when I was browsing some webpages that people begin to mention the memory leaks that using alias creates. No where did I find any efficiency tests between alias methods or anything that provides additional data on the leaks. :'(
     
    #4
  5. FenixFyreX

    FenixFyreX Fire Deity Veteran

    Messages:
    434
    Likes Received:
    307
    Location:
    A Volcano Somewhere
    First Language:
    English
    They use approximately the same memory. Actually, a single method would probably be less memory than both Ruby objects (UnboundMethod and Proc).


    Also, there isn't a leak. I would like to see the source from which this information came from; I've delved into the Ruby source a lot, and from what I can tell, alias doesn't introduce any leaks at all.


    As well, a simple test with benchmark/ips reveals that the normal way of aliasing (the alias keyword) is >= 4x faster than using define_method with a proc. The normal alias keyword introduces speeds that are almost at pace with normal method definition.


    EDIT: To clarify, this 4x speed difference is both when aliasing the method, -and- execution of the method.
     
    Last edited by a moderator: Jan 19, 2015
    #5
    Another Fen, ?????? and Zeriab like this.

Share This Page