Help in converting Ruby function to C

c
sdk

#1

Hi

The most computationally expensive task in my plugin is a large loop that performs several mathematical operations per cycle. This cycle is contained in an independent function.

I would like to convert this function in C and call it from the main script. I have looked previously at the Ruby SDK and available examples, but honestly I have always abandoned it as it was a bit confusing (at least for my level of knowledge).

Could you recommend an existing plugin where something similar has been done before? Do you think that, at least in principle, the increase in performance would be worth the effort. I have a very basic knowledge of C, but I am completely lost when it comes to build libraries, projects in Xcode etc… The only thing that I have managed to do so far is the basic example in the ‘Programming Ruby’ book.


#2

In my Vertex Tools extension I have this feature “soft-selection” - it calculates a falloff for a given set of vertices. This was extremely slow when initially written in Ruby. I then ported it to a Ruby C extensions and I got more than 100 times speed improvement.

Note that speed improvement is mostly to gain when you don’t have the need to call Ruby in the middle. I would iterate over all Geom::Point3d object and convert to an array of C/C++ structs and then do the computations before I’d convert the results back to Ruby.

There is a Ruby C Extension example for SketchUp on the SketchUp GitHub account. It has Visual Studio and Xcode projects set up which you can use for quick tinkering and experimentation. This let you focus on writing the code instead of setting up the compiler: https://github.com/SketchUp/ruby-c-extension-examples


#3

Thanks Thom. Encouraging. No need to call Ruby in the middle fortunately. Noted.

I tried to use that example with no success. I want to give it another go though.


#4

Errors when building?


#5

More than errors it was not having a clear idea from where to start :smile:

As I said I am not very familiar with C.


#6

Made some progress.

I am using Xcode 7.2.

  1. I load the xcode project file SUEX_HelloWorld.xcodeproj
  2. I fix some warnings
  3. I build.

Unfortunately I receive the error ‘ruby/encoding.h’ file not found. I have tried to add the path of the file in the build settings, but I must be be doing something wrong. During one of my various attempts the building process seemed to work (can’t remember what I did exactly) but then the bundle file was not created.


#7

Can you post a screenshot?

I just tried with Xcode 7.2, and after allowing it to upgrade the project it built fine. This was with the latest commit on the master branch.


#8

Let me reproduce my steps.

  1. Download the latest commit from here.

  2. Open the Xcode project ~/Downloads/ruby-c-extension-examples-master/Hello World/SUEX_HelloWorld.xcodeproj

  3. Edit scheme and remove Ruby 1.8 target. Then Product/Clean

  4. Update to recommended settings

  5. Build product for Running and it succeeds. (It didn’t previously BTW. Probably I was doing something wrong.)

  6. At this point I would expect to have a bundle file, but it is not there

Thanks for your help!


#9

Xcode doesn’t look for the bundle in the correct location for that project. It normally wants to build products with different names. But in order to build Ruby 1.8 and 2.0 with the same name the project was set to output the bundles in different folders. This is set in the bundle target config, but Xcode doesn’t seem to pick this up - I think it’s a long standing bug. But the bundle is generated - just have a poke in the folder.


#10

Thanks Thom. Found it and it works.


#11

The project is due for an update. Hopefully this should be clearer in the future.