activesupport-minimizer 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b015df981e76956005ec4dbec6f10808c1fe61a2be2d17395be4bc622ae0541d
4
- data.tar.gz: c569ebbecfa90d6b135966578016eb725e9b705e8bb3896443a87fa096c9c340
3
+ metadata.gz: 31636aeda8a4735600af514ddc5baf5e8fab14e491f893b402f3bf073c89b105
4
+ data.tar.gz: 2281325cd2c192462987066dcef9bb331cd560b2f18e764fdf1a2b9fb586094f
5
5
  SHA512:
6
- metadata.gz: a65151b39365a9eaeefb4bf743edfd103e20f0c9e3cdb9efb38384ed61a4b6f771263df98a8c15e766d00072686fd25fea6abf7187f62b70bce6ec42db751112
7
- data.tar.gz: c9f8740139908120958cec43192c1b241cdb7df945aa2bf01851dc0c05060bd67e1ff8f8b32170c56fcabbe8f5e3b872f01bcbe1c07fe402f00df6acb9de35a3
6
+ metadata.gz: 3b531206671b29e479b83d5c2009ca3126a29479de0e5a5b06afb0fddc8d1a01368b6ed2657a996cf6a155eeee2423518f97e755929e83ce9babb9712b7c888e
7
+ data.tar.gz: 2147719958aacc0b70fd371b9d34bc79167f80873e753bb6399b825e4c0b3087ca368154b59d1c3412987710b7a93037fb1d7d5c1ae4d1e7ee1b51aa74cac229
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0 (2026-10-20)
4
+
5
+ - README: the callback checks come first; unrolled callbacks are experimental (no faster in production, ~20 MB more per worker). No code changes.
6
+
7
+ ## 0.1.2 (2026-10-18)
8
+
9
+ - Rails main: the controller terminator (now `AbstractController::Callbacks::TERMINATOR`, a lambda in 8.1) is recognized again, so a before_action is a plain call followed by `performed?` instead of a lambda passed to the terminator: one block less per callback to compile, one allocation less per call.
10
+ - The part of a chain an around callback wraps is a method of its own, which the around gets as a one-line block, instead of a proc holding the rest of the chain: no callbacks in blocks nested in blocks, and no proc allocated per around per run.
11
+
3
12
  ## 0.1.1 (2026-10-18)
4
13
 
5
14
  - Unrolled callbacks: one method per class and chain instead of one per controller action (`only:`/`except:` become a Set lookup), and classes whose chains are the same share one method body. Fewer methods for the JIT to compile.
data/README.md CHANGED
@@ -1,9 +1,32 @@
1
1
  # activesupport-minimizer
2
2
 
3
- Runs `ActiveSupport::Callbacks` chains as generated methods: controller
4
- `before_action`/`around_action`/`after_action`, model `before_save`,
5
- `after_commit`, validations, `after_initialize`, Active Job callbacks — any
6
- class using `define_callbacks`. Part of [rails-minify](../rails-minify).
3
+ Finds callback problems Rails only reports when the chain runs, and,
4
+ experimentally, runs `ActiveSupport::Callbacks` chains as generated methods.
5
+ Part of [rails-minify](../rails-minify), whose `bin/rails minify:check` runs
6
+ the checks in CI.
7
+
8
+ ## Checks
9
+
10
+ `AsMinimizer::Check`, for any class using `define_callbacks` (controllers,
11
+ models, validations, Active Job):
12
+
13
+ * `callback_targets`: callbacks naming a method that doesn't exist. Rails
14
+ registers callbacks by name and finds out when the chain runs: for a
15
+ controller, a 500 on every action the filter covers.
16
+ * `long_chains(limit: 5)`: chains with more than `limit` of the app's own
17
+ callbacks (Rails' and gems' don't count; controllers per action).
18
+ Callbacks inherited from `exclude` (default `ApplicationController`) don't
19
+ count either; a base class over the limit is reported once, with the
20
+ classes that inherit it.
21
+ * `callback_loops`: model callbacks that call `save`, `update`, `touch`, ...
22
+ on the record they run for, which runs the chain again (found with Prism),
23
+ with the guard if there is one.
24
+
25
+ ## Experimental: unrolled callbacks
26
+
27
+ Off unless set. Faster per call in benchmarks; in production it made pages no
28
+ faster and cost ~20 MB per worker, because each worker generates the methods
29
+ as chains first run (see [Numbers](#numbers)).
7
30
 
8
31
  ```ruby
9
32
  # config/environments/production.rb
@@ -47,7 +70,7 @@ after a halt when the chain says so. Symbols, procs of any arity, objects
47
70
  with `raise_on_missing_callback_actions` on they are checked at runtime
48
71
  instead, as Rails does. A chain that changes gets a new method.
49
72
 
50
- ## Debugging
73
+ ### Debugging
51
74
 
52
75
  Backtraces name the chain and the line in it:
53
76
 
@@ -78,3 +101,9 @@ A controller action behind 9 callbacks: 12.6 µs of callback overhead → 4.0 µ
78
101
  (6.4 → 2.3 µs with YJIT).
79
102
 
80
103
  [Japan Voyage](https://japanvoyage.jp)'s test suite (4317 runs) gives the same results with it on.
104
+
105
+ In production there (Falcon, 6 workers, half of them unrolling, the same
106
+ traffic for an hour, October 2026): no faster, ~20 MB more per worker. That
107
+ was before the app was preloaded; generating the methods at boot, before the
108
+ fork, for the loaded classes would be the next thing to try, and then mostly
109
+ for memory.
@@ -38,6 +38,11 @@ module AsMinimizer
38
38
  # skipped after a halt when the chain says so. For controllers, `only:` and
39
39
  # `except:` are a lookup of action_name in the filter's action set.
40
40
  #
41
+ # The part of the chain an around callback wraps is a method of its own
42
+ # (__as_save_1, ...), which the around gets as a one-line block: the
43
+ # callbacks run in plain methods rather than blocks nested in blocks, and
44
+ # nothing is allocated for an around callback that only yields.
45
+ #
41
46
  # One method per class and chain (not per controller action), and one body
42
47
  # per distinct chain: classes whose chains come out the same (subclasses of
43
48
  # a controller that add no callbacks of their own, ...) get the same method
@@ -48,6 +53,8 @@ module AsMinimizer
48
53
  module Unroller
49
54
  # Filters and conditions that aren't symbols are referenced from here.
50
55
  OBJECTS = []
56
+ # What a nested level returns when the chain halted in it.
57
+ HALTED = Object.new.freeze
51
58
  # [kind, body] => a module holding the generated method; classes with the
52
59
  # same chain define theirs from its UnboundMethod, sharing the body.
53
60
  SHARED = {}
@@ -99,16 +106,16 @@ module AsMinimizer
99
106
  generator = Generator.new(klass, kind, chain)
100
107
  body = generator.source
101
108
  warn_missing(klass, kind, generator.targets)
102
- name = :"__as_#{kind}"
103
109
  shared = SHARED[[kind, body]] ||= begin
104
110
  label = "#{klass.name || "anonymous"}##{kind}"
105
- code = "# #{label} callbacks (and every class whose chain is the same), unrolled by activesupport-minimizer\n" \
106
- "def #{name}\n#{body}\nend\n"
111
+ code = "# #{label} callbacks (and every class whose chain is the same), unrolled by activesupport-minimizer\n#{body}"
107
112
  Module.new.tap { it.module_eval(code, file_for(klass, kind, label, code), 1) }
108
113
  end
109
- klass.send(:define_method, name, shared.instance_method(name))
110
- klass.send(:private, name)
111
- name
114
+ shared.instance_methods(false).each do |name|
115
+ klass.send(:define_method, name, shared.instance_method(name))
116
+ klass.send(:private, name)
117
+ end
118
+ :"__as_#{kind}"
112
119
  rescue Unsupported
113
120
  nil
114
121
  end
@@ -159,13 +166,15 @@ module AsMinimizer
159
166
  @kind = kind
160
167
  @chain = chain
161
168
  @config = chain.config
162
- @lambdas = 0
163
169
  @targets = []
164
170
  end
165
171
 
172
+ # The method definitions: __as_<kind>, and one more per around callback.
166
173
  def source
167
174
  terminator!
168
- "halted = false\nvalue = nil\n#{level(levels)}value"
175
+ @methods = []
176
+ @methods << "def __as_#{@kind}(&)\nhalted = false\nvalue = nil\n#{level(levels)}value\nend\n"
177
+ @methods.join
169
178
  end
170
179
 
171
180
  private
@@ -175,7 +184,7 @@ module AsMinimizer
175
184
  @terminator =
176
185
  if terminator.equal?(ActiveSupport::Callbacks::CallbackChain.const_get(:DEFAULT_TERMINATOR))
177
186
  :abort
178
- elsif terminator.is_a?(Proc) && terminator.source_location&.first&.end_with?("abstract_controller/callbacks.rb")
187
+ elsif performed_terminator?(terminator)
179
188
  :performed
180
189
  else
181
190
  object(terminator)
@@ -186,6 +195,15 @@ module AsMinimizer
186
195
  @inline_actions = !(@klass.respond_to?(:raise_on_missing_callback_actions) && @klass.raise_on_missing_callback_actions)
187
196
  end
188
197
 
198
+ # Rails 8.1's controller terminator is a lambda; main's is TERMINATOR.
199
+ def performed_terminator?(terminator)
200
+ if terminator.is_a?(Proc)
201
+ terminator.source_location&.first&.end_with?("abstract_controller/callbacks.rb")
202
+ else
203
+ defined?(AbstractController::Callbacks::TERMINATOR) && terminator.equal?(AbstractController::Callbacks::TERMINATOR)
204
+ end
205
+ end
206
+
189
207
  # What CallbackChain#compile builds: each around opens a nested level;
190
208
  # befores keep definition order, afters run last-defined first.
191
209
  def levels
@@ -234,16 +252,22 @@ module AsMinimizer
234
252
  check.empty? ? "#{call}\n" : "if #{check.join(" && ")}\n #{call}\nend\n"
235
253
  end
236
254
 
237
- # The rest of the chain goes in a proc: the around callback gets it as
238
- # its block, and it runs directly when the around is skipped.
255
+ # The rest of the chain is a method of its own: the around callback gets
256
+ # a block calling it, and it's called directly when the around is
257
+ # skipped. It returns the chain's value, or HALTED.
239
258
  def around(callback, inner)
259
+ name = "__as_#{@kind}_#{@methods.size}"
260
+ @methods << "def #{name}(halted, &)\nvalue = nil\n#{inner}halted ? ::AsMinimizer::Unroller::HALTED : value\nend\n"
240
261
  cond = conditions(callback)
241
- inner_name = "inner#{@lambdas += 1}"
242
- code = "#{inner_name} = proc do\n#{inner}end\n" # a Proc, as ActiveSupport passes: extra arguments are ignored
243
- return code << "#{inner_name}.()\n" unless cond
244
-
245
- skip = cond == "true" ? "halted" : "halted || !(#{cond})"
246
- code << "if #{skip}\n #{inner_name}.()\nelse\n #{call(callback, callback.filter, inner_name)}\nend\n"
262
+ direct = "inner = #{name}(halted, &)\n"
263
+ code =
264
+ if cond
265
+ skip = cond == "true" ? "halted" : "halted || !(#{cond})"
266
+ "inner = nil\nif #{skip}\n #{direct.chomp}\nelse\n #{call(callback, callback.filter, "{ inner = #{name}(halted, &) }")}\nend\n"
267
+ else
268
+ direct
269
+ end
270
+ code << "if ::AsMinimizer::Unroller::HALTED.equal?(inner)\n halted = true\n value = false\nelse\n value = inner\nend\n"
247
271
  end
248
272
 
249
273
  # Ruby for the callback's :if/:unless, "true" when there are none, nil
@@ -288,18 +312,19 @@ module AsMinimizer
288
312
  end
289
313
  end
290
314
 
315
+ # block: a block literal ("{ ... }") an around callback runs the rest of the chain with.
291
316
  def call(callback, filter, block = nil)
292
317
  case filter
293
- when Symbol then (@targets << [filter, :filter, callback.kind]; "self.#{method_call(filter)}#{"(&#{block})" if block}")
318
+ when Symbol then (@targets << [filter, :filter, callback.kind]; "self.#{method_call(filter)}#{" #{block}" if block}")
294
319
  when Proc
295
320
  case filter.arity
296
321
  when 2
297
322
  raise Unsupported, "a before/after proc with 2 parameters" unless block # ActiveSupport raises ArgumentError here
298
- "instance_exec(self, #{block}, &#{object(filter)})"
323
+ "instance_exec(self, proc #{block}, &#{object(filter)})"
299
324
  when 1, -2 then "instance_exec(self, &#{object(filter)})"
300
325
  else "instance_exec(&#{object(filter)})"
301
326
  end
302
- else "#{object(filter)}.#{scope_method(callback)}(#{["self", ("&#{block}" if block)].compact.join(", ")})"
327
+ else "#{object(filter)}.#{scope_method(callback)}(self)#{" #{block}" if block}"
303
328
  end
304
329
  end
305
330
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AsMinimizer
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesupport-minimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampo Kuokkanen