September 30th, 2024

Eliminating Intermediate Array Allocations

The article highlights Ruby's memory allocation optimization, particularly in tokenizers and array operations, emphasizing that simple literals do not allocate memory and that idiomatic Ruby can be performant.

Read original articleLink Icon
Eliminating Intermediate Array Allocations

The article discusses the optimization of memory allocation in Ruby, particularly in the context of tokenizers and array operations. The author shares insights from a talk given at RailsWorld, focusing on how certain Ruby operations can avoid unnecessary memory allocations. A function is introduced to measure object allocations, demonstrating that simple literals like booleans, nil, symbols, integers, and floats do not allocate memory. The author addresses a common concern regarding the use of array literals in min or max calculations, explaining that Ruby's compiler can optimize these operations to avoid allocating an array on the heap. Instead, it uses stack memory for temporary storage, thus enhancing performance. The article also touches on the importance of understanding the rationale behind coding rules, particularly those enforced by tools like RuboCop, and emphasizes that idiomatic Ruby can be performant. The author concludes by noting that while some methods may not benefit from these optimizations, the overall goal should be to foster understanding of Ruby's performance characteristics.

- Ruby can optimize certain operations to avoid unnecessary memory allocations.

- Simple literals in Ruby do not allocate memory when executed.

- The use of array literals in min or max calculations can be optimized by the Ruby compiler.

- Understanding the reasoning behind coding rules is important for developers.

- Idiomatic Ruby can often be performant without needing to sacrifice code style.

Related

Optimizing JavaScript for Fun and for Profit

Optimizing JavaScript for Fun and for Profit

Optimizing JavaScript code for performance involves benchmarking, avoiding unnecessary work, string comparisons, and diverse object shapes. JavaScript engines optimize based on object shapes, impacting array/object methods and indirection. Creating objects with the same shape improves optimization, cautioning against slower functional programming methods. Costs of indirection like proxy objects and function calls affect performance. Code examples and benchmarks demonstrate optimization variances.

Some Tricks from the Scrapscript Compiler

Some Tricks from the Scrapscript Compiler

The Scrapscript compiler implements optimization tricks like immediate objects, small strings, and variants for better performance. It introduces immediate variants and const heap to enhance efficiency without complexity, seeking suggestions for future improvements.

Malloc() and free() are a bad API (2022)

Malloc() and free() are a bad API (2022)

The post delves into malloc() and free() limitations in C, proposing a new interface with allocate(), deallocate(), and try_expand(). It discusses C++ improvements and emphasizes the significance of a robust API.

Use the Ruby on Rails logger block syntax

Use the Ruby on Rails logger block syntax

Using Ruby on Rails logger block syntax improves performance by reducing object allocations and CPU usage, especially in larger applications, enhancing resource management as traffic and complexity increase.

Small Strings in Rust: smolstr vs. smartstring

Small Strings in Rust: smolstr vs. smartstring

The article explores Rust's small string libraries `smolstr` and `smartstring`, demonstrating JSON parsing, a custom memory allocator, and a reporting subcommand for analyzing memory usage and allocations.

Link Icon 3 comments
By @saagarjha - 7 months
Does Ruby not have a way of monkey-patching builtins? Does the compiler check for this?
By @kragen - 7 months
this is a great look at how to poke around under ruby's blankets! i wish it were so ready-to-hand in every environment