From: Gene Tani Date: 2005-10-31T23:12:06+09:00 Subject: Re: determining whether an object is an immediate? ################ benchmark, round 2 OBJECTS = [1, 3.14159, [], :sym, "astring", true, {}, false, 1..3, nil, /x/] ITERATIONS = 10000 ## change bm(2) do |x| x.report("VClass ") do ITERATIONS.times do OBJECTS.each do |obj| begin class< On 10/31/05, Brian Mitchell wrote: > > > The clone test does not work. Reason? There are plenty of > > non-immediate objects that won't clone like Float and Bignum > > instances. > > > Didn't notice that. I wonder why they didn't make this work. If it were up > to me, I would make all immutable objects (including immediate objects) > return self for clone. > > One test you could use is the singleton class: > > > > # Should raise a TypeError > > class << :immediate_object; end > > > I thought of that one previously too. The problem with this one is that for > non-immediates, it will create singleton/meta classes for them when I don't > need them. Maybe not that big a deal. > > Of course immediate object could also be found missing from > > ObjectSpace, but I would imagine that test to be very slow. > > > Probably the best test for now, but slow. > > May I ask why you need to know the immediacy of an object? > > > > Performance. I don't really "need" to know - since this is an optimization. > I'm doing some code generation (parser generator). When I'm given an > immediate object instead of storing it in a variable and having the code use > the variable, I want immediates to appear directly in the code (because > there is no object creation penalty and I know it is immutable). My > generated code doesn't reassign these variables (but it may modify the > objects) so this works. > > Looking at the C code (ruby.h: SPECIAL_CONST_P), another option would be > this: > > id = obj.__id__ > (id&3).nonzero? || (id&~4).zero? > > As long as ruby continues to encode immediates as it does now (can add > more), this should work.