Image for Writing Efficient Ruby Code (Digital Short Cut)

Writing Efficient Ruby Code (Digital Short Cut) (1st edition)

See all formats and editions

This short cut focuses on a number of coding patterns that are useful when trying to get maximum speed out of performance-critical sections of Ruby code. Anti-patterns, that is, coding idioms, which should be avoided in performance-sensitive code sections are discussed, including details on how to transform such code to make it more efficient. Most patterns were extracted from Stefan Kaes' work on improving performance of the Rails core and his regular Rails performance consulting work. These patterns are largely non-algorithmic, detailing local code transformations to achieve identical results with slightly different and faster code, as even local code changes can sometimes result in orders of magnitude improvements. Some patterns are useful independent of Rails' implementation language, but some of them are specific to Ruby, or more specifically, the current implementation of Ruby. Converts from other languages, especially from statically typed languages such as Java or C++ may find this material useful, as the performance characteristics of certain operations, like performing a function call or accessing object fields/attributes, are quite different from what you expect.

What This Short Cut Covers

Introduction

Ruby's Interpreter Is Slow

Runtime Complexity of Ruby Language Constructs

Patterns

Instance Variables versus Accessors

Local Variables Are Cheap

Assignments in Expressions

Interpolated Strings

In-Place Updates

Sets versus Arrays

For Loops versus each

Make Decisions at Load Time

Self Modifying Code

Test Most Frequent Case First

Optimize Access to Global Constants

Caching Data in Instance Variables

Caching Data in Class Variables

Coding Variable Caching Efficiently

Initializing Variables with nil

Using .nil?

nil? or empty? versus blank?

Using return

Using returning

Using any?

Block Local Variables

Date Formatting

Temporary Datastructure Constants

File System Access

ObjectSpace.each_object

Unnecessary Block Parameters

Symbol.to_proc

Chained Calls of map

Requiring Files Dynamically

Including Modules versus Opening Classes

About the Author

Read More
Special order line: only available to educational & business accounts. Sign In
£59.94
Product Details
Addison-Wesley Professional
0321540034 / 9780321540034
eBook (Adobe Pdf)
27/04/2021
United States
English
1 pages
Copy: 10%; print: 10%