View story at Medium.com
all credits to: Vivek Marakana
Why is this a big deal?
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.
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
….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
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
Code & Support