Safe override of Class#new?

Discussion in 'Ruby' started by Trans, Mar 5, 2007.

  1. Trans

    Trans Guest

    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.
     
    Trans, Mar 5, 2007
    #1
    1. Advertising

  2. > 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)

    ----------------------------------------------------------------
     
    Erik Veenstra, Mar 5, 2007
    #2
    1. Advertising

  3. Trans

    Trans Guest

    On Mar 5, 3:39 pm, "Erik Veenstra" <> 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.
     
    Trans, Mar 6, 2007
    #3
  4. Trans

    Pit Capitain Guest

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


    Welcome to the club :-(

    Regards,
    Pit
     
    Pit Capitain, Mar 6, 2007
    #4
  5. On Mar 6, 1:38 am, "Trans" <> 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.
     
    Paolo Nusco Perrotta, Mar 6, 2007
    #5
  6. Trans

    Trans Guest

    On Mar 6, 6:10 pm, "Paolo Nusco Perrotta"
    <> wrote:
    > On Mar 6, 1:38 am, "Trans" <> 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.
     
    Trans, Mar 7, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Rossetti
    Replies:
    0
    Views:
    1,331
    Gabriel Rossetti
    Aug 29, 2008
  2. Replies:
    1
    Views:
    343
    Brian Candler
    Aug 12, 2003
  3. Aredridel

    Not just $SAFE, but damn $SAFE

    Aredridel, Sep 2, 2004, in forum: Ruby
    Replies:
    19
    Views:
    244
  4. Farrel Lifson

    $SAFE =4 safe enough?

    Farrel Lifson, Aug 29, 2006, in forum: Ruby
    Replies:
    7
    Views:
    105
    Eric Hodel
    Aug 31, 2006
  5. James
    Replies:
    2
    Views:
    134
    James
    Mar 18, 2009
Loading...

Share This Page