Module: Audition::Rewriters::Memoization
- Defined in:
- lib/audition/rewriters.rb
Overview
Rewrites singleton-scope memoization, both idioms:
@x ||= expr
return @x if defined?(@x); @x = expr
Preferred strategy is freeze-on-memoize, the pattern Rails
core applies to its own code: the memoization stays exactly
as written and only the memoized value becomes shareable
(.freeze appended; Ractor.make_shareable for containers).
Non-main Ractors may then read the ivar once it has been
computed; the first write must still happen on the main
Ractor, which is a boot-warming concern the static check
reports as an info note. Chosen when the value expression
carries no block (a proxy for one-time side effects) and no
writes exist outside the memo sites.
Otherwise falls back to Ractor-local storage:
@x ||= expr -> Ractor.store_if_absent(:"Klass/@x") { expr }
@x = expr -> Ractor.current[:"Klass/@x"] = expr
@x -> Ractor.current[:"Klass/@x"]
Only when every reference in singleton scope is visible in this file, none sit directly in the class body, and no compound writes exist. Caveat (why this is unsafe): freezing changes value mutability, and each Ractor computes its own copy under store_if_absent.
Defined Under Namespace
Classes: SingletonIvars
Class Method Summary collapse
- .accumulator?(value) ⇒ Boolean
- .blockless?(node) ⇒ Boolean
- .classifier(file) ⇒ Object
- .constructor?(value, classifier) ⇒ Boolean
-
.deletion(source, node) ⇒ Object
Deletes the guard statement together with its line and any blank lines that follow, so the method body does not open with a hole.
-
.freezable?(ops, memos) ⇒ Boolean
Freeze-on-memoize applies when every write is a memo site (a stray write means cache invalidation; frozen values cannot support that) and no value needs a block to build.
-
.freeze_edits(file, memos) ⇒ Object
One edit per memo site: make the memoized value shareable while leaving the memoization intact.
-
.freeze_value(value, kind) ⇒ Object
Plain
.freezeonly where the value is provably a string; everything unproven gets Ractor.make_shareable, which is a no-op for already-shareable values. - .frozen_call?(value) ⇒ Boolean
- .guarded_with_strays?(ops, memos) ⇒ Boolean
-
.memo_sites(ops) ⇒ Object
A memo site is an ||= write, or a plain write paired with a defined? return guard inside the same method.
-
.mutator_receivers(file) ⇒ Object
Receivers of in-place mutator calls anywhere in the file, index writes included; mirrors SourceFile#mutated_constants.
- .orphan_guards?(ops, memos) ⇒ Boolean
-
.parens?(value) ⇒ Boolean
.freezebinds tighter than operators and ternaries, so compound expressions get wrapped; message sends and plain literals do not need it. - .plan(file, findings) ⇒ Object
-
.ractor_edits(file, ops, memos, key) ⇒ Object
Two Ractor-local flavors.
-
.setter_edits(file, ops) ⇒ Object
Config setters (
def self.backend=(value); @backend = value; end) get the Rails try_make_shareable recipe in plain Ruby: shareable values are deeply frozen so reads from any Ractor become legal, unshareable values keep today's behavior through the rescue. -
.store_wrap(source, node, key) ⇒ Object
A single-line value keeps the brace form; a multi-line value becomes a do..end block with the body shifted one level right, so the rewrite stays idiomatic.
- .tail_write?(op) ⇒ Boolean
Class Method Details
.accumulator?(value) ⇒ Boolean
322 323 324 325 |
# File 'lib/audition/rewriters.rb', line 322 def self.accumulator?(value) (value.is_a?(Prism::ArrayNode) || value.is_a?(Prism::HashNode)) && value.elements.empty? end |
.blockless?(node) ⇒ Boolean
423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/audition/rewriters.rb', line 423 def self.blockless?(node) queue = [node] until queue.empty? current = queue.shift if current.is_a?(Prism::BlockNode) || current.is_a?(Prism::LambdaNode) return false end queue.concat(current.child_nodes.compact) end true end |
.classifier(file) ⇒ Object
293 294 295 296 297 |
# File 'lib/audition/rewriters.rb', line 293 def self.classifier(file) Static::LiteralClassifier.new( frozen_string_literal: file.frozen_string_literal? ) end |
.constructor?(value, classifier) ⇒ Boolean
299 300 301 302 303 |
# File 'lib/audition/rewriters.rb', line 299 def self.constructor?(value, classifier) value.is_a?(Prism::CallNode) && %i[new dup clone].include?(value.name) && classifier.classify(value) != :shareable end |
.deletion(source, node) ⇒ Object
Deletes the guard statement together with its line and any blank lines that follow, so the method body does not open with a hole. Falls back to the bare node span when other code shares the line.
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/audition/rewriters.rb', line 584 def self.deletion(source, node) raw = source.dup.force_encoding(Encoding::BINARY) from = node.location.start_offset upto = node.location.end_offset start = from.zero? ? 0 : (raw.rindex("\n", from - 1) || -1) + 1 if raw[start...from].match?(/\A[ \t]*\z/n) newline = raw.index("\n", upto) stop = newline ? newline + 1 : raw.length loop do newline = raw.index("\n", stop) break unless newline break unless raw[stop...newline].match?(/\A[ \t]*\z/n) stop = newline + 1 end Autofix.new(start_offset: start, end_offset: stop, replacement: "", safety: :unsafe) else Autofix.new(start_offset: from, end_offset: upto, replacement: "", safety: :unsafe) end end |
.freezable?(ops, memos) ⇒ Boolean
Freeze-on-memoize applies when every write is a memo site (a stray write means cache invalidation; frozen values cannot support that) and no value needs a block to build.
415 416 417 418 419 420 421 |
# File 'lib/audition/rewriters.rb', line 415 def self.freezable?(ops, memos) memo_ops = memos.map { |memo| memo[:op] } writes = ops.select { |op| op[:kind] == :write } return false unless (writes - memo_ops).empty? memos.all? { |memo| blockless?(memo[:op][:node].value) } end |
.freeze_edits(file, memos) ⇒ Object
One edit per memo site: make the memoized value shareable while leaving the memoization intact. Guards, sibling reads, and method shapes stay untouched.
439 440 441 442 443 444 445 |
# File 'lib/audition/rewriters.rb', line 439 def self.freeze_edits(file, memos) kinds = classifier(file) memos.filter_map do |memo| value = memo[:op][:node].value freeze_value(value, kinds.classify(value)) end end |
.freeze_value(value, kind) ⇒ Object
Plain .freeze only where the value is provably a string;
everything unproven gets Ractor.make_shareable, which is a
no-op for already-shareable values. This matters for
memoized classes (multi_json memoizes adapter classes):
.freeze on a Class freezes the class object and later
ivar writes on it raise FrozenError.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/audition/rewriters.rb', line 453 def self.freeze_value(value, kind) return nil if kind == :shareable # Freezing or wrapping a sync primitive raises; leave the # finding in place for a human. return nil if kind == :sync_primitive return nil if frozen_call?(value) slice = value.location.slice replacement = if kind == :mutable_string parens?(value) ? "(#{slice}).freeze" : "#{slice}.freeze" else "Ractor.make_shareable(#{slice})" end Autofix.new( start_offset: value.location.start_offset, end_offset: value.location.end_offset, replacement: replacement, safety: :unsafe ) end |
.frozen_call?(value) ⇒ Boolean
475 476 477 478 479 |
# File 'lib/audition/rewriters.rb', line 475 def self.frozen_call?(value) value.is_a?(Prism::CallNode) && value.name == :freeze && value.receiver && value.arguments.nil? end |
.guarded_with_strays?(ops, memos) ⇒ Boolean
392 393 394 395 396 397 398 399 |
# File 'lib/audition/rewriters.rb', line 392 def self.guarded_with_strays?(ops, memos) return false if memos.none? { |memo| memo[:guard] } memo_ops = memos.map { |memo| memo[:op] } ops.any? do |op| op[:kind] == :write && !memo_ops.include?(op) end end |
.memo_sites(ops) ⇒ Object
A memo site is an ||= write, or a plain write paired with a defined? return guard inside the same method. A guarded method with more than one write is nobody's memoization; such groups are dropped by the orphan check below.
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/audition/rewriters.rb', line 309 def self.memo_sites(ops) guards = ops.select { |op| op[:kind] == :guard } ops.filter_map do |op| case op[:kind] when :or_write {op: op, guard: nil} when :write guard = guards.find { |g| g[:def_id] == op[:def_id] } {op: op, guard: guard} if guard end end end |
.mutator_receivers(file) ⇒ Object
Receivers of in-place mutator calls anywhere in the file, index writes included; mirrors SourceFile#mutated_constants.
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/audition/rewriters.rb', line 373 def self.mutator_receivers(file) receivers = [] queue = [file.root] until queue.empty? node = queue.shift queue.concat(node.child_nodes.compact) mutator = (node.is_a?(Prism::CallNode) && Static::SourceFile::CONST_MUTATORS .include?(node.name)) || Static::SourceFile::INDEX_WRITES .any? { |type| node.is_a?(type) } next unless mutator && node.receiver receivers << node.receiver end receivers end |
.orphan_guards?(ops, memos) ⇒ Boolean
401 402 403 404 405 406 407 408 409 410 |
# File 'lib/audition/rewriters.rb', line 401 def self.orphan_guards?(ops, memos) used = memos.filter_map { |m| m[:guard] } guards = ops.select { |op| op[:kind] == :guard } return true if guards.size != used.size writes = ops.select { |op| op[:kind] == :write } guards.any? do |guard| writes.count { |w| w[:def_id] == guard[:def_id] } != 1 end end |
.parens?(value) ⇒ Boolean
.freeze binds tighter than operators and ternaries, so
compound expressions get wrapped; message sends and plain
literals do not need it.
484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/audition/rewriters.rb', line 484 def self.parens?(value) case value when Prism::CallNode !value.name.to_s.match?(/\A[a-z_]/i) when Prism::StringNode, Prism::InterpolatedStringNode, Prism::ArrayNode, Prism::HashNode, Prism::ConstantReadNode, Prism::ConstantPathNode false else true end end |
.plan(file, findings) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/audition/rewriters.rb', line 215 def self.plan(file, findings) return [] if findings.none? { |f| f.check == "class-level-state" } collector = SingletonIvars.new collector.visit(file.root) bundles = [] collector.groups.each do |(namespace, name), ops| next if namespace.empty? kinds = ops.map { |op| op[:kind] } next if kinds.include?(:other) next if ops.any? { |op| op[:body] } memos = memo_sites(ops) if memos.empty? bundles << Rewriters.bundle(ops, setter_edits(file, ops)) next end next if orphan_guards?(ops, memos) next if guarded_with_strays?(ops, memos) # `@x ||= {}` is a lazily-built accumulator, mutated # through the accessor after memoization (jwt's # algorithm registry). Freezing it breaks registration # and Ractor-local copies would leave other Ractors an # empty registry; the copy-on-write refactor is human # work, so no edit is offered. Constructor memos (`new`, # `Set.new`, `.dup`) are the same family: liquid's # filter set and money's bank singleton both broke under # freezing, and per-Ractor copies hide main-Ractor # registrations. next if memos.any? { |m| accumulator?(m[:op][:node].value) } next if memos.any? do |m| constructor?(m[:op][:node].value, classifier(file)) end if freezable?(ops, memos) bundles << Rewriters.bundle(ops, freeze_edits(file, memos)) else # Ractor-local slots are keyed by lexical owner; on a # class the ivar is per-subclass (faraday's # DEFAULT_OPTIONS), and one shared key would merge # every subclass's state. Modules cannot be # subclassed, so only module-owned state converts. next if ops.any? { |op| op[:class_owner] } # Deleting the defined? guard sends every call through # the statements after the write, so the warm path # only keeps returning the memo when the guarded write # ends its def body (or a bare read of the same ivar # does). next unless memos.all? do |m| m[:guard].nil? || tail_write?(m[:op]) end key = %(:"#{namespace}/#{name}") bundles << Rewriters.bundle( ops, ractor_edits(file, ops, memos, key) ) end end bundles.compact end |
.ractor_edits(file, ops, memos, key) ⇒ Object
Two Ractor-local flavors. With stray writes present (cache
invalidation, @x = nil), memo sites become
Ractor.current[key] ||= expr: it recomputes after a nil
reset exactly like the original ||=, where
store_if_absent would treat the stored nil as present and
never recompute (this broke i18n's reserved_keys_pattern).
Without strays, store_if_absent keeps its atomic lazy
init. Guard-idiom groups with strays are skipped entirely
in plan: the defined? guard caches nil deliberately and
neither flavor reproduces that alongside invalidation.
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/audition/rewriters.rb', line 507 def self.ractor_edits(file, ops, memos, key) guarded = memos.filter_map { |m| m[:op] if m[:guard] } memo_ops = memos.map { |memo| memo[:op] } strays = ops.any? do |op| op[:kind] == :write && !memo_ops.include?(op) end edits = memos.filter_map do |memo| deletion(file.source, memo[:guard][:node]) if memo[:guard] end ops.each do |op| node = op[:node] edits << if op[:kind] == :or_write || guarded.include?(op) replacement = if strays value = node.value.location.slice "Ractor.current[#{key}] ||= #{value}" else store_wrap(file.source, node, key) end Autofix.new( start_offset: node.location.start_offset, end_offset: node.location.end_offset, replacement: replacement, safety: :unsafe ) elsif op[:kind] == :write Autofix.new( start_offset: node.name_loc.start_offset, end_offset: node.name_loc.end_offset, replacement: "Ractor.current[#{key}]", safety: :unsafe ) else Autofix.new( start_offset: node.location.start_offset, end_offset: node.location.end_offset, replacement: "Ractor.current[#{key}]", safety: :unsafe ) end end edits end |
.setter_edits(file, ops) ⇒ Object
Config setters (def self.backend=(value); @backend = value; end) get the Rails try_make_shareable recipe in
plain Ruby: shareable values are deeply frozen so reads
from any Ractor become legal, unshareable values keep
today's behavior through the rescue. Only bare local reads
are wrapped; computed values are left for a human.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/audition/rewriters.rb', line 333 def self.setter_edits(file, ops) receivers = nil ops.filter_map do |op| next unless op[:kind] == :write # Only genuine setters: a plain method restoring a saved # local (sinatra's route conditions) and operator defs # ([]=) must stay untouched. setter = op[:def_name].to_s next unless setter.match?(/\A\w+=\z/) value = op[:node].value next unless value.is_a?(Prism::LocalVariableReadNode) # A value the file mutates in place through the reader # accessor (`def self.set(k, v); options[k] = v; end`) # or through a direct read of the ivar must never be # frozen: the mutation would raise FrozenError. receivers ||= mutator_receivers(file) reader = setter.delete_suffix("=").to_sym mutated = receivers.any? do |receiver| (receiver.is_a?(Prism::CallNode) && receiver.name == reader) || (receiver.is_a?(Prism::InstanceVariableReadNode) && receiver.name == op[:node].name) end next if mutated local = value.name Autofix.new( start_offset: value.location.start_offset, end_offset: value.location.end_offset, replacement: "(Ractor.make_shareable(#{local}) rescue #{local})", safety: :unsafe ) end end |
.store_wrap(source, node, key) ⇒ Object
A single-line value keeps the brace form; a multi-line value becomes a do..end block with the body shifted one level right, so the rewrite stays idiomatic. When the write shares its line with other code, layout cannot be inferred and the brace form is kept as-is.
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/audition/rewriters.rb', line 557 def self.store_wrap(source, node, key) value = node.value.location.slice call = "Ractor.store_if_absent(#{key})" return "#{call} { #{value} }" unless value.include?("\n") raw = source.dup.force_encoding(Encoding::BINARY) from = node.location.start_offset start = from.zero? ? 0 : (raw.rindex("\n", from - 1) || -1) + 1 indent = raw[start...from].force_encoding(source.encoding) return "#{call} { #{value} }" unless indent.match?(/\A[ \t]*\z/) body = value.lines.map.with_index do |line, index| if index.zero? "#{indent} #{line}" elsif line.match?(/\A\s*\z/) line else " #{line}" end end.join "#{call} do\n#{body}\n#{indent}end" end |
.tail_write?(op) ⇒ Boolean
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/audition/rewriters.rb', line 277 def self.tail_write?(op) body = op[:def_node]&.body return false unless body.is_a?(Prism::StatementsNode) statements = body.body index = statements.index { |s| s.equal?(op[:node]) } return false unless index rest = statements[(index + 1)..] return true if rest.empty? rest.size == 1 && rest[0].is_a?(Prism::InstanceVariableReadNode) && rest[0].name == op[:node].name end |