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.
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: GitHub - SketchUp/ruby-c-extension-examples: Ruby C extension examples
I load the xcode project file SUEX_HelloWorld.xcodeproj
I fix some warnings
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.
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.