Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Immediate values

Reply
Thread Tools

Immediate values

 
 
Eustaquio Rangel de Oliveira Jr.
Guest
Posts: n/a
 
      01-10-2005
-----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

http://beam.to/taq
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

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


 
Reply With Quote
 
 
 
 
Austin Ziegler
Guest
Posts: n/a
 
      01-10-2005
On Mon, 10 Jan 2005 19:00:55 +0900, Eustaquio Rangel de Oliveira Jr.
<> 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 *
* Alternate:


 
Reply With Quote
 
 
 
 
Kaspar Schiess
Guest
Posts: n/a
 
      01-10-2005
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_classes.html,
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



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-10-2005

"Austin Ziegler" <> schrieb im Newsbeitrag
news:...
> On Mon, 10 Jan 2005 19:00:55 +0900, Eustaquio Rangel de Oliveira Jr.
> <> 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

 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      01-10-2005
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.
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-10-2005

"Florian Gross" <> schrieb im Newsbeitrag
news:...
> 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

 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      01-10-2005
>>>>> "R" == Robert Klemme <> writes:

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

Look at [ruby-talk:17321]


Guy Decoux


 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      01-10-2005
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-scharpf.de


 
Reply With Quote
 
Eustaquio Rangel de Oliveira Jr.
Guest
Posts: n/a
 
      01-10-2005
-----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

http://beam.to/taq
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4qQkb6UiZnhJiLsRAvBsAJ9NdntqBt0ZoPKAyrfnpr 2J8aQRJwCfS4DX
KO3f8rtIznKnp5pICtOc/+U=
=vBrT
-----END PGP SIGNATURE-----


 
Reply With Quote
 
Eustaquio Rangel de Oliveira Jr.
Guest
Posts: n/a
 
      01-10-2005
-----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-memory.html

kind of things. Maybe something on topic right now also.

- ----------------------------
Eustáquio "TaQ" Rangel

http://beam.to/taq
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Immediate Values gwtmp01@mac.com Ruby 1 01-01-2006 01:20 PM
Enable Content Expiration: Immediate TR ASP .Net 0 05-23-2005 01:51 PM
ASP .NET and ASP style confusion. Immediate solution required =?Utf-8?B?TW9oaXQgR3VwdGE=?= ASP .Net 1 10-18-2004 01:52 PM
CISCO/ $50HR/ IMMEDIATE Tom Gugger Cisco 0 05-19-2004 01:43 PM
CISCO 800 Series 801 Router IMMEDIATE HELP NEEDED!! Thomas Perl Cisco 4 07-16-2003 05:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57