From: RichardOnRails Date: 2009-12-22T05:25:07+09:00 Subject: Re: Ruby's implementation of Fixnum-assignment On Dec 21, 12:26 pm, Robert Klemme wrote: > 2009/12/21 RichardOnRails : > > > I've made the following observations about Ruby's apparent > > implementation: > > > 1. Integer's in the range -2**30 to 2**30-1 are Fixnum's. Integers > > outside that range are Bignum's > > 2. For Fixnum "i",  i's object_id is twice i's value plus 1.  Said > > another way, i's object_id is i''s binary value left-shifted with 1 as > > padding. > > 3. Object_id's of Bignum's close to the boundaries of Fixnum's bear no > > apparent relationship to the Fixnum object_id's. > > All correct. > > > These observations suggest to me that an assignment statement like > > "a=1" requires the interpreter to do no more than: > > 1.  Search for the identifier "a" in the current scope. > > 2.  If found, use it; if not, enter it in the current scope and then > > use it. > > 3. "Use it" means take the binary representation of the assigned > > value,  left shift it with "1" padding, and make that current > > object_id of the identifier "a". > > 4. There is no need to locate free space in an memory pool where > > object values for Bignums, Strings and other object are stored.  For > > Fixnums, the object_id IS the value, just a little shifted. > > > Is my speculation about this aspect of Ruby's implementation correct, > > or am I all wet? > > Hm...  While you have many things right, I don't fully agree.  No > variable (be it a local variable like "a" in your example or an > instance variable like "@a") has an object id.  Object id is a > property of an object which might be referenced by many variables. > Your step 3 actually mixes two separate things: evaluation of the > expression of the right side of "=" and assignment.  "Using" in an > assignments means to take whatever the expression spits out and store > it in the variable. > > Now, what your right hand expression yields is an object reference. > For optimization purposes some object references are special in that > they actually *are* the object (these are the "immediate values" which > Gary mentioned, Fixnums for example).  This does not make special > treatment for assignments necessary.  Rather, it makes special > treatment for _method calls_ necessary.  Because then the interpreter > does not have to look up the object on the heap etc. > > Someone with more intimate knowledge of the implementation might be > able to explain this better.  But I believe it's important to point > out that the difference is not in the assignment but in the right hand > side expression which - in the case of a Fixnum - yields a special > object reference. > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/ Hi Robert, > No variable ... has an object id. Robert, I know, by virtue of reading a number of your responses in this NG, that you’re a Ruby expert. But the statement referenced above is contradicted by at least two authorities on Ruby: 1. “... every object has a unique object identifier (abbreviated as object ID)” [“Programming Ruby”, Second Edition, Thomas, et al, p. 12] , where you praised the book’s usefulness :-). Of course, the “id” method has since be deprecated to “object_id” when we want to reference an object ID. 2. “... an object in Ruby has an identity: “abc”object_id #53744407 This object ID is of limited utility." [“The Ruby Way”, Second Edition, Fulton, p.26] > step 3 actually mixes two separate things: evaluation of the expression of the right side of "=" and assignment Granted, I stuck to what I viewed as the essential issues. Perhaps a more complete estimate might read. 3.1 Ruby interprets “a = 1” to mean “a.=(1)”, i.e. invokes the “=” method on the object. Normally, that reference will cascade up to Object.=, I suppose. 3.1a If “a” is undefined in the current scope, “a” will be added to the scope with an ID of (2a+1) = 3. 3.1b Otherwise: 3.1.b.1 If a’s ID is in the range of Fixnums, (or a’s ID indicates it’s true, false, etc .), a’s ID will be set to 3 3.1b.2 Otherwise, if a’s ID indicates any other type, a’s ID will be pushed to the garbage stack and a’s ID will be set to 3 Is that better? > For optimization purposes some object references are special in that they actually *are* the object That sounds like you mean in the case of Fixnum “1”, that the number 1 IS the ID as well as the value of the object. But we can see “puts 1.object_id” => 3 So I think that fact (plus all the other evidence I offered) suggests that ID’s for Fixnum’s are manufactured as 2*value+1 QED??? As always, thanks for your insightful response. Richard