Improving and Deprecating Ruby's JSON gem APIs

2025-08-09

This post details the reasoning and specifics behind the maintainer's improvements and deprecations to the Ruby JSON gem's APIs. Three key areas are addressed: First, the unsafe `create_additions: true` option is deprecated, with `JSON.unsafe_load` or explicit option passing recommended. Second, the default tolerance for duplicate keys is deprecated, suggesting the use of `allow_duplicate_key: true`. Lastly, while not deprecating `Object#to_json`, a new `JSON::Coder` API is introduced, offering a safer and more flexible JSON serialization method, addressing global behavior and configuration issues. The author emphasizes the need to weigh the costs and benefits of deprecating APIs and highlights that the new APIs enhance security and reduce the risk of errors.

Read more
Development

Unlocking Ruby Ractor Performance: Eliminating Class Variable Contention

2025-05-28

This post delves into a performance bottleneck in Ruby's Ractors when dealing with class instance variables. The global interpreter lock causes significant performance degradation when multiple Ractors concurrently access these variables. The author meticulously analyzes the underlying implementation of class instance variables and proposes a solution using object delegation to eliminate lock contention, resulting in a nearly threefold performance improvement in microbenchmarks. This solution also unexpectedly fixes a bug and performance regression introduced by the new Namespace feature.

Read more
Development

Boosting Ruby Ractor Performance: Tackling the `object_id` Bottleneck

2025-04-27

Ruby's Ractor concurrency model suffers from performance limitations due to global locks. This post dives deep into a performance bottleneck caused by the `object_id` method, stemming from historical design choices and improvements to garbage collection. By optimizing `object_id`'s implementation, storing it directly within objects instead of using a global hash table lookup, the author significantly improves Ractor performance, resulting in a two-fold speed increase in JSON benchmarks. While challenges remain, such as handling special object types, this work represents a crucial step towards making Ractors truly parallel.

Read more
Development

Improving Database Protocols: A Developer Experience Perspective

2025-04-05

This article discusses shortcomings in SQL database client protocols, specifically MySQL and PostgreSQL. The author points out issues with connection management, error recovery, and prepared statements, leading to increased development complexity. For example, mutable connection state makes error recovery difficult, while the session-scoped nature of prepared statements limits their use in connection pools. The author proposes improvements borrowing from the Redis protocol, such as an explicit configuration phase, idempotency keys, and globally scoped prepared statement identifiers. These changes would simplify development workflows and improve the reliability of database clients, resulting in a better developer experience and more user-friendly databases.

Read more
Development protocol

HTTP/2: Why It Doesn't Matter in Ruby HTTP Servers

2025-02-25

This post discusses the relevance of HTTP/2 support in Ruby HTTP servers like Puma. The author argues that while HTTP/2's main advantage – multiplexing for faster page load times – is significant over the internet, it offers little benefit on a LAN. Low latency and long-lived connections within a LAN minimize the impact of TCP slow start. Furthermore, HTTP/2's server push feature proved detrimental and has been superseded by the more elegant 103 Early Hints. The author advocates leaving HTTP/2 handling to load balancers or reverse proxies, simplifying deployment and maintenance for the application server.

Read more
Development Network Performance

The Myth of the IO-Bound Rails App

2025-01-25

It's a common belief that Rails apps are inherently IO-bound, with the database being the primary performance bottleneck, making Ruby performance less critical. This post challenges that notion. While the database is indeed a scaling bottleneck, the author argues that this doesn't mean the application spends most of its time waiting for I/O. Analysis of YJIT performance improvements and common performance issues (like missing database indexes) suggests many Rails apps are actually CPU-bound. The post highlights confusion between CPU starvation and I/O wait, and emphasizes that choosing the right execution model (asynchronous, threaded, or process-based) depends on the app's I/O/CPU ratio. The author calls for attention to Ruby performance and points out opportunities for optimization within Rails itself.

Read more
Development

Optimizing Ruby's JSON: A Tale of Stack Allocation and Inlining

2025-01-02

This blog post, part four in a series on optimizing Ruby's JSON performance, details the author's journey in improving Ruby's JSON serialization speed. Through meticulous micro-benchmarking and profiling, the author explores stack allocation and inlining techniques. By shifting buffer allocation from the heap to the stack and strategically using inlining, significant performance gains are achieved. However, the article highlights the importance of balancing micro-benchmark improvements with real-world application performance, showcasing a case where optimization negatively impacted larger datasets.

Read more
Development

Optimizing Ruby's JSON: Part 1

2024-12-18

This blog post details how the author optimized Ruby's `json` gem to become one of the fastest JSON parsers and generators. Instead of complex techniques, simple optimizations were applied based on profiling, such as avoiding redundant checks, prioritizing cheaper conditions, reducing setup costs, and using lookup tables. These improvements apply to both C and Ruby code. The optimizations significantly boosted the `json` gem's performance, making it competitive with alternatives like `oj`, reducing the need for monkey patching, and addressing stability and compatibility issues associated with `oj`.

Read more