Articles tagged 'performance'
-
Ruby performance: #attr_accessor vs. method definition
February 19, 2018
I’ve been digging a lot into Ruby performance lately and this much digging has taken me into some very interesting corners of Rubyland. My latest surprise has been the difference between defining attribute methods via the
attr_accessor
provided language construct (as well asattr_reader
andattr_writer
) vs. defining them yourself (as indef attribute
anddef attribute=
). Here’s what I ran into…I created two simple classes with identical interfaces. The only difference between the two is how we are defining access to set and get the
@value
attribute in each:class TestClassAttrAccessor attr_accessor :value def initialize value @value = value end end class TestClassDefMethod def initialize value @value = value end def
-
Ruby performance: Hash's #has_key? vs. #[] (square brackets)
February 15, 2018
During my time last year developing performance improvements for Rambling Trie, I stumbled into something quite interesting that happens with Ruby’s
Hash
class.A commonly used method from the
Hash
interface is#has_key?
which tells you if theHash
in question contains a particular key. Rambling Trie’s underlying data structure is an n-ary tree backed by aHash
where each key is the letter corresponding to a child and each value is theNode
that corresponds to that letter. As you might imagine,#has_key?
is a common operation called throughout the gem implementation.To my surprise, while running some Ruby benchmarks I noticed that accessing the key with
#[]
and verifying if it wasnil
instead of calling#has_key?
lowered the time it took… -
Ruby performance: Using benchmarks and memory_profiler to chase performance degradations
February 11, 2018
While working on some of the performance improvements for version 1.0.0 of Rambling Trie late in 2016 and early in 2017, I upgraded from Ruby 2.3.3 to Ruby 2.4.0 imagining that the newer version would help me on my mission to make all operations execute a bit faster. To my surprise, I ran into a significant performance degradation after upgrading - specifically, there was a ~25% increase in the time it took to execute a script using a trie with a big-ish word dictionary. This intrigued me enough that I decided to take a closer look.
Benchmarking with Ruby
After some investigation with the help of Ruby’s own
Benchmark
, I realized that while most operations were a bit slower, the main problem was a lot more visible during intensive operations… -
Unit Testing ActiveRecord eager-loading
January 27, 2016
If you’ve worked with relational databases and any ORMs like Java’s Hibernate, .NET’s NHibernate or Rails’
ActiveRecord
in the past, you might be familiar with SELECT N+1 issues. It is a common performance problem in database-dependent applications and, because of this, these ORMs provide a built-in solution to this problem.In
ActiveRecord
,includes
,preload
andeager_load
come to the rescue. Therefore, it is not unusual to find these keywords scattered in different places where your application accesses the database. Hopefully this isn’t a lot of places though - you are using Query Objects, right?An example application
Let’s imagine for a second that we have an application where you can browse restaurants, which in turn have many reviews…
-
New homepage for Rambling Trie
July 28, 2012
This post was originally published in the Rambling Labs Blog on July 28, 2012.
As done with the jQuery Rambling Slider previously on this week, and as I promised on that post as well, the Rambling Trie now has its own homepage on ramblinglabs.com! Here’s the link:
Rambling Trie | A Ruby implementation of the Trie data structure
It features installation and usage steps as well as a contributing page, and includes the badges from various cool GitHub services (more on that later). It also has a brand new logo, inspired on the trie data structure (and other tree data structures) concept.
Go ahead and check it out!
Still pending:
- Examples page for jQuery Rambling Slider
- Examples page for Rambling Trie
- Examples page for Rambling Slider…
-
Rambling Trie 0.4.1 is out!
July 21, 2012
This post was originally published in the Rambling Labs Blog on July 21, 2012.
Version 0.4.1 of the Rambling Trie is here. It has some minor performance improvements over previous versions, changes in file/directoy structure, as well as a new API entry point, other API methods and more documentation.
You can now instance a new trie like this:
trie = Rambling::Trie.create
DEPRECATION WARNING
The old API entry point
Rambling::Trie.new
is now marked as deprecated and will be removed soon. Use the newRambling::Trie.create
method.
Also, you can add words to the trie using
<<
:trie << 'word'
And check if a word is contained in the trie with
include?
:trie.include? 'word'
You can see more documentation available on the project’s repository…
-
Rambling Trie 0.3.3 has been released!
February 13, 2012
This post was originally published in the Rambling Labs Blog on February 13, 2012.
The new version of the
rambling-trie
gem is out! As I said on my previous post, therambling-trie
is an implementation of the Trie data structure in Ruby.Version 0.3.3 has several performance improvements, and I also added the code’s documentation via Yard. Also, since the previous version (0.3.2), the methods
has_branch_for?
andis_word?
have been fixed for the compressed trie.For the uncompressed trie, the
has_branch_for?
andis_word?
methods work very fast. The compressed trie’sis_word?
method is as fast as it’s uncompressed counterpart, but thehas_branch_for?
is a bit slower.I’ve also added some benchmarking reports for each version of the
rambling…