PERF: Fix memory leak
We used to leak some memory every time you cook a post see: https://github.com/cowboyd/therubyracer/pull/336
This commit is contained in:
parent
3d0c03bfb0
commit
d390603274
|
@ -0,0 +1,33 @@
|
|||
## TODO: DELETE ME WHEN https://github.com/cowboyd/therubyracer/pull/336
|
||||
# is upstreamed and released
|
||||
#
|
||||
module V8
|
||||
module Weak
|
||||
class WeakValueMap
|
||||
def initialize
|
||||
@values = {}
|
||||
end
|
||||
|
||||
def [](key)
|
||||
if ref = @values[key]
|
||||
ref.object
|
||||
end
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
ref = V8::Weak::Ref.new(value)
|
||||
ObjectSpace.define_finalizer(value, self.class.ensure_cleanup(@values, key, ref))
|
||||
|
||||
@values[key] = ref
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.ensure_cleanup(values,key,ref)
|
||||
proc {
|
||||
values.delete(key) if values[key] == ref
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue