pakiderm 3.0.0 → 3.0.1

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: e8ec2e82bcb28b17ae2f38cd49cd65374c92d5c385de0adc3a19d8727dcf5111
4
- data.tar.gz: 2aeb401bb66c7c00152c17d380025200c841998beda60ea10975b61a6ffd2b75
3
+ metadata.gz: 6ecbb403a6459578fba812d5ebfbe298cc42bab0d8c442f1d26c7f48add8ed17
4
+ data.tar.gz: 45d158f5eaff0afabc579777215394ae84cd61f54b9f440b82c44a847de479e4
5
5
  SHA512:
6
- metadata.gz: 36829cae9492a8209de0709446541828f2d2008cd5122539dc832c28f535628989b3df12fba12624e5681f5b5507283fb65b4d61c6c1d9b8d65d3d1ba138a236
7
- data.tar.gz: 5a2a336b8e8cb59d4ca96361210b808010b6639ae703e40d6e2d8d3ca7bd4885671bd3cb127bb048c4f69a5e4be1f0fe3c4e9eca4ba0c11dcd135f84a852df1f
6
+ metadata.gz: d7f34cca2e806aa6b3975e9ee28b3047d43ef5f388fae875edd39406bc272678e792ff014ff098e97668b4c32e13a3ae76cb84ccf0496a9aa265b8be77931573
7
+ data.tar.gz: fd496d6aacf53d1b6dedb7ad620a9dde54a47f6f69aca59b6ff9672f068a3fdda74788d3f01e13de545b2bf197efceabf23f35c555b5631246a9b57a4b1737db
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## Pakiderm 3.0.1 (2026-12-03)
2
+
3
+ * Set `Pakiderm.cache_prefix` at boot to customize or omit the prefix for newly memoized methods.
4
+
1
5
  ## Pakiderm 3.0.0 (2026-12-03)
2
6
 
3
7
  * Support Ruby 3.3.5+. Put `memoize` directly above a method definition.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Pakiderm [![Gem Version](https://img.shields.io/gem/v/pakiderm)](https://rubygems.org/gems/pakiderm) [![Build Status](https://github.com/ElMassimo/pakiderm/actions/workflows/ruby.yml/badge.svg?branch=main)](https://github.com/ElMassimo/pakiderm/actions/workflows/ruby.yml) [![Test Coverage](https://qlty.sh/gh/ElMassimo/projects/pakiderm/coverage.svg)](https://qlty.sh/gh/ElMassimo/projects/pakiderm) [![Maintainability](https://qlty.sh/gh/ElMassimo/projects/pakiderm/maintainability.svg)](https://qlty.sh/gh/ElMassimo/projects/pakiderm) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
2
2
  =====================
3
3
 
4
- Allows you to unobtrusively memoize methods without parameters. Pakiderm 3 requires Ruby 3.3.5 or newer.
4
+ Allows you to easily memoize methods, with the best performance.
5
5
 
6
6
  ```ruby
7
7
  class Stopwatch
@@ -37,19 +37,33 @@ You can also pass a list of methods after defining them on the same class or mod
37
37
  memoize :complex_calculation, :api_response, :db_query
38
38
  ```
39
39
 
40
- ## Releasing
40
+ ### Configuration ⚙️
41
41
 
42
- Update `lib/pakiderm/version.rb`, `Gemfile.lock`, and `CHANGELOG.md` for the release. Commit and push those changes, then wait for CI to pass on `main`.
42
+ You can configure Pakiderm at boot, before loading your memoized methods:
43
43
 
44
- From a clean, up-to-date `main` with RubyGems push access, run `bin/rake release`. It runs the specs, builds the gem, pushes the `v<version>` tag, and publishes the gem to RubyGems. The tag push triggers the GitHub Release workflow. To check the specs and package without publishing, run `bin/rake spec build` instead.
44
+ ```ruby
45
+ # config/initializers/pakiderm.rb
46
+ Pakiderm.cache_prefix = ""
47
+ ```
48
+
49
+ #### `cache_prefix`
50
+
51
+ _Default:_ `"pakiderm_"`
52
+
53
+ The prefix for memoized instance variables. Set it to `""` to use `@songs` for `songs` if your app reserves that name for memoization. With an empty prefix, methods that use the same instance variable share a cache: don’t memoize both an override and its `super` method.
45
54
 
46
55
  ## Background
47
56
 
48
- Pakiderm 2 used `Module#prepend` to put a wrapper in front of each memoized method. Even a cache hit went through that wrapper and called `instance_variable_defined?` and `instance_variable_get`.
57
+ `memoize` uses Prism to recompile the method in place from:
49
58
 
50
- ### Compiling the cache into the method
59
+ ```ruby
60
+ memoize
61
+ def elapsed_time
62
+ Time.now - @started_at
63
+ end
64
+ ```
51
65
 
52
- In Pakiderm 3, `memoize` marks the next definition. When Ruby calls `method_added`, Pakiderm uses Prism to find the original `def` and recompiles it in place. For the stopwatch above, the result is equivalent to writing this by hand (the actual instance variable name is private to Pakiderm):
66
+ to
53
67
 
54
68
  ```ruby
55
69
  def elapsed_time
@@ -58,15 +72,10 @@ def elapsed_time
58
72
  end
59
73
  ```
60
74
 
61
- The parsing cost is paid when the class loads, not when you call the method. A cache hit reads the instance variable directly, with no wrapper call. You can compare compiled and handwritten readers with `bundle exec ruby --yjit benchmark/readers.rb`.
62
-
63
- The method keeps its visibility and its place in Ruby's lookup order. A module prepended before the definition can still call `super`. A subclass can memoize an override that calls `super`, too: the parent and child store their results separately. No hidden alias is needed. An alias made after memoizing uses the cache; an earlier alias still calls the original method.
75
+ It performs just like writing it by hand, run the benchmarks with `bundle exec ruby --yjit benchmark/readers.rb`.
64
76
 
65
- ### Caveat
77
+ Unlike other techniques, this works well with alias and module prepend.
66
78
 
67
- An override is a new method: annotate it again if you want it memoized. The old `memoize def` and backslash forms still work, but neither is required. For class methods, put `memoize` above `def self.name`, or `extend Pakiderm` inside `class << self`.
79
+ ## License
68
80
 
69
- - Choose a parameterless `def` in a Ruby source file. Pakiderm rejects explicit `return`, operator names, aliases, and generated readers such as `attr_reader`. Don't memoize a result that depends on a caller's block.
70
- - Pakiderm preserves source lines, lexical constants, and frozen-string settings. Files that activate refinements with `using`, dynamic class or module paths around the reader, and `class << self` inside a block with unknown `self` aren't supported.
71
- - Custom method-added hooks must call `super` and can see both definitions. To export a module function, use `module_function :name` **after** memoizing; bare `module_function` mode leaves the module-level copy uncached.
72
- - As with handwritten memoization, an exception is not cached, a cache miss cannot write to a frozen receiver, and concurrent first calls are not synchronized.
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -58,7 +58,12 @@ module Pakiderm
58
58
  raise Error, "unsupported method name: `#{name}`" unless match
59
59
 
60
60
  kind = { "?" => "question", "!" => "bang", "" => "reader" }.fetch(match[2])
61
- ivar = "@pakiderm_#{owner.object_id}_#{kind}_#{match[1]}"
61
+ prefix = Pakiderm.cache_prefix
62
+ ivar = if prefix.empty?
63
+ kind == "reader" ? "@#{match[1]}" : "@#{match[1]}_#{kind}"
64
+ else
65
+ "@#{prefix}#{owner.object_id}_#{kind}_#{match[1]}"
66
+ end
62
67
  guard = "; return #{ivar} if defined?(#{ivar}); #{ivar} = "
63
68
  source = node.slice
64
69
  start = node.location.start_offset
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pakiderm
4
- VERSION = "3.0.0"
4
+ VERSION = "3.0.1"
5
5
  end
data/lib/pakiderm.rb CHANGED
@@ -6,6 +6,19 @@ require_relative "pakiderm/version"
6
6
  module Pakiderm
7
7
  Error = Class.new(StandardError)
8
8
 
9
+ @cache_prefix = "pakiderm_"
10
+
11
+ class << self
12
+ attr_reader :cache_prefix
13
+
14
+ def cache_prefix=(prefix)
15
+ unless prefix.is_a?(String) && /\A(?:[a-zA-Z_]\w*)?\z/.match?(prefix)
16
+ raise ArgumentError, "cache_prefix must be an identifier prefix or an empty string"
17
+ end
18
+ @cache_prefix = prefix.dup.freeze
19
+ end
20
+ end
21
+
9
22
  def memoize(*names)
10
23
  if names.empty?
11
24
  @_pakiderm_pending = true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pakiderm
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini