forked from roo-rb/roo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce GC'able cache for Roo::Excelx::Extractor#doc (roo-rb#456)
Memory Profiling script: ``` MemoryProfiler.report{Roo::Excelx.new('test/files/Bibelbund.xlsx').tap{|x|(2..x.last_row).each{|i| x.row(i)}}}.pretty_print ``` Benchmark Script: ``` puts Benchmark.measure{ Roo::Excelx.new('test/files/Bibelbund.xlsx').tap{|x|(2..x.last_row).each{|i| x.row(i)}} } ``` Result on Master: ``` Total allocated: 37131810 bytes (517026 objects) Total retained: 5562913 bytes (103010 objects) allocated memory by gem ----------------------------------- 19288066 roo/lib 11049821 nokogiri-1.8.4 6792403 rubyzip-1.2.2 1304 tmpdir 216 other retained memory by gem ----------------------------------- 5560934 roo/lib 782 rubyzip-1.2.2 725 nokogiri-1.8.4 296 tmpdir 176 other 0.720000 0.020000 0.740000 ( 0.733750) ``` Result after patch: ``` Total allocated: 34561842 bytes (504998 objects) Total retained: 5563553 bytes (103026 objects) allocated memory by gem ----------------------------------- 19254338 roo/lib 8513101 nokogiri-1.8.4 6792403 rubyzip-1.2.2 1304 tmpdir 320 weakref 216 other 160 ref-2.0.0 retained memory by gem ----------------------------------- 5561094 roo/lib 782 rubyzip-1.2.2 725 nokogiri-1.8.4 320 weakref 296 tmpdir 176 other 160 ref-2.0.0 0.610000 0.010000 0.620000 ( 0.618642) ```
- Loading branch information
1 parent
49af97c
commit f1f0259
Showing
5 changed files
with
61 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roo | ||
module Helpers | ||
module DefaultAttrReader | ||
def attr_reader_with_default(attr_hash) | ||
attr_hash.each do |attr_name, default_value| | ||
instance_variable = :"@#{attr_name}" | ||
define_method attr_name do | ||
if instance_variable_defined? instance_variable | ||
instance_variable_get instance_variable | ||
else | ||
default_value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require "weakref" | ||
|
||
module Roo | ||
module Helpers | ||
module WeakInstanceCache | ||
private | ||
|
||
def instance_cache(key) | ||
object = nil | ||
|
||
if (ref = instance_variable_get(key)) && ref.weakref_alive? | ||
begin | ||
object = ref.__getobj__ | ||
rescue => e | ||
unless (defined?(::WeakRef::RefError) && e.is_a?(::WeakRef::RefError)) || (defined?(RefError) && e.is_a?(RefError)) | ||
raise e | ||
end | ||
end | ||
end | ||
|
||
unless object | ||
object = yield | ||
instance_variable_set(key, WeakRef.new(object)) | ||
end | ||
|
||
object | ||
end | ||
end | ||
end | ||
end |