Friday, October 27, 2023

Reduce memory used by ActiveSupport::Callbacks, non-column-backed attributes for enum and more!

Posted by vipulnsward

Hi! This is Vipul bringing you the latest from this week’s changes in the Rails codebase.

Reduce memory used by ActiveSupport::Callbacks

Previously, callbacks which were shared with subclasses would not share between them the procs generated in before/after filters. This was also lazily generated so would cause memory growth after an application is booted.

This change adds reuse of these in before/after blocks halving the memory growth from boot to post-request-serving.

Support non-column-backed attributes for enum

This re-adds support for using enum with non-column-backed attributes while still guarding against typos in enum attribute names. When using enum with a non-column-backed attribute, the attribute must be previously declared with an explicit type.

For example:

class Post < ActiveRecord::Base 
  attribute :topic, :string 
  enum topic: %i[science tech engineering math]

Handle negative numbers in NumberToHumanSizeConverter

This Pull Request adds support for number_to_human_size to handle negative numbers.

# Before
# => "-1234567 Bytes"

# After
# => "-1.18 MB"

Dump schema only for a specific db for rollback/up/down tasks for multiple dbs

Before this change, rails db:migrate:primary (and rails db:up:primary, rails db:down:primary) dumped schema files for all the databases, even though this is not necessary. This change fixes this before to only perform dump for specific database.

Fix casting PG money with comma as radix point

For example, when casting "3,50" works as expected instead of raising an error.

Add BroadcastLogger#deep_dup

This change adds proper support for dup for BroadcastLogger class, where just calling dup wouldn’t suffice before.

Fix StrongParameters#extract_value to include blank values

This change fixes extract_value to also work and retain blank values.

params = "ruby,rails,,web")
params.extract_value(:tags) # => ["ruby", "rails", "", "web"]

This helps to fix a bug where using url_helpers generated invalid urls if a records second composite primary key is empty.

Ignore invalid PRIMARY KEY values in ActiveRecord::Persistence.delete method

When some invalid primary key values are passed to ActiveRecord::Persistence.delete method, Active Record still performed a DELETE query even if it is not needed. For example,

irb(main):001> User.delete([])
  User Delete All (0.6ms)  DELETE FROM "users" WHERE 1=0
=> 0

This change now skips unnecessarily running the query.

Fix rails-ujs auto start() in bundled environments

This change fixes using rails-ujs in bundled environments(with esbuild, etc), where rails-ujs would not auto start properly as its start method was getting called twice.

You can view the whole list of changes here.
We had 34 contributors to the Rails codebase this past week!

Until next time!

Subscribe to get these updates mailed to you.