Extending RGSS (ruby) with C

FenixFyreX

Fire Deity
Veteran
Joined
Mar 1, 2012
Messages
434
Reaction score
310
First Language
English
Primarily Uses
You can check out benchmark-ips here, it's pretty accurate; a lot of Rubyists use it to benchmark things, and as you can see, it's quite popular just on github; it's recommended around the Ruby forums and such as well.


The numbers are there, whether you choose to believe them or not :p The overhead of parsing through Ruby is that slow as compared to a C Extension. Ruby is an interpreted language...this is just something you'll have to accept.


If you feel like benchmark-ips is flawed (which it isn't), then you can always use a different benchmark tool, such as the SES version floating around, on the anagram solver I posted. It'll most likely give similar results :p
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
The overhead of parsing through Ruby is that slow as compared to a C Extension. Ruby is an interpreted language..
I'm talking about your DLL vs SO numbers, not Ruby vs C.

Your first benchmark showed that native C extensions run 20x faster than native C libraries.

This would suggest that if the benchmark is accurately measuring how many iterations the DLL is running, that the DLL is doing a lot of unnecessary things. If it's performing type-checks everytime it does something, then that may explain why it's so much slower. For example, if I tell it to add 1 to 10 in a loop

int sum_upto(int { int sum = 0 for (int i = 0; i < b; i++) { sum++; // but instead of just incrementing by 1, we also error check } return sum;}and everytime it performs an add operation it needs to make sure that ruby isn't going to complain, that's a lot of extra overhead.Am I misunderstanding what the benchmark is showing?
 
Last edited by a moderator:

FenixFyreX

Fire Deity
Veteran
Joined
Mar 1, 2012
Messages
434
Reaction score
310
First Language
English
Primarily Uses
I know; I'm talking about the DLL vs SO as well; have you ever looked at the code that makes up Win32API / DL? It is (mostly) written in Ruby, as well as DL. That is, and always will be, slower than using direct C function calls exposed by a C extension.

And no, my first benchmark showed that native C extensions run 20x faster than calling native C libraries through Ruby and an underlying C extension. It also had no type checking whatsoever. Win32API has internal type checking of it's own that you can't remove, and that is the bottleneck right there.

Think of it this way:

Object.some_c_extension_method# Execution stack: Look up method name in method table, call function pointer# from said method table.# This is pretty much the equivalent of referencing an# instance of Method in an array, executing that method, then returning the result.# Except faster, cause it's done C-side.SomeApiFunc.call(*vars)# Execution stack: Send vars array to be parsed Ruby-side, a string to be iterated# through Ruby-side, vars array is then passed C-side to be interated through and# converted, then the Win32API function is referenced from an internal list C-side,# then it is called C-side, and the result is converted to a Ruby object via a# Ruby-side string prototype, then the result is returned.TL;DR: Win32API and DL have Ruby boilerplate code, which is what is slowing it down. You do not directly call a native C function with Win32API / DL; with a Ruby C Extension method, you do. I think you are confused as to how Win32API and DL works, exactly.Note that both means of accessing C functions have a small amount of overhead of Ruby parsing the mehod call, no matter the means. Excluding that, A C Extension is pretty much equivalent to directly calling a C function. You have them backwards :p

EDIT: And, as a bonus, I can guarantee that writing a C extension that calls any Win32API function from a windows dll will be significantly faster than using Win32API, simply because of said boilerplate code, written in Ruby.

EDIT2: Sorry for the clusterfu** that is this post; I'm dealing with a little one at the moment, she just got home from school. I'll clean it up / better explain in a bit.
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
Perhaps worth noting the differences in using a normal ruby extension compared to an extension used in an rpg maker environment.  ^_^

You can likely explain that better than I :D
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
And no, my first benchmark showed that native C extensions run 20x faster than calling native C libraries through Ruby and an underlying C extension. It also had no type checking whatsoever. Win32API has internal type checking of it's own that you can't remove, and that is the bottleneck right there.
Oh, ok I was under the impression that your benchmark demonstrated how DLL's performed 20x poorer than using an SO since both tests make a single call to each function and for some reason one of them just ran 20x slower (ie: ran so much less iterations per second) to solve the same problem.


So basically when the DLL function is running, win32API is still sticking checking every operation (ie: internal type checking) instead of just letting the DLL finish its work before checking whether the final result is ok for ruby. This would account for the extra overhead per iteration, resulting in the 20x difference per iteration of the DLL function.


EDIT: I see the problem in my logic. I'm still thinking that the benchmark was simply testing how fast the DLL was running, and not taking into consideration all the extra ruby stuff. The term "iteration" was confusing me because I see that the method is being called once, and the only thing that seems to be iterating to me was the actual anagram solving function.
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,049
Members
137,569
Latest member
Shtelsky
Top