Comparing Alias Methods

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,271
Reaction score
2,329
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:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,695
Reaction score
924
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.
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
536
Reaction score
249
First Language
German
Primarily Uses
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:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,271
Reaction score
2,329
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. :'(
 

FenixFyreX

Fire Deity
Veteran
Joined
Mar 1, 2012
Messages
434
Reaction score
308
First Language
English
Primarily Uses
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:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

meet with Quon,
the goddess of Rakuen (circle mascot)
It turns out that if I set the collider to 0,0,0,0 before despawning an event.... QMovement will actually play nice with Galv Event Spawner! :LZSexcite:
So I cloned an event in MV today.
Just got two custom tracks for my game. Things are shaping up nicely. :)
Remember, life is sh*t. But it will difinitely be less sh*t in the future! Stay happy everyone ^^ ...*continues to die inside*

Forum statistics

Threads
93,503
Messages
913,065
Members
123,037
Latest member
__Andrea__
Top