An Object Going Out Of Scope

“Jim Weirich” jweirich@one.net wrote in message
news:1053406677.17559.8.camel@traken…

How about this syntax …

auto(foo=MyObject.new, :cleanup) do
# use foo
end

fine if you can handle multiple objects this way. Mauricios suggestion does
that, but does not allow you to specify cleanup method. This, of course can
be done in many ways.

{ auto(:$critsec, :exit); @critsec.enter; … }

Critcal sections are better handled by blocks …

lock(mutex) do
# critical section
end

Like files, except when you want both at the same time. You can probably get
a way with nesting though.

Mikkel

i take it back, my problems still occur even if i call rb_gc directly from
some native method i write, — i’ll investigate if this is my fault instead
of ruby’s

cheers

···

-----Original Message-----
From: Gaffer [mailto:gaffer@gaffer.org]
Sent: Sunday, May 18, 2003 10:10 PM
To: ruby-talk ML
Subject: Re: ruby garbage collection

strange, i found the rb_gc call on my own and called that to good effect
(after a nasty false start with rb_gc_force_recycle exported on one of my
objects)

it seems to work, where calling GC.start from ruby still has the slowdown
for about 10secs, as if some background processing is going on

cant really explain what i’m experiencing then, if rb_gc_start just calls
rb_gc directly :frowning:

cheers

-----Original Message-----
From: Mauricio Fernandez [mailto:batsman.geo@yahoo.com]
Sent: Sunday, May 18, 2003 9:57 PM
To: ruby-talk ML
Subject: Re: ruby garbage collection

On Sun, May 18, 2003 at 08:35:11PM +0900, Gaffer wrote:

i need this for a realtime game application which has embedded ruby –
after
destroying all objects in my scene and recreating everything, ruby gc
takes
about 10seconds to ‘think it through’ and collect all garbage… during
which time the framerate drops in half

i’d much rather it was all done in one hit in between levels, but i cannot
find an API to do this, and GC.start seems async

so,

assuming GC.start is asynchronous, is there a way to force mark/sweep GC
to
occur, blocking for however long it takes to complete?

AFAIK GC.start forces GC to take place right away:
gc.c

rb_define_singleton_method(rb_mGC, “start”, rb_gc_start, 0);

VALUE
rb_gc_start()
{
rb_gc();


void
rb_gc()
{

[marks stuff]

 gc_sweep();

}


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

need help: my first packet to my provider gets lost :frowning:
sel: dont send the first one, start with #2

  • netgod is kidding

it seems to work, where calling GC.start from ruby still has the slowdown
for about 10secs, as if some background processing is going on

are you marking ruby instances yourself ?

can you estimate roughly how many ruby-instances you have ?
B: 500 < n < 5000
C: 5000 < n < 50000
D: 50000 < n < infinite

perhaps you are having a thread which is consuming all time ?
maybe pause all threads during GC can help (untested).
rb_thread_stop_timer();
rb_gc_start();
rb_thread_start_timer();

···

On Sun, 18 May 2003 22:10:18 +0900, Gaffer wrote:
A: 0 < n < 500


Simon Strandgaard

The syntax is not a big deal. One idea is to allow “auto” to take a
list of variables. Here’s another idea that’s more general approach
(although a bit more verbose …)

auto_block do |closer|
a = closer.register(Thing.new) { |thing| thing.close }
b = closer.register(Stuff.new) { |x| x.shut_down }
# do stuff with a & b…
end # closer will close anything registered with it.

···

On Tue, 2003-05-20 at 16:14, MikkelFJ wrote:

“Jim Weirich” jweirich@one.net wrote in message

auto(foo=MyObject.new, :cleanup) do
# use foo
end

fine if you can handle multiple objects this way. Mauricios suggestion does
that, but does not allow you to specify cleanup method. This, of course can
be done in many ways.


– Jim Weirich jweirich@one.net http://jimweirich.umlcoop.net

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

It seems you overlooked the fact that it is not implementable in current
Ruby, for it needs Procs to be rebound :slight_smile:

It’d be easy to change it to specify the cleanup method, should it run
at all.

def auto(hash)

end

and then

auto( :a => :finalize, :file => close ) do
file = File.new(“/tmp/bla”)
a = Something.new
end

···

On Wed, May 21, 2003 at 05:14:06AM +0900, MikkelFJ wrote:

“Jim Weirich” jweirich@one.net wrote in message
news:1053406677.17559.8.camel@traken…

How about this syntax …

auto(foo=MyObject.new, :cleanup) do
# use foo
end

fine if you can handle multiple objects this way. Mauricios suggestion does
that, but does not allow you to specify cleanup method. This, of course can
be done in many ways.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

#Debian makes me feel all warm and fuzzy inside. :slight_smile:
– HippieGuy on #Debian

i think its actually the GC cleaning up matrix and vector classes (my own
implementation extended in ruby, they just alloc and free using c++
new/delete operators, matching my c++ classes: vector (float[4]} and matrix
float[16];

the slowdowns appear to be caused by the gc collecting at longer and longer
intervals, – initially it works quite well, and it gc’s every sec or faster

later on it seems to buffer up gc for about 5-10 secs, and takes a good
chunk of time each sweep

also, i’m not doing any marking myself in c, these are pretty primitive c++
classes, and never ref any other ruby objects, so i just implement the free
function ptr for the classes

cheers

···

-----Original Message-----
From: Simon Strandgaard [mailto:0bz63fz3m1qt3001@sneakemail.com]
Sent: Sunday, May 18, 2003 10:30 PM
To: ruby-talk ML
Subject: Re: ruby garbage collection

On Sun, 18 May 2003 22:10:18 +0900, Gaffer wrote:

it seems to work, where calling GC.start from ruby still has the slowdown
for about 10secs, as if some background processing is going on

are you marking ruby instances yourself ?

can you estimate roughly how many ruby-instances you have ?
A: 0 < n < 500
B: 500 < n < 5000
C: 5000 < n < 50000
D: 50000 < n < infinite

perhaps you are having a thread which is consuming all time ?
maybe pause all threads during GC can help (untested).
rb_thread_stop_timer();
rb_gc_start();
rb_thread_start_timer();


Simon Strandgaard

  rb_thread_stop_timer();

No, you don't need this.

If you really need this, this mean that you have a bug in an extension
that you use (see [ruby-talk:62437] for an example)

Guy Decoux

“Mauricio Fernández” batsman.geo@yahoo.com schrieb im Newsbeitrag
news:20030521055137.GA6046@student.ei.uni-stuttgart.de

“Jim Weirich” jweirich@one.net wrote in message
news:1053406677.17559.8.camel@traken…

How about this syntax …

auto(foo=MyObject.new, :cleanup) do
# use foo
end

fine if you can handle multiple objects this way. Mauricios suggestion
does
that, but does not allow you to specify cleanup method. This, of
course can
be done in many ways.

It seems you overlooked the fact that it is not implementable in current
Ruby, for it needs Procs to be rebound :slight_smile:

I’m not sure, whether I understand you correctly. The solution below does
work. Why then is it not possible without rebinding procs?

It’d be easy to change it to specify the cleanup method, should it run
at all.

def auto(hash)

end

and then

auto( :a => :finalize, :file => close ) do
file = File.new(“/tmp/bla”)
a = Something.new
end

How about:

def auto2( hash )
begin
yield
ensure
hash.each do |obj, method|
obj.send method
end
end
end

class Foo
def cleanup
puts “cleanup”
end
end

auto2( Foo.new => :cleanup ) do
puts “in block”
end

robert
···

On Wed, May 21, 2003 at 05:14:06AM +0900, MikkelFJ wrote:

“Mauricio Fernández” batsman.geo@yahoo.com wrote in message
news:20030521055137.GA6046@student.ei.uni-stuttgart.de

It seems you overlooked the fact that it is not implementable in current
Ruby, for it needs Procs to be rebound :slight_smile:

No I didn’t overlook it, but that doesn’t prevent me from reflecting on it
:slight_smile:

Mikkel

i think its actually the GC cleaning up matrix and vector classes (my own
implementation extended in ruby, they just alloc and free using c++
new/delete operators, matching my c++ classes: vector (float[4]} and matrix
float[16];

Can you reveal some of this code ?

also, i’m not doing any marking myself in c, these are pretty primitive c++
classes, and never ref any other ruby objects, so i just implement the free
function ptr for the classes

OK

···

On Sun, 18 May 2003 22:39:17 +0900, Gaffer wrote:


Simon Strandgaard

I meant my method, which is used like this

auto(:var1, :var2, :etc) do

end

ie.

def auto(*vars, &block)
begin
block.rebind(binding).call # rebind not in Ruby!!!
ensure
vars.each { |x| eval “#{x}.cleanup if #{x}” }
end
end

The idea is getting 'var = foo" inside the block to create the local in
the outer scope (binding).

You had better forget about this stupid thing of mine, and stay with the
good, implementable thing :slight_smile:

···

On Wed, May 21, 2003 at 05:56:34PM +0900, Robert Klemme wrote:

It seems you overlooked the fact that it is not implementable in current
Ruby, for it needs Procs to be rebound :slight_smile:

I’m not sure, whether I understand you correctly. The solution below does
work. Why then is it not possible without rebinding procs?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

  1. is qmail as secure as they say?

Depends on what they were saying, but most likely yes.
– Seen on debian-devel

i’m pretty sure i’ve tracked down the cause, this is my first time embedding
ruby and writing classes in c++,

so i didnt know you could override the singleton method new for a class, to
do the Data_Make_Struct,

so i was wrapping an outer ‘husk’ object with a member variable @data which
was pointing to a Data_Wrap_Struct around my c++ object created with new

pretty nasty, and i have no idea how the gc would have interpreted this (as
i did no marking)

now that i’ve deciphered how everything should work, my matrix/vector
classes should be as efficient as they should have always been with ruby

i’ll post back when i have results, cheers

···

-----Original Message-----
From: Simon Strandgaard [mailto:0bz63fz3m1qt3001@sneakemail.com]
Sent: Sunday, May 18, 2003 11:30 PM
To: ruby-talk ML
Subject: Re: ruby garbage collection

On Sun, 18 May 2003 22:39:17 +0900, Gaffer wrote:

i think its actually the GC cleaning up matrix and vector classes (my own
implementation extended in ruby, they just alloc and free using c++
new/delete operators, matching my c++ classes: vector (float[4]} and
matrix
float[16];

Can you reveal some of this code ?

also, i’m not doing any marking myself in c, these are pretty primitive
c++
classes, and never ref any other ruby objects, so i just implement the
free
function ptr for the classes

OK


Simon Strandgaard

Hi,

def auto(*vars, &block)
begin
block.rebind(binding).call # rebind not in Ruby!!!
ensure
vars.each { |x| eval “#{x}.cleanup if #{x}”, block }

···

At Wed, 21 May 2003 18:57:31 +0900, Mauricio Fernández wrote:

end
end


Nobu Nakada

“Mauricio Fernández” batsman.geo@yahoo.com schrieb im Newsbeitrag
news:20030521095729.GA24238@student.ei.uni-stuttgart.de

It seems you overlooked the fact that it is not implementable in
current
Ruby, for it needs Procs to be rebound :slight_smile:

I’m not sure, whether I understand you correctly. The solution below
does
work. Why then is it not possible without rebinding procs?

I meant my method, which is used like this

auto(:var1, :var2, :etc) do

end

ie.

def auto(*vars, &block)
begin
block.rebind(binding).call # rebind not in Ruby!!!
ensure
vars.each { |x| eval “#{x}.cleanup if #{x}” }
end
end

The idea is getting 'var = foo" inside the block to create the local in
the outer scope (binding).

Ok, I see. I distantly thought of that but dropped it since one does not
really need the identifiers. :slight_smile:

You had better forget about this stupid thing of mine, and stay with the
good, implementable thing :slight_smile:

I’ll obediently do that. :-))

Kind regards

robert
···

On Wed, May 21, 2003 at 05:56:34PM +0900, Robert Klemme wrote:

ok, looks like it fixed it

warning! dont try to manually do the pointer wrapping, ruby already does it
for you! heheh :wink:

···

-----Original Message-----
From: Gaffer [mailto:gaffer@gaffer.org]
Sent: Sunday, May 18, 2003 11:48 PM
To: ruby-talk ML
Subject: Re: ruby garbage collection

i’m pretty sure i’ve tracked down the cause, this is my first time embedding
ruby and writing classes in c++,

so i didnt know you could override the singleton method new for a class, to
do the Data_Make_Struct,

so i was wrapping an outer ‘husk’ object with a member variable @data which
was pointing to a Data_Wrap_Struct around my c++ object created with new

pretty nasty, and i have no idea how the gc would have interpreted this (as
i did no marking)

now that i’ve deciphered how everything should work, my matrix/vector
classes should be as efficient as they should have always been with ruby

i’ll post back when i have results, cheers

-----Original Message-----
From: Simon Strandgaard [mailto:0bz63fz3m1qt3001@sneakemail.com]
Sent: Sunday, May 18, 2003 11:30 PM
To: ruby-talk ML
Subject: Re: ruby garbage collection

On Sun, 18 May 2003 22:39:17 +0900, Gaffer wrote:

i think its actually the GC cleaning up matrix and vector classes (my own
implementation extended in ruby, they just alloc and free using c++
new/delete operators, matching my c++ classes: vector (float[4]} and
matrix
float[16];

Can you reveal some of this code ?

also, i’m not doing any marking myself in c, these are pretty primitive
c++
classes, and never ref any other ruby objects, so i just implement the
free
function ptr for the classes

OK


Simon Strandgaard

so i didnt know you could override the singleton method new for a class, to
do the Data_Make_Struct,

Yeah its a bastard :slight_smile:

so i was wrapping an outer ‘husk’ object with a member variable @data which
was pointing to a Data_Wrap_Struct around my c++ object created with new

Yes… My alloc/free looks something like this (ruby-1.8.0)

void Free(Klass *p) {
delete p;
}

VALUE Alloc(VALUE self) {
Klass* p = new Klass();
return Data_Wrap_Struct(self, 0, Free, p);
}

void KlassInit() {
VALUE h = rb_define_class(“Klass”, rb_cObject);
rb_define_alloc_func(h, Alloc);
}

···

On Sun, 18 May 2003 23:48:28 +0900, Gaffer wrote:


Simon Strandgaard

Hi,

def auto(*vars, &block)
begin
block.rebind(binding).call # rebind not in Ruby!!!
ensure
vars.each { |x| eval “#{x}.cleanup if #{x}”, block }

Nice!

For the record:

It works the other way around: instead of trying to define the vars in
the begin…end scope, it destroys the ones inside the proc.

Thanks!

···

On Wed, May 21, 2003 at 09:24:37PM +0900, nobu.nokada@softhome.net wrote:

At Wed, 21 May 2003 18:57:31 +0900, > Mauricio Fernández wrote:

end
end


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

LILO, you’ve got me on my knees!
– David Black, dblack@pilot.njin.net, with apologies to Derek and the
Dominos, and Werner Almsberger

i’ve switched over all my ‘concrete’ classes quite easily, looks like i’ll
have to do some placement new (finally an excuse to use it) tricks for my
more complex classes

but its all working fine now, cheers :slight_smile:

···

-----Original Message-----
From: Simon Strandgaard [mailto:0bz63fz3m1qt3001@sneakemail.com]
Sent: Monday, May 19, 2003 12:10 AM
To: ruby-talk ML
Subject: Re: ruby garbage collection

On Sun, 18 May 2003 23:48:28 +0900, Gaffer wrote:

so i didnt know you could override the singleton method new for a class,
to
do the Data_Make_Struct,

Yeah its a bastard :slight_smile:

so i was wrapping an outer ‘husk’ object with a member variable @data
which
was pointing to a Data_Wrap_Struct around my c++ object created with new

Yes… My alloc/free looks something like this (ruby-1.8.0)

void Free(Klass *p) {
delete p;
}

VALUE Alloc(VALUE self) {
Klass* p = new Klass();
return Data_Wrap_Struct(self, 0, Free, p);
}

void KlassInit() {
VALUE h = rb_define_class(“Klass”, rb_cObject);
rb_define_alloc_func(h, Alloc);
}


Simon Strandgaard

an interesting aside, is there any benefit to using ruby’s ALLOC etc.
functions over new/delete? i notice you dont, cheers

···

-----Original Message-----
From: Simon Strandgaard [mailto:0bz63fz3m1qt3001@sneakemail.com]
Sent: Monday, May 19, 2003 12:10 AM
To: ruby-talk ML
Subject: Re: ruby garbage collection

On Sun, 18 May 2003 23:48:28 +0900, Gaffer wrote:

so i didnt know you could override the singleton method new for a class,
to
do the Data_Make_Struct,

Yeah its a bastard :slight_smile:

so i was wrapping an outer ‘husk’ object with a member variable @data
which
was pointing to a Data_Wrap_Struct around my c++ object created with new

Yes… My alloc/free looks something like this (ruby-1.8.0)

void Free(Klass *p) {
delete p;
}

VALUE Alloc(VALUE self) {
Klass* p = new Klass();
return Data_Wrap_Struct(self, 0, Free, p);
}

void KlassInit() {
VALUE h = rb_define_class(“Klass”, rb_cObject);
rb_define_alloc_func(h, Alloc);
}


Simon Strandgaard

I don’t know the difference: new/delete versus rubys ALLOC.
Can anyone shed some light on this ?

···

On Mon, 19 May 2003 00:15:33 +0900, Gaffer wrote:

an interesting aside, is there any benefit to using ruby’s ALLOC etc.
functions over new/delete? i notice you dont, cheers


Simon Strandgaard