Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Safe override of Class#new?

Reply
Thread Tools

Safe override of Class#new?

 
 
Trans
Guest
Posts: n/a
 
      03-05-2007

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.


 
Reply With Quote
 
 
 
 
Erik Veenstra
Guest
Posts: n/a
 
      03-05-2007
> 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)

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


 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      03-06-2007


On Mar 5, 3:39 pm, "Erik Veenstra" <erikv...@gmail.com> wrote:
> > 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)
>


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

T.


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      03-06-2007
Trans schrieb:
> Ouch. Sigh, any way I seem to slice it, AOP in pure Ruby sucks.


Welcome to the club

Regards,
Pit

 
Reply With Quote
 
Paolo Nusco Perrotta
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 1:38 am, "Trans" <transf...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      03-07-2007


On Mar 6, 6:10 pm, "Paolo Nusco Perrotta"
<paolo.nusco.perro...@gmail.com> wrote:
> On Mar 6, 1:38 am, "Trans" <transf...@gmail.com> wrote:
>
> > 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.


Thank you. I think that will indeed be enough.

T.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it safe to override #hash and #eql? to enable hash key equality? James Ruby 2 03-18-2009 06:27 PM
os.ChDir() not thread-safe; was : Is tempfile.mkdtemp() thread-safe? Gabriel Rossetti Python 0 08-29-2008 08:30 AM
Safe Mode (?) - It is meant to be normal mode but looks like safe mode English Patient Computer Support 3 10-03-2004 11:10 PM
Re: Those cute little "WORK-SAFE" / "NOT WORK-SAFE" tags that people put in the Subject headers of their posts... Soapy Digital Photography 1 08-16-2004 12:07 PM
Re: Those cute little "WORK-SAFE" / "NOT WORK-SAFE" tags that people put in the Subject headers of their posts... Soapy Digital Photography 1 08-16-2004 06:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57