Safe override of Class#new?

T

Trans

class Class
alias :create :new

def new(*a,&b)
obj = allocate
obj.extend advice
obj.send:)initialize,*a,&b)
return obj
end
end

Note #advice is a sprcisl module that provides AOP features.

Like to get the opinions of other Ruby experts on this. What kind of
potential trouble am I asking for by using this?

Thanks,
T.
 
E

Erik Veenstra

Like to get the opinions of other Ruby experts on this. What
kind of potential trouble am I asking for by using this?

(Not that I'm a Ruby expert, but anyway...)

You'll run into a real slowdown of your application. AFAIK,
creating objects is one of the most expensive operations in any
OO language. Even when it's implemented in C, as is done with
Ruby.

Yeah, sure, go ahead, implement it in Ruby and slow it down by
a factor of three!... :}

(Just kidding...)

gegroet,
Erik V. - http://www.erikveen.dds.nl/

----------------------------------------------------------------

require "benchmark"

class Class
alias :create :new

def new_new(*a, &b)
obj = allocate
obj.send:)initialize, *a, &b)
obj
end
end

times = 1_000_000

Benchmark.bmbm do |bm|
bm.report("old_new") do
times.times do
Object.new
end
end

bm.report("new_new") do
times.times do
Object.new_new
end
end
end

----------------------------------------------------------------

<snip/>

user system total real
old_new 0.680000 0.010000 0.690000 ( 0.687622)
new_new 1.880000 0.010000 1.890000 ( 1.889214)

----------------------------------------------------------------
 
T

Trans

(Not that I'm a Ruby expert, but anyway...)

You'll run into a real slowdown of your application. AFAIK,
creating objects is one of the most expensive operations in any
OO language. Even when it's implemented in C, as is done with
Ruby.

Yeah, sure, go ahead, implement it in Ruby and slow it down by
a factor of three!... :}

(Just kidding...)

gegroet,
Erik V. -http://www.erikveen.dds.nl/

----------------------------------------------------------------

require "benchmark"

class Class
alias :create :new

def new_new(*a, &b)
obj = allocate
obj.send:)initialize, *a, &b)
obj
end
end

times = 1_000_000

Benchmark.bmbm do |bm|
bm.report("old_new") do
times.times do
Object.new
end
end

bm.report("new_new") do
times.times do
Object.new_new
end
end
end

----------------------------------------------------------------

<snip/>

user system total real
old_new 0.680000 0.010000 0.690000 ( 0.687622)
new_new 1.880000 0.010000 1.890000 ( 1.889214)

Ouch. Sigh, any way I seem to slice it, AOP in pure Ruby sucks.

T.
 
P

Paolo Nusco Perrotta

Ouch. Sigh, any way I seem to slice it, AOP in pure Rubysucks.

OTOH, do you really want to AOP-ify all Ruby classes? Maybe you can
apply the advice to a limited subset of classes. The performance hit
might be acceptable in the end.
 
T

Trans

OTOH, do you really want to AOP-ify all Ruby classes? Maybe you can
apply the advice to a limited subset of classes. The performance hit
might be acceptable in the end.

Thank you. I think that will indeed be enough.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top