Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How can I pin a Ruby object in memory?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  16 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Lam  
View profile  
 More options Jun 1 2006, 5:10 pm
From: "John Lam" <drjf...@gmail.com>
Date: Fri, 2 Jun 2006 06:10:00 +0900
Subject: How can I pin a Ruby object in memory?
I have some data that I'm storing in a T_DATA VALUE. Is the data
that's stored there part of the GC heap - IOW can it move in memory?
If so, is there a way to pin it so that it doesn't move while I'm
using it?

Thanks!
-John
http://www.iunknown.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lyle Johnson  
View profile  
 More options Jun 1 2006, 5:30 pm
From: "Lyle Johnson" <lyle.john...@gmail.com>
Date: Fri, 2 Jun 2006 06:30:49 +0900
Local: Thurs, Jun 1 2006 5:30 pm
Subject: Re: How can I pin a Ruby object in memory?
On 6/1/06, John Lam <drjf...@gmail.com> wrote:

> I have some data that I'm storing in a T_DATA VALUE. Is the data
> that's stored there part of the GC heap - IOW can it move in memory?
> If so, is there a way to pin it so that it doesn't move while I'm
> using it?

Not sure if I understand the question. A Data object has a pointer
(RDATA(obj)->data) to some block of memory that you've allocated, and
no, Ruby's GC process isn't going to assign some new value to that
pointer.

If you're asking whether Ruby will move the address of the Data object
itself: I'm guessing that that's possible.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Minkoo Seo  
View profile  
 More options Jun 1 2006, 5:35 pm
From: "Minkoo Seo" <minkoo....@gmail.com>
Date: Fri, 2 Jun 2006 06:35:07 +0900
Local: Thurs, Jun 1 2006 5:35 pm
Subject: Re: How can I pin a Ruby object in memory?

Hi.

On 6/2/06, Lyle Johnson <lyle.john...@gmail.com> wrote:

> If you're asking whether Ruby will move the address of the Data object
> itself: I'm guessing that that's possible.

I guess this is not true, because Ruby's GC does not compact
memory (at least up to now).

- Minkoo Seo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Lam  
View profile  
 More options Jun 1 2006, 6:43 pm
From: "John Lam" <drjf...@gmail.com>
Date: Fri, 2 Jun 2006 07:43:28 +0900
Local: Thurs, Jun 1 2006 6:43 pm
Subject: Re: How can I pin a Ruby object in memory?

On 6/1/06, Lyle Johnson <lyle.john...@gmail.com> wrote:

> If you're asking whether Ruby will move the address of the Data object
> itself: I'm guessing that that's possible.

I was wondering about the latter. I couldn't find any APIs for pinning
objects in memory so I was worried that the object might move out from
underneath me. But on second thought I'd have the DATA pointer cached in a
register / call stack in any event so it probably doesn't matter if the
object moves in the future.

Cheers,
-John
http://www.iunknown.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel VanderWerf  
View profile  
 More options Jun 1 2006, 6:47 pm
From: Joel VanderWerf <vj...@path.berkeley.edu>
Date: Fri, 2 Jun 2006 07:47:41 +0900
Local: Thurs, Jun 1 2006 6:47 pm
Subject: Re: How can I pin a Ruby object in memory?

Lyle Johnson wrote:
> If you're asking whether Ruby will move the address of the Data object
> itself: I'm guessing that that's possible.

If ruby moved objects like that (whether T_DATA or T_OBJECT, T_STRING,
etc), it would be a disaster. Every VALUE that referred to the object
(in other words every reference to it in a variable, array, hash, etc.)
would become invalid, since the VALUE type is actually a pointer in
these cases. (I may be misunderstanding the question though...)

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options Jun 1 2006, 7:15 pm
From: Logan Capaldo <logancapa...@gmail.com>
Date: Fri, 2 Jun 2006 08:15:39 +0900
Local: Thurs, Jun 1 2006 7:15 pm
Subject: Re: How can I pin a Ruby object in memory?

On Jun 1, 2006, at 6:47 PM, Joel VanderWerf wrote:

> Lyle Johnson wrote:
>> If you're asking whether Ruby will move the address of the Data  
>> object
>> itself: I'm guessing that that's possible.

> If ruby moved objects like that (whether T_DATA or T_OBJECT, T_STRING,
> etc), it would be a disaster. Every VALUE that referred to the object
> (in other words every reference to it in a variable, array, hash,  
> etc.)
> would become invalid, since the VALUE type is actually a pointer in
> these cases. (I may be misunderstanding the question though...)

> --
>       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

No ruby does not move objects in memory. As to how horrible that  
would be if it did, there are GCs that do work like this (Copying  
GC). Believe it or not there are speed advantages to copying gcs in  
that the algorithm has runtime  proportional to the number of  
reachable objects, rather than the size of the heap like mark-and-
sweep (which is what ruby uses). Copying collectors also compact the  
the memory, reducing fragmentation. A copying GC would be difficult  
in the current ruby implementation since a copying gc cannot really  
be conservative (it has to change things in the root set), and ruby  
uses the C stack so it is difficult to be sure if something is  
definitely _not_ a pointer. With mark-and-sweep false positives are  
ok, since nothing ever gets moved. With a copying gc it could mistake  
an int on the c stack for a pointer "collect" the "object" it  
"pointed" to and then change the value. Which of course would be the  
cause of many odd and subtle bugs in ruby code.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Lam  
View profile  
 More options Jun 1 2006, 8:23 pm
From: "John Lam" <drjf...@gmail.com>
Date: Fri, 2 Jun 2006 09:23:04 +0900
Local: Thurs, Jun 1 2006 8:23 pm
Subject: Re: How can I pin a Ruby object in memory?
So I would guess that Ruby memory allocation is relatively expensive?
Certainly nowhere near as fast as allocating memory off of the "end"
of the heap or the stack, right? Does it have to search a free list of
blocks itself or does it delegate allocation to the system's malloc()
implementation?

It's tricky doing the interop with the CLR since things like boxed
value type objects *can* be moved in memory, so I need create a pinned
GCHandle object to keep the GC from moving the object (this is also
bad as you could imagine since it leads to heap fragmentation). So
after spending most of the day thinking about the CLR side of the
house, I was a bit surprised to find that Ruby doesn't move objects
around.

This makes me a bit happier in a way since I don't have to worry about
the issues on both sides of the house, but since I figured out how to
do it on the CLR side, I was hoping to reuse that new-found experience
on the Ruby side :)

Thanks for the insights.
-John
http://www.iunknown.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options Jun 2 2006, 3:57 am
From: Logan Capaldo <logancapa...@gmail.com>
Date: Fri, 2 Jun 2006 16:57:54 +0900
Local: Fri, Jun 2 2006 3:57 am
Subject: Re: How can I pin a Ruby object in memory?

On Jun 1, 2006, at 8:23 PM, John Lam wrote:

> So I would guess that Ruby memory allocation is relatively expensive?
> Certainly nowhere near as fast as allocating memory off of the "end"
> of the heap or the stack, right? Does it have to search a free list of
> blocks itself or does it delegate allocation to the system's malloc()
> implementation?

Speaking without any knowledge of ruby's internals I imagine it's  
actually is just allocating from the end of some pre-allocated buffer  
until it reaches the end of the buffer. So if you never run out of  
room in the buffer the allocation is just incrementing a pointer.  
When you reach the end you do the first GC and subsequent allocations  
have to search the freelist for a big enough chunk.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mauricio Fernandez  
View profile  
 More options Jun 2 2006, 4:44 am
From: Mauricio Fernandez <m...@acm.org>
Date: Fri, 2 Jun 2006 17:44:46 +0900
Local: Fri, Jun 2 2006 4:44 am
Subject: Re: How can I pin a Ruby object in memory?

On Fri, Jun 02, 2006 at 04:57:54PM +0900, Logan Capaldo wrote:
> On Jun 1, 2006, at 8:23 PM, John Lam wrote:

> >So I would guess that Ruby memory allocation is relatively expensive?
> >Certainly nowhere near as fast as allocating memory off of the "end"
> >of the heap or the stack, right? Does it have to search a free list of
> >blocks itself or does it delegate allocation to the system's malloc()
> >implementation?

> Speaking without any knowledge of ruby's internals I imagine it's  
> actually is just allocating from the end of some pre-allocated buffer  
> until it reaches the end of the buffer. So if you never run out of  
> room in the buffer the allocation is just incrementing a pointer.  
> When you reach the end you do the first GC and subsequent allocations  
> have to search the freelist for a big enough chunk.

Ruby does not use a compacting GC and doesn't manage memory itself (the way a
normal memory allocator does) either.
There are two parts to allocating an object:
* each non-immediate object takes a sizeof(RVALUE)-sized slot (typically 20
  bytes) from one of the heaps managed by ruby (look for RVALUE and heaps in
  gc.c). It's sizeof(RVALUE) for any object so there's no problem with "chunk
  sizes" and fragmentation (iow. all chunks are ~20 bytes long). A freelist
  is used to find unused slots in said heaps. Additional heaps of increasing
  size will be created when there are no free slots or too few were freed in a
  GC run.
* most objects need additional memory (pointed to by fields in their
  corresponding slots): instance variable tables, char* for Strings, VALUE*
  for Arrays... these are allocated with malloc and will be freed when the
  corresponding object is reclaimed.

ruby relies on malloc(3) for low-level allocation, instead of doing it all
with sbrk(2) and friends.

--
Mauricio Fernandez  -   http://eigenclass.org   -  singular Ruby


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options Jun 2 2006, 2:25 pm
From: Logan Capaldo <logancapa...@gmail.com>
Date: Sat, 3 Jun 2006 03:25:02 +0900
Local: Fri, Jun 2 2006 2:25 pm
Subject: Re: How can I pin a Ruby object in memory?

On Jun 2, 2006, at 4:44 AM, Mauricio Fernandez wrote:

Interesting. (-- takes notes --). Almost seems like cheating :). But  
in a good way. I'm going to have read gc.c. Speaking of reading ruby  
source, is there an order you would recommend? Every time I look at  
it I get overwhelmed by a) not knowing where to start and b) K&R C. I  
can power-through the K&R C for the most part I think, but figuring  
out what to read when is tougher.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lyle Johnson  
View profile  
 More options Jun 2 2006, 2:45 pm
From: "Lyle Johnson" <lyle.john...@gmail.com>
Date: Sat, 3 Jun 2006 03:45:06 +0900
Local: Fri, Jun 2 2006 2:45 pm
Subject: Re: How can I pin a Ruby object in memory?
On 6/1/06, Joel VanderWerf <vj...@path.berkeley.edu> wrote:

> If ruby moved objects like that (whether T_DATA or T_OBJECT, T_STRING,
> etc), it would be a disaster. Every VALUE that referred to the object
> (in other words every reference to it in a variable, array, hash, etc.)
> would become invalid, since the VALUE type is actually a pointer in
> these cases.

You know, I did know that, but it didn't occur to me at the time. Good point.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pierre-Charles David  
View profile  
 More options Jun 2 2006, 2:51 pm
From: "Pierre-Charles David" <pcda...@gmail.com>
Date: Sat, 3 Jun 2006 03:51:03 +0900
Local: Fri, Jun 2 2006 2:51 pm
Subject: Re: How can I pin a Ruby object in memory?
2006/6/2, Logan Capaldo <logancapa...@gmail.com>:

> Interesting. (-- takes notes --). Almost seems like cheating :). But
> in a good way. I'm going to have read gc.c. Speaking of reading ruby
> source, is there an order you would recommend? Every time I look at
> it I get overwhelmed by a) not knowing where to start and b) K&R C. I
> can power-through the K&R C for the most part I think, but figuring
> out what to read when is tougher.

_why had an interesting article last summer about the internals of
Ruby's memory management and how to use it efficiently:
http://whytheluckystiff.net/articles/theFullyUpturnedBin.html

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dominik Bathon  
View profile  
 More options Jun 2 2006, 4:25 pm
From: "Dominik Bathon" <dba...@gmx.de>
Date: Sat, 3 Jun 2006 05:25:11 +0900
Local: Fri, Jun 2 2006 4:25 pm
Subject: Re: How can I pin a Ruby object in memory?
Hi,

On Fri, 02 Jun 2006 20:25:02 +0200, Logan Capaldo <logancapa...@gmail.com>  
wrote:

> Interesting. (-- takes notes --). Almost seems like cheating :). But in  
> a good way. I'm going to have read gc.c. Speaking of reading ruby  
> source, is there an order you would recommend? Every time I look at it I  
> get overwhelmed by a) not knowing where to start and b) K&R C. I can  
> power-through the K&R C for the most part I think, but figuring out what  
> to read when is tougher.

Have you seen the "Ruby Hacking Guide" translation at  
http://rhg.rubyforge.org/ ?

It's not complete, but it should definitely get you started.

Dominik


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel VanderWerf  
View profile  
 More options Jun 4 2006, 4:30 pm
From: Joel VanderWerf <vj...@path.berkeley.edu>
Date: Mon, 5 Jun 2006 05:30:29 +0900
Local: Sun, Jun 4 2006 4:30 pm
Subject: Re: How can I pin a Ruby object in memory?

Lyle Johnson wrote:
> On 6/1/06, Joel VanderWerf <vj...@path.berkeley.edu> wrote:

>> If ruby moved objects like that (whether T_DATA or T_OBJECT, T_STRING,
>> etc), it would be a disaster. Every VALUE that referred to the object
>> (in other words every reference to it in a variable, array, hash, etc.)
>> would become invalid, since the VALUE type is actually a pointer in
>> these cases.

> You know, I did know that, but it didn't occur to me at the time. Good
> point.

I had no doubt that you knew it; we would not have FXRuby otherwise :)

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mauricio Fernandez  
View profile  
 More options Jun 5 2006, 4:01 pm
From: Mauricio Fernandez <m...@acm.org>
Date: Tue, 6 Jun 2006 05:01:49 +0900
Local: Mon, Jun 5 2006 4:01 pm
Subject: Re: How can I pin a Ruby object in memory?

On Sat, Jun 03, 2006 at 03:25:02AM +0900, Logan Capaldo wrote:
> On Jun 2, 2006, at 4:44 AM, Mauricio Fernandez wrote:
[...]
> >ruby relies on malloc(3) for low-level allocation, instead of doing  it all
> >with sbrk(2) and friends.

> Interesting. (-- takes notes --). Almost seems like cheating :). But  
> in a good way. I'm going to have read gc.c. Speaking of reading ruby  
> source, is there an order you would recommend? Every time I look at  
> it I get overwhelmed by a) not knowing where to start and b) K&R C. I  
> can power-through the K&R C for the most part I think, but figuring  
> out what to read when is tougher.

It depends on what you're interested in (/me slaps self). The easiest starting
points would be array.c, hash.c (st.c if you really want to see the underlying
st_table implementation, but it's just your regular hash table), string.c...
that is, the core data structures. They are very easy to read, but maybe not
that interesting ultimately due to this very straightforwardness.

As for the more interesting stuff, here are some functions to begin with:
* eval.c:
  * rb_eval: the basic AST walker
  * rb_call, rb_get_method_body: method dispatching (+method cache) at work
  * rb_add_method: managing the method tables (m_tbl)
  * rb_include_module: to see how proxy classes (T_ICLASS) work; bits of
    Ruby's object model
  ....  
* parse.y: the grammar + yylex (*tricky*)

This is what I answered to a similar question 3 years ago in [74002]:

    Ruby Core
     * dln.c: wraps dlopen or the equiv. function of your platform, not very
       interesting
     * gc.c: quite easy to follow, of interest only if you want to know how
        the GC works internally, but it's just mark & sweep doing "common
        sense" things so you can safely skip it.
     * st.c: a hash table implementation used internally by Ruby, quite
       straightforward
     * eval.c: much harder to read as you have to know the node types to
       follow it; several functions are essentially a big switch() statement
       for a node
     * parse.y: this can help you see what different node types correspond
       to by having a look at the grammar.
     * regex.c: whatever, don't read it :-)

    some other .c files contain only support code

    Built-in classes
    Take the class you like, scroll down to the Init_xxx() function and
    locate the C function that implements the method you want to study. No
    particular order required.

Hope this helps,

--
Mauricio Fernandez  -   http://eigenclass.org   -  singular Ruby


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options Jun 6 2006, 9:30 am
From: Logan Capaldo <logancapa...@gmail.com>
Date: Tue, 6 Jun 2006 22:30:43 +0900
Local: Tues, Jun 6 2006 9:30 am
Subject: Re: How can I pin a Ruby object in memory?

On Jun 5, 2006, at 4:01 PM, Mauricio Fernandez wrote:

> [snip my "homework" for the rest of the summer]

> Hope this helps,

It does, thanks.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google