An assortment of things I encountered in the last week that may be useful in the future.

Benchmarking and Profiling Ruby

This Shopify Engineering post about profiling and benchmarking ruby nicely complements their prior post about performance in ruby on rails, which delves into details about SQL execution, caching, and delayed jobs in rails. It also discusses programming considerations like the cost of metaprogramming, allocations, indirection, and slow algorithms. All of that information is valuable, but knowing where to apply it is even more so. You could spend a bunch of time optimizing some piece of code, but it may not have a significant effect. The profiling and benchmarking post discusses how to figure out what to optimize.

Profiling discerns which pieces of code are causing the most delays, or whether the delay is due to something like garbage collection that isn’t as easy to control. The post discusses several profiling tools and their merits, including the one Shopify prefers. Once the slow parts of the code are identified, optimization efforts can be focused there, to make it more-likely that the effort results in a significant performance gain. When working on these optimizations is when bechmarking is useful. It provides rigorous comparisons between different arrangements or algorithms. This helps you determine whether a change is helpful, and worth any added complexity. The post has several example uses of benchmarking tools, and discusses different ways to use them and why.

Overriding ActiveRecord Association Methods

It’s possible to override methods generated by ActiveRecord associations. ActiveRecord automatically generates reader and writer methods for the associated models, but you may want these to do something slightly different. The methods can be overridden simply by defining new ones, and the original can be accessed using super.

I had occasion to use this when I added another association to the same model and wanted the new association to fallback to the first if it wasn’t populated. Originally, my model looked like this:

class Foo < ActiveRecord::Base
    belongs_to :bar
end

The bar gets used for two different purposes, A and B, and I wanted to separate them and allow a different bar to be used for each. The original association will be used for A, and a new one one will be added and used for B. I added another column, b_bar_id, but this will be null for all existing records, leaving nothing for B to use. An easy way to solve this is for Foo#b_bar to return foo.bar if b_bar_id is null. That way, B just has to call the method and get a bar to use, rather than handling the fallback itself. The fact that the method could be overridden made this easy to accomplish.

class Foo < ActiveRecord::Base
    belongs_to :bar
    belongs_to :b_bar, class_name: 'Bar'

    def b_bar
        super || bar
    end
end

Firefox Screenshots

Lastly, I learned an easy way to take screenshots of web pages in firefox. When responsive design mode is enabled, which can be done by pressing ctrl+shift+M, there is a button to save a screenshot of the page in the upper right corner. As a bonus, being in responsive design mode means the dimensions of the viewport can be manipulated to produce a good quality screenshot. It’s easy to focus on only the relevant part of the page to produce a screenshot that is proportioned well and doesn’t include unnecessary elements like excessive whitespace.