[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Immediate values

Eustaquio Rangel de Oliveira Jr.

1/10/2005 10:01:00 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

I heard that immediate values holds the object, not a reference to it, is
that right?
I mean:

s1 = "test" # a String located on for ex 0xCC53D5DF
s2 = s1 # points to the same place as s1
s3 = "test" # ANOTHER String, locate on for ex 0xC0DD54D0
n1 = 1 # Fixnum here, located on ... ?
n2 = n1 # points to the same place as n1
n3 = 1 # points to the same place as n1

So, Fixnum (as true, false and nil) objects uses the same object for all
over the program, but Strings, for ex, does not, even if the value are the
same there are distinct objects, right?

On the end, n1, n2 and n3 are not reference to this only one object
allocated there, shared by all? Variables are not all references, even on
the Fixnum case, pointing to an allocated single object there?

And I think the mark-and-sweep garbage collector works on the same way as
other objects to Fixnum, true, false and nil right?

Thanks! :-)

- ----------------------------
Eustáquio "TaQ" Rangel
eustaquiorangel@yahoo.com
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFB4lJNb6UiZnhJiLsRAqS1AJ9PG+dTod2hRCEJAo71ciqIY+KiMQCdGbxU
yj5Dg/5NfkpW+Qnislw9Nz4=
=DRR4
-----END PGP SIGNATURE-----


32 Answers

Austin Ziegler

1/10/2005 12:08:00 PM

0

On Mon, 10 Jan 2005 19:00:55 +0900, Eustaquio Rangel de Oliveira Jr.
<eustaquiorangel@yahoo.com> wrote:
> I heard that immediate values holds the object, not a reference to
> it, is that right?
>
> I mean:
>
> s1 = "test" # a String located on for ex 0xCC53D5DF
> s2 = s1 # points to the same place as s1
> s3 = "test" # ANOTHER String, locate on for ex 0xC0DD54D0
> n1 = 1 # Fixnum here, located on ... ?
> n2 = n1 # points to the same place as n1
> n3 = 1 # points to the same place as n1
>
> So, Fixnum (as true, false and nil) objects uses the same object
> for all over the program, but Strings, for ex, does not, even if
> the value are the same there are distinct objects, right?
>
> On the end, n1, n2 and n3 are not reference to this only one
> object allocated there, shared by all? Variables are not all
> references, even on the Fixnum case, pointing to an allocated
> single object there?

Up until this paragraph I was with you and completely agree.
Variables are all references, even to Fixnum and Symbol objects.
It's an implementation detail that all Fixnum and Symbol object
instances s are references to the same object, IMO.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Kaspar Schiess

1/10/2005 12:09:00 PM

0

Hello,

I have already answered to this on the irc channel, but will try to
collect the answers given as an answer for the history:

Ruby variables hold references to objects. Objects are allocated on the
heap, so a variable saves a pointer to the heap.

n1 = "test"
n2 = n1

# n1 and n2 point to the _same_ object.
n1.object_id == n2.object_id # > true

So you do have to take care not to modify n1, otherwise you will be
modifying n2 too. This is actually less often a problem than one would
think.

# look here
n1 += 'test' # creates new object and leaves n2 alone

# of course
n1.sub! /est/, 'ry' # will modify n1 and n2

Look also here: http://www.rubycentral.com/book/tut_cl...,
starting at the title 'Variables'.

Of course that is what you need to think about objects, rather than what
it looks like behind the facade. If you are interested in the underlying
reality I guess you need to look at some code.

best regards,
kaspar



Robert Klemme

1/10/2005 2:44:00 PM

0


"Austin Ziegler" <halostatue@gmail.com> schrieb im Newsbeitrag
news:9e7db91105011004083d19a85f@mail.gmail.com...
> On Mon, 10 Jan 2005 19:00:55 +0900, Eustaquio Rangel de Oliveira Jr.
> <eustaquiorangel@yahoo.com> wrote:
> > I heard that immediate values holds the object, not a reference to
> > it, is that right?
> >
> > I mean:
> >
> > s1 = "test" # a String located on for ex 0xCC53D5DF
> > s2 = s1 # points to the same place as s1
> > s3 = "test" # ANOTHER String, locate on for ex 0xC0DD54D0
> > n1 = 1 # Fixnum here, located on ... ?
> > n2 = n1 # points to the same place as n1
> > n3 = 1 # points to the same place as n1
> >
> > So, Fixnum (as true, false and nil) objects uses the same object
> > for all over the program, but Strings, for ex, does not, even if
> > the value are the same there are distinct objects, right?
> >
> > On the end, n1, n2 and n3 are not reference to this only one
> > object allocated there, shared by all? Variables are not all
> > references, even on the Fixnum case, pointing to an allocated
> > single object there?
>
> Up until this paragraph I was with you and completely agree.
> Variables are all references, even to Fixnum and Symbol objects.
> It's an implementation detail that all Fixnum and Symbol object
> instances s are references to the same object, IMO.

Totally agree! And it doesn't make a difference from the usage point of
view since instances of Symbol, Fixnum, TrueClass, FalseClass, NilClass
are immutable.

Addenum: it's especially noteworthy that strings literals are treated
differently from true, false, Fixnums and Symbols. They create a new
string instance on each evaluation.

>> 5.times { p ["foo", 'foo', :foo, 1, 1.2, 10000000000000, true, false,
nil].map {|o| o.id} }
[134663704, 134663692, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134661376, 134661316, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134660428, 134660392, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134659348, 134659336, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134656588, 134656372, 3938574, 3, 134665492, 134665228, 2, 0, 4]
=> 5

Kind regards

robert

georgesawyer

1/10/2005 2:51:00 PM

0

"Eustaquio Rangel de Oliveira Jr." <eustaquiorangel@yahoo.com> Jan 10, 2005
at 07:00 PM wrote:
>I heard that immediate values hold the object, not a reference to it; is
that right? I mean:
>s1 = "test" # a String located on for ex 0xCC53D5DF
>s2 = s1 # points to the same place as s1
>s3 = "test" # ANOTHER String, locate on for ex 0xC0DD54D0
>n1 = 1 # Fixnum here, located on ... ?
>n2 = n1 # points to the same place as n1
>n3 = 1 # points to the same place as n1
>So, Fixnum (as true, false and nil) objects use the same object for all
over the program, but Strings, for ex, do not: even if the values are the
same, they are distinct objects; right?

Moreover, during program execution, each execution of the same line
containing a literal String creates a different object:

->for i in (1..2) do a='test'; p a.object_id end
20816438
20816414
->b=a; b.object_id
20816414

>On the end, are not n1, n2 and n3 references to this [single] object
allocated there, shared by all? Are not variables all references, even in
the Fixnum case, pointing to an allocated single object there?

Fixnum objects are immutable. A variable can refer to different Fixnum's.
That's the ideal, and Ruby's behavior. About implementation efficiency and
understanding, from _Programming Ruby (1st ed.)_, chapter "Extending Ruby",
section "Ruby Objects in C":

"[I]mmediate values are not pointers: Fixnum, Symbol, true, false, and nil
are stored directly in VALUE. Fixnum values are stored as 31-bit numbers[Or
63-bit on wider CPU architectures.] that are formed by shifting the
original number left 1 bit and then setting the least significant bit (bit
0) to ``1.'' When VALUE is used as a pointer to a specific Ruby structure,
it is guaranteed always to have an LSB of zero; the other immediate values
also have LSBs of zero. Thus, a simple bit test can tell you whether or not
you have a Fixnum."

->for i in (1..2) do a=10; p a.object_id end
21
21
->b=a; p b.object_id; b=nil; b.object_id
21
4

>I think the garbage collector works the same way as other objects on
Fixnum, true, false and nil, right?

Naturally, it ignores immutable objects. You can create millions of Fixnum
objects, yet they take no storage. Not so for String or Bignum objects:

->a=1_000; GC.disable; for i in (1..100_000_000) do b=a*2 end
1..100000000
->a='z'*1_000; GC.disable; for i in (1..1_000_000) do b=a*2 end
(irb):2:in `*': failed to allocate memory (NoMemoryError)
->a=1_000_000_000_000_000_000_000; GC.disable; for i in (1..10_000_000)
do b=a*2 end
[FATAL] failed to allocate memory
->system 'ruby --version'
ruby 1.8.2 (2004-07-29) [i386-mswin32]

Florian Gross

1/10/2005 2:58:00 PM

0

Robert Klemme wrote:

> Totally agree! And it doesn't make a difference from the usage point of
> view since instances of Symbol, Fixnum, TrueClass, FalseClass, NilClass
> are immutable.

Note that there is a small difference for Symbols and Fixnums. You can
not define singleton methods on those objects. Maybe external singleton
classes that work like exivars would help with that.

> Addenum: it's especially noteworthy that strings literals are treated
> differently from true, false, Fixnums and Symbols. They create a new
> string instance on each evaluation.

But note that literals with the same content will actually share the
String buffer.

Robert Klemme

1/10/2005 3:11:00 PM

0


"Florian Gross" <flgr@ccan.de> schrieb im Newsbeitrag
news:34fjg2F48l5qgU1@individual.net...
> Robert Klemme wrote:
>
> > Totally agree! And it doesn't make a difference from the usage point
of
> > view since instances of Symbol, Fixnum, TrueClass, FalseClass,
NilClass
> > are immutable.
>
> Note that there is a small difference for Symbols and Fixnums. You can
> not define singleton methods on those objects.

Yep.

> Maybe external singleton
> classes that work like exivars would help with that.

Uh, oh... What's an "exivar"?

> > Addenum: it's especially noteworthy that strings literals are treated
> > differently from true, false, Fixnums and Symbols. They create a new
> > string instance on each evaluation.
>
> But note that literals with the same content will actually share the
> String buffer.

True (yet another optimization). But only as long as they are not
modified. It's about the same behavior as #dup was called on some string
held behind the scenes. Still another object instance has to be allocated
which is why in performance critical parts you're usually better off
defining a frozen string constant if the string doesn't change anyway.

class Dummy
SAMPLE = "foo".freeze

def call_often(str)
str.include? SAMPLE
end
end

Kind regards

robert

ts

1/10/2005 3:20:00 PM

0

>>>>> "R" == Robert Klemme <bob.news@gmx.net> writes:

R> Uh, oh... What's an "exivar"?

Look at [ruby-talk:17321]


Guy Decoux


Bertram Scharpf

1/10/2005 3:41:00 PM

0

Hi,

Am Montag, 10. Jan 2005, 21:09:03 +0900 schrieb Kaspar Schiess:
> Ruby variables hold references to objects.

Stricly spoken, Fixnums don't. They are treated a special
way.

> If you are interested in the underlying
> reality I guess you need to look at some code.

And here it comes (paste to `irb'):

max = 2**30
a = Array.new 5_000 do rand max end ; nil
a.all? { |i| i == i.object_id >> 1 }
a.any? { |i| (i.object_id & 0x1).zero? }

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


Eustaquio Rangel de Oliveira Jr.

1/10/2005 3:51:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

| "[I]mmediate values are not pointers: Fixnum, Symbol, true, false, and nil
| are stored directly in VALUE. Fixnum values are stored as 31-bit numbers[Or
| 63-bit on wider CPU architectures.] that are formed by shifting the
| original number left 1 bit and then setting the least significant bit (bit
| 0) to ``1.'' When VALUE is used as a pointer to a specific Ruby structure,
| it is guaranteed always to have an LSB of zero; the other immediate values
| also have LSBs of zero. Thus, a simple bit test can tell you whether or not
| you have a Fixnum."

Forgive me if I misunderstood, but so VALUEs are variables?

a = 1

a is the VALUE, with 32 bit length?

So, to find the object_id of *all* the Fixnum, all I have to do is
something like:

[taq@~]irb
irb(main):001:0> i = 3
=> 3
irb(main):002:0> s = "0b" << i.to_s(2) << "1"
=> "0b111"
irb(main):003:0> s.to_i(2)
=> 7

The object_id of 3 always will be 7 right? But i does not point to
somewhere on memory there? i is the own object?
And when I have a bit 1 there on LSB is *always* a Fixnum?

| Naturally, it ignores immutable objects. You can create millions of Fixnum
| objects, yet they take no storage. Not so for String or Bignum objects:

Ok, but how it does that? I mean, I have 31 (32?) bit numbers there on my
one thousand vars on the local scope. Where they are stored? They are
somewhere on the memory, right? How it can take no storage? Automatically
killed after it scopes ends? But before this, where they are? :-)

Sorry if I'm asking a lot about that, but immediate values made me curious
about it. :-)

Regards,

- ----------------------------
Eustáquio "TaQ" Rangel
eustaquiorangel@yahoo.com
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFB4qQkb6UiZnhJiLsRAvBsAJ9NdntqBt0ZoPKAyrfnpr2J8aQRJwCfS4DX
KO3f8rtIznKnp5pICtOc/+U=
=vBrT
-----END PGP SIGNATURE-----


Eustaquio Rangel de Oliveira Jr.

1/10/2005 4:01:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Btw, I'm trying to understand how these things works to see if it avoids
this

http://evanjones.ca/python-m...

kind of things. Maybe something on topic right now also. ;-)

- ----------------------------
Eustáquio "TaQ" Rangel
eustaquiorangel@yahoo.com
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFB4qa/b6UiZnhJiLsRAik7AKCdcRhL6q2jgeu2YHc30haonwd1awCgr/7b
aRNxWaC9MACCcfgYSUUTcqk=
=X3Fl
-----END PGP SIGNATURE-----