Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > class A::B verses module A ; class B

Reply
Thread Tools

class A::B verses module A ; class B

 
 
daz
Guest
Posts: n/a
 
      06-05-2004

Joel VanderWerf wrote:
> daz wrote:
> > Austin McDonald wrote:
> >
> >>1) Do the functions I need to use have to be implemented in C as well,
> >>or can I access their "ruby version" using some C functions? What C
> >>functions do I need to use to do this?
> >>

> >
> > Define an empty class (if its name is known) in the Init_foo part of
> > your extension - look at any other extension to get clues (rb_define_class).
> >
> > Reopen your class (looks the same as a class definition) and define
> > your methods inside it (all in Ruby):
> >
> > class Foo
> > def a
> > # whatever
> > end
> >
> > def b
> > end
> > end
> >
> > Call them from C using the rb_funcall family.

>
> This isn't going to help much, if at all, with the speed of these calls,
> since it still goes thru the usual method lookup. Maybe that's ok, if
> the bottleneck is not these calls, but the code around them.
>


I should have said that. To gain any speed benefit from the extension,
you'd have to cut the intensive bits out of Ruby code and write them in C.

You might use rb_define_method() / rb_define_singleton_method() when
building your class instead of creating an empty class.

More help on the Ruby API is in README.EXT in the source distro.

-----

The profile library (described in Programming Ruby under "Standard Libraries")
can help you to track down where the bottlenecks are in your Ruby script and
you should be able to find ways of reducing the need for C.

require 'profile' # at the top of your script

With experience, you'll find ways to avoid making Ruby do unnecessary,
time-consuming work like object duplication.


daz



 
Reply With Quote
 
 
 
 
Austin McDonald
Guest
Posts: n/a
 
      06-06-2004
Thanks for all the help with my last set of questions.

I'm writing a function in my C extension where I copy a variable from my
object structure to a temporary location. Then, I put a new instance in
the new location. However, Ruby keeps garbage collecting the new
variable. I fixed this error by disabling GC for awhile, then
re-enabling it when I'm done. Is there a nicer way to do that?

Code snippet:

static VALUE
pop_reproduce(VALUE self, VALUE payoffs) {
rb_gc_disable();
Population* p;
Data_Get_Struct(self, Population, p);

VALUE oldArray = p->popArray;
p->popArray = rb_ary_new2(p->size);

/*...do some stuff...*/

rb_gc_enable();
return self;
}

In this code snippet, p->popArray is the one getting GC'd. My mark
function only does this:

void pop_mark(VALUE self) {
Population* p;
Data_Get_Struct(self, Population, p);
rb_gc_mark(p->popArray);
}

It's possible that popArray isn't what is getting GC'ed; did I write the
mark function wrong? Here's where I register the mark function:

VALUE
pop_new(VALUE class, VALUE size, VALUE tagsize, VALUE strategysize) {
Population* p = ALLOC(Population);
/* do some stuff to the struct*/
VALUE obj = Data_Wrap_Struct(class, pop_mark, free, p);
VALUE argv[3];
argv[0]=size;
argv[1]=tagsize;
argv[2]=strategysize;
rb_obj_call_init(obj, 3, argv);
return obj;
}

Anyway, I realize that's a lot of code to paste... I'm just wondering if
there's any way to fix this short of actually disabling the GC.

Thanks,
Austin McDonald


 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      06-06-2004
>>>>> "A" == Austin McDonald <(E-Mail Removed)> writes:

A> In this code snippet, p->popArray is the one getting GC'd. My mark
A> function only does this:

Your mark function is wrong, you must write it

A> void pop_mark(VALUE self) {
A> Population* p;
A> Data_Get_Struct(self, Population, p);
A> rb_gc_mark(p->popArray);
A> }

void pop_mark(Population *)
{
rb_gc_mark(p->popArray);
}


A> VALUE
A> pop_new(VALUE class, VALUE size, VALUE tagsize, VALUE strategysize) {

it's best if you use the scheme allocate/initialize rather than new

A> Population* p = ALLOC(Population);
A> /* do some stuff to the struct*/
A> VALUE obj = Data_Wrap_Struct(class, pop_mark, free, p);

best to use Data_Make_Struct

obj = Data_Make_Struct(class, Population, pop_mark, free, p);

the advantage is that p will be filled with zero, it's important for the
GC (try to avoid the variable name `class')


A> VALUE argv[3];



Guy Decoux



 
Reply With Quote
 
Austin McDonald
Guest
Posts: n/a
 
      06-06-2004
Thanks; sorry about the neophyte question.

Austin

ts wrote:

>>>>>>"A" == Austin McDonald <(E-Mail Removed)> writes:
>>>>>>
>>>>>>

>
>A> In this code snippet, p->popArray is the one getting GC'd. My mark
>A> function only does this:
>
> Your mark function is wrong, you must write it
>
>A> void pop_mark(VALUE self) {
>A> Population* p;
>A> Data_Get_Struct(self, Population, p);
>A> rb_gc_mark(p->popArray);
>A> }
>
> void pop_mark(Population *)
> {
> rb_gc_mark(p->popArray);
> }
>
>
>A> VALUE
>A> pop_new(VALUE class, VALUE size, VALUE tagsize, VALUE strategysize) {
>
> it's best if you use the scheme allocate/initialize rather than new
>
>A> Population* p = ALLOC(Population);
>A> /* do some stuff to the struct*/
>A> VALUE obj = Data_Wrap_Struct(class, pop_mark, free, p);
>
> best to use Data_Make_Struct
>
> obj = Data_Make_Struct(class, Population, pop_mark, free, p);
>
> the advantage is that p will be filled with zero, it's important for the
> GC (try to avoid the variable name `class')
>
>
>A> VALUE argv[3];
>
>
>
>Guy Decoux
>
>
>
>
>



 
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
Design question: class methods verses initialize Jason Lillywhite Ruby 2 01-22-2009 09:59 PM
Data Source in ASP.NET 2.0 Verses System.Forms Hoffmania ASP .Net 1 03-29-2006 08:04 AM
Standard verses extended rule Mark St Laurent Cisco 1 02-18-2005 09:58 AM
Database Table Verses DropDownList Leon ASP .Net 4 10-22-2004 02:22 PM
visual studio .net 2003 verses visual studio .net 2002 wh ASP .Net 2 01-16-2004 04:54 PM



Advertisments