[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"val.dup rescue val" sloooow

Michael Neumann

10/27/2004 10:23:00 AM

Hi,

I just wanted to tell you that if you do

val.dup rescue val

somewhere in your program, you should probably switch to:

case val
when Fixnum, nil, true, false
# non dup-able
val
else
val.dup rescue val
end

which is much faster if you have lots of non-dupable values.

Regards,

Michael


2 Answers

Robert Klemme

10/27/2004 11:29:00 AM

0


"Michael Neumann" <mneumann@ntecs.de> schrieb im Newsbeitrag
news:20041027102249.GA4281@miya.intranet.ntecs.de...
> Hi,
>
> I just wanted to tell you that if you do
>
> val.dup rescue val
>
> somewhere in your program, you should probably switch to:
>
> case val
> when Fixnum, nil, true, false
> # non dup-able
> val
> else
> val.dup rescue val
> end
>
> which is much faster if you have lots of non-dupable values.

While that works and is efficient, I'd prefer any of these two solutions:

- Remove #dup from types that can't be duped, so we can check with
respond_to?

- Have #dup return self in these cases (immutable instances anyway).

I'm not sure though which negative implications the second approach could
have. From a usage point of view I'd certainly prefer it. I remember
this came up before and I think Matz opted for the first solution:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...

Kind regards

robert

Michael Neumann

10/27/2004 3:41:00 PM

0

On Wed, Oct 27, 2004 at 08:34:06PM +0900, Robert Klemme wrote:
>
> "Michael Neumann" <mneumann@ntecs.de> schrieb im Newsbeitrag
> news:20041027102249.GA4281@miya.intranet.ntecs.de...
> > Hi,
> >
> > I just wanted to tell you that if you do
> >
> > val.dup rescue val
> >
> > somewhere in your program, you should probably switch to:
> >
> > case val
> > when Fixnum, nil, true, false
> > # non dup-able
> > val
> > else
> > val.dup rescue val
> > end
> >
> > which is much faster if you have lots of non-dupable values.
>
> While that works and is efficient, I'd prefer any of these two solutions:
>
> - Remove #dup from types that can't be duped, so we can check with
> respond_to?
>
> - Have #dup return self in these cases (immutable instances anyway).

Yes that seems reasonable, as e.g. #freeze does not raise an error on
these kind of objects, too.

Regards,

Michael