-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce Memory Size of Roo::Excelx::Cell::* classes
One of the factor which determines the memsize of object is total no. of instance variables Reducing no. of instance variables can also reduce memsize. To do so I've done two major things 1. Remove unnecessary instance_variable link 2. Created method attr_reader_with_default i. With help of this method we can remove instance variable which are static accross class e.g. type and in somecases cell_type ii. We can use better default to reduce memory e.g. style Script ``` require 'roo' require 'objspace' coordinate = Roo::Excelx::Coordinate.new 1,1 base_date = Date.new(1899, 12, 30) cells = [] formulas = [nil, "something"]#.reverse styles = [1,2]#.reverse # Changing the sequence of this will result into different result formulas.each do |formula| styles.each do |style| cells << ["formula=#{formula};style=#{style}", Roo::Excelx::Cell::String.new('Sample String',formula, style, nil, coordinate)] cells << ["formula=#{formula};style=#{style}", Roo::Excelx::Cell::Boolean.new('1',formula, style,nil,coordinate)] cells << ["formula=#{formula};style=#{style}", Roo::Excelx::Cell::Number.new('20.25', formula, [:numeric_or_formula, '%.2f'], style, nil, coordinate)] cells << ["formula=#{formula};style=#{style}", Roo::Excelx::Cell::Date.new(5, formula, [:numeric_or_formula, 'yyyy-mm-ddd'], style,nil,base_date,coordinate)] cells << ["formula=#{formula};style=#{style}", Roo::Excelx::Cell::DateTime.new(5.25, formula, [:numeric_or_formula, 'yyyy-mm-ddd hh:mm'], style,nil,base_date,coordinate)] cells << ["formula=#{formula};style=#{style}", Roo::Excelx::Cell::Time.new(0.25, formula, [:numeric_or_formula, 'hh:mm'], style,nil,base_date,coordinate)] cells << ["", Roo::Excelx::Cell::Empty.new(coordinate)] end end cells = cells.collect{|variant,cell| ["#{cell.class}(#{variant})",cell] }.uniq(&:first).sort_by(&:first) cells.each do |text, cell| puts "#{text} => #{ObjectSpace.memsize_of(cell)} (#{cell.instance_variables.length})" end ``` Result on Master ``` Roo::Excelx::Cell::Boolean(formula=;style=1) => 120 (8) Roo::Excelx::Cell::Boolean(formula=;style=2) => 104 (8) Roo::Excelx::Cell::Boolean(formula=something;style=1) => 104 (8) Roo::Excelx::Cell::Boolean(formula=something;style=2) => 104 (8) Roo::Excelx::Cell::Date(formula=;style=1) => 120 (9) Roo::Excelx::Cell::Date(formula=;style=2) => 112 (9) Roo::Excelx::Cell::Date(formula=something;style=1) => 112 (9) Roo::Excelx::Cell::Date(formula=something;style=2) => 112 (9) Roo::Excelx::Cell::DateTime(formula=;style=1) => 120 (9) Roo::Excelx::Cell::DateTime(formula=;style=2) => 112 (9) Roo::Excelx::Cell::DateTime(formula=something;style=1) => 112 (9) Roo::Excelx::Cell::DateTime(formula=something;style=2) => 112 (9) Roo::Excelx::Cell::Empty() => 96 (7) Roo::Excelx::Cell::Number(formula=;style=1) => 120 (9) Roo::Excelx::Cell::Number(formula=;style=2) => 112 (9) Roo::Excelx::Cell::Number(formula=something;style=1) => 112 (9) Roo::Excelx::Cell::Number(formula=something;style=2) => 112 (9) Roo::Excelx::Cell::String(formula=;style=1) => 120 (8) Roo::Excelx::Cell::String(formula=;style=2) => 104 (8) Roo::Excelx::Cell::String(formula=something;style=1) => 104 (8) Roo::Excelx::Cell::String(formula=something;style=2) => 104 (8) Roo::Excelx::Cell::Time(formula=;style=1) => 120 (10) Roo::Excelx::Cell::Time(formula=;style=2) => 120 (10) Roo::Excelx::Cell::Time(formula=something;style=1) => 120 (10) Roo::Excelx::Cell::Time(formula=something;style=2) => 120 (10) ``` Result after this patch ``` Roo::Excelx::Cell::Boolean(formula=;style=1) => 40 (3) Roo::Excelx::Cell::Boolean(formula=;style=2) => 80 (4) Roo::Excelx::Cell::Boolean(formula=something;style=1) => 88 (4) Roo::Excelx::Cell::Boolean(formula=something;style=2) => 80 (5) Roo::Excelx::Cell::Date(formula=;style=1) => 80 (5) Roo::Excelx::Cell::Date(formula=;style=2) => 96 (6) Roo::Excelx::Cell::Date(formula=something;style=1) => 104 (6) Roo::Excelx::Cell::Date(formula=something;style=2) => 96 (7) Roo::Excelx::Cell::DateTime(formula=;style=1) => 80 (5) Roo::Excelx::Cell::DateTime(formula=;style=2) => 96 (6) Roo::Excelx::Cell::DateTime(formula=something;style=1) => 104 (6) Roo::Excelx::Cell::DateTime(formula=something;style=2) => 96 (7) Roo::Excelx::Cell::Empty() => 40 (1) Roo::Excelx::Cell::Number(formula=;style=1) => 80 (5) Roo::Excelx::Cell::Number(formula=;style=2) => 96 (6) Roo::Excelx::Cell::Number(formula=something;style=1) => 104 (6) Roo::Excelx::Cell::Number(formula=something;style=2) => 96 (7) Roo::Excelx::Cell::String(formula=;style=1) => 40 (3) Roo::Excelx::Cell::String(formula=;style=2) => 80 (4) Roo::Excelx::Cell::String(formula=something;style=1) => 88 (4) Roo::Excelx::Cell::String(formula=something;style=2) => 80 (5) Roo::Excelx::Cell::Time(formula=;style=1) => 96 (6) Roo::Excelx::Cell::Time(formula=;style=2) => 104 (7) Roo::Excelx::Cell::Time(formula=something;style=1) => 120 (7) Roo::Excelx::Cell::Time(formula=something;style=2) => 104 (8) ```
- Loading branch information
1 parent
4f9b166
commit ca31e0e
Showing
11 changed files
with
129 additions
and
28 deletions.
There are no files selected for viewing
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roo | ||
module AttrReaderHelper | ||
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 |
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
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'forwardable' | ||
require 'roo/excelx/extractor' | ||
|
||
|
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,72 @@ | ||
require "test_helper" | ||
|
||
class TestAttrReaderDefault < Minitest::Test | ||
def base | ||
Roo::Excelx::Cell::Base | ||
end | ||
|
||
def boolean | ||
Roo::Excelx::Cell::Boolean | ||
end | ||
|
||
def class_date | ||
Roo::Excelx::Cell::Date | ||
end | ||
|
||
def datetime | ||
Roo::Excelx::Cell::DateTime | ||
end | ||
|
||
def empty | ||
Roo::Excelx::Cell::Empty | ||
end | ||
|
||
def number | ||
Roo::Excelx::Cell::Number | ||
end | ||
|
||
def string | ||
Roo::Excelx::Cell::String | ||
end | ||
|
||
def base_date | ||
::Date.new(1899, 12, 30) | ||
end | ||
|
||
def base_timestamp | ||
::Date.new(1899, 12, 30).to_datetime.to_time.to_i | ||
end | ||
|
||
def class_time | ||
Roo::Excelx::Cell::Time | ||
end | ||
|
||
def test_cell_default_values | ||
assert_values base.new(nil, nil, [], 1, nil, nil), default_type: :base, :@default_type => nil, style: 1, :@style => nil | ||
assert_values boolean.new("1", nil, nil, nil, nil), default_type: :boolean, :@default_type => nil, cell_type: :boolean, :@cell_type => nil | ||
assert_values class_date.new("41791", nil, [:numeric_or_formula, "mm-dd-yy"], 6, nil, base_date, nil), default_type: :date, :@default_type => nil | ||
assert_values class_time.new("0.521", nil, [:numeric_or_formula, "hh:mm"], 6, nil, base_timestamp, nil), default_type: :time, :@default_type => nil | ||
assert_values datetime.new("41791.521", nil, [:numeric_or_formula, "mm-dd-yy hh:mm"], 6, nil, base_timestamp, nil), default_type: :datetime, :@default_type => nil | ||
assert_values empty.new(nil), default_type: nil, :@default_type => nil, style: nil, :@style => nil | ||
assert_values number.new("42", nil, ["0"], nil, nil, nil), default_type: :float, :@default_type => nil | ||
assert_values string.new("1", nil, nil, nil, nil), default_type: :string, :@default_type => nil, cell_type: :string, :@cell_type => nil | ||
|
||
assert_values base.new(nil, nil, [], 2, nil, nil), style: 2, :@style => 2 | ||
end | ||
|
||
def assert_values(object, value_hash) | ||
value_hash.each do |attr_name, expected_value| | ||
value = if attr_name.to_s.include?("@") | ||
object.instance_variable_get(attr_name) | ||
else | ||
object.public_send(attr_name) | ||
end | ||
|
||
if expected_value | ||
assert_equal expected_value, value | ||
else | ||
assert_nil value | ||
end | ||
end | ||
end | ||
end |