I wonder if this API allow will foster a big movement to track down all the performance-hungry calls in say, Rails, and help the framework move forward in terms of efficiency. This is really a welcome addition anyway.
Rewritten in longer form, assuming I've made no mistakes, the two versions are equivalent to...
if params[:user_id].present?
if !@owner
@owner = User.find_by_login(params[:user_id])
end
end
vs
if !@owner
if params[:user_id].present?
@owner = User.find_by_login(params[:user_id])
end
end
Checking the member variable truthiness is presumably faster than a dictionary lookup followed by a call to "present?". As @owner is probably frequently/always true after the first call (it will be unless find_by_login returns false/nil/? or another method resets @owner, given the same instance), this leads to fewer dictionary lookups and present? calls, resulting in a performance increase.
EDIT: Well that was an amusing spam of simultaneous answers.
Assignment has higher precedence than if, so the line originally would have evaluated as:
(@owner ||= User.find_by_login(params[:user_id])) if params[:user_id].present?
Thus always testing the if, and then conditionally setting @owner. The new line checks the existence of @owner before evaluating the if by using parens to set precedence.
My theory is that it takes both a certain amount of pressure to create those tools, a certain amount of expertise and a certain amount of tooling available from the language. Due to being old, Perl has all of these and in Devel::NYTProf has a more than excellent profiler that goes beyond line, into statement level. See for example these profiling runs created with it: https://dl.dropboxusercontent.com/u/10190786/ppi_bench.zip