Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak for ruby 3.2 #882

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bugs Fixed

* [#874](https://github.com/toptal/chewy/pull/874): Fix `chewy:journal:clean` task for ruby 3.x. ([@muk-ai](https://github.com/muk-ai))
* [#882](https://github.com/toptal/chewy/pull/882): Fix memory leak during `chewy:reset` for ruby 3.2 ([@konalegi](https://github.com/konalegi))

## 7.3.0 (2023-04-03)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class ProductsIndex < Chewy::Index

field :name
# simply use crutch-fetched data as a value:
field :category_names, value: ->(product, crutches) { crutches.categories[product.id] }
field :category_names, value: ->(product, crutches) { crutches[:categories][product.id] }
end
```

Expand Down
22 changes: 15 additions & 7 deletions lib/chewy/index/crutch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class Crutches
def initialize(index, collection)
@index = index
@collection = collection
@index._crutches.each_key do |name|
singleton_class.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{name}
@#{name} ||= @index._crutches[:#{name}].call @collection
end
METHOD
end
@crutches_instances = {}
end

def method_missing(name, *, **)
return self[name] if @index._crutches.key?(name)

super
end

def respond_to_missing?(name, include_private = false)
@index._crutches.key?(name) || super
end

def [](name)
@crutches_instances[name] ||= @index._crutches[:"#{name}"].call(@collection)
end
end

Expand Down