"val.dup rescue val" sloooow

M

Michael Neumann

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
 
R

Robert Klemme

Michael Neumann said:
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-talk/32556

Kind regards

robert
 
M

Michael Neumann

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
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top