Vivek Marakana: Adding Curl C Extension to IOTA Ruby Gem

Original post:
View story at Medium.com

all credits to: Vivek Marakana

 

Why is this a big deal?

Curl processing is important part of the IOTA library as it is required to process trytes to get transaction hash. This functions usually take 4–5ms in JavaScript library and even lower time in Java.

This functions were taking around 400ms to process single transaction tryte in Ruby in the first version. By rewriting it and doing extreme performance improvements, I was able to make it 2x faster which was around 210–230ms. But still it was very large amount of time when the bundle is large or replayed many times.

Problem & Solution

This was the problem in Ruby as well as Python library as both are not that performance efficient in doing data processing. After researching a bit, I found that Python library uses CCurl extension written in C to make it faster. Ruby Gem also supports C extension to run functions at low level are used in numbers of gems like mysql, nokogiri etc.

I used few articles online to get started with the process. http://silverhammermba.github.io/emberb/c/ was a great help. Finally, I finished adding C extension and using it wherever possible. C extensions are supported in few ruby flavours like jruby where the library will fallback to improved version of Ruby Curl.

Performance Effect

After integrating C extension, performance has been greatly improved. It’s almost 70x faster than current Ruby Curl and 145x faster that original implementation.

You can check this improvement by using:

$ git clone https://github.com/vivekmarakana/iota.lib.rb.git
cd iota.lib.rb
bundle
rake
……..
# Running:
….Ruby Curl time: 209.741ms
.C Curl time: 3.043ms
……
Finished in 0.650446s, 16.9115 runs/s, 56.8840 assertions/s.
11 runs, 37 assertions, 0 failures, 0 errors, 0 skips

Other Improvements

Batch fetching of trytes is implemented in IOTA_CLIENT.api.getTrytesInBatches, which will fetch trytes for hashes in batch of 500 inside. From now on, getBundle will internally call this function instead of getTrytes which will allow developers to fetch large bundle without changing full node configuration. You can also use it separately.

Using in ruby project

You can install this gem by adding following line to your Gemfile:

gem ‘iota-ruby’, ‘~> 1.1.0’, require: ‘iota’

and then running bundle install

Code & Support

Code for this library is open sourced under MIT licence at https://github.com/vivekmarakana/iota.lib.rb and you can ask for help in #iota-ruby channel on https://iotatangle.slack.com

 

 




One Reply to “Vivek Marakana: Adding Curl C Extension to IOTA Ruby Gem”

  1. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

Comments are closed.