Skip to content

Commit f44a9e2

Browse files
committed
Inherit Roo::Excelx::Coordinate from Array
Profiling Script ``` MemoryProfiler.report{ Roo::Excelx.new(file_name).tap{|x|(2..x.last_row).each{|i| x.row(i)}} }.pretty_print ``` Master: ``` Total allocated: 34559100 bytes (504994 objects) Total retained: 5563403 bytes (103022 objects) allocated memory by gem ----------------------------------- 19254338 roo/lib 8509100 nokogiri-1.8.4 6793822 rubyzip-1.2.2 1304 tmpdir 320 weakref 216 other allocated objects by gem ----------------------------------- 358381 roo/lib 145846 nokogiri-1.8.4 735 rubyzip-1.2.2 22 tmpdir 8 weakref 2 other retained memory by gem ----------------------------------- 5561094 roo/lib 792 rubyzip-1.2.2 725 nokogiri-1.8.4 320 weakref 296 tmpdir 176 other retained objects by gem ----------------------------------- 102989 roo/lib 14 nokogiri-1.8.4 8 weakref 7 rubyzip-1.2.2 3 tmpdir 1 other ``` After Patch: ``` Total allocated: 33439850 bytes (477013 objects) Total retained: 4444153 bytes (75041 objects) allocated memory by gem ----------------------------------- 18135098 roo/lib 8509100 nokogiri-1.8.4 6793812 rubyzip-1.2.2 1304 tmpdir 320 weakref 216 other allocated objects by gem ----------------------------------- 330400 roo/lib 145846 nokogiri-1.8.4 735 rubyzip-1.2.2 22 tmpdir 8 weakref 2 other retained memory by gem ----------------------------------- 4441854 roo/lib 782 rubyzip-1.2.2 725 nokogiri-1.8.4 320 weakref 296 tmpdir 176 other retained objects by gem ----------------------------------- 75008 roo/lib 14 nokogiri-1.8.4 8 weakref 7 rubyzip-1.2.2 3 tmpdir 1 other ```
1 parent bd0b0f8 commit f44a9e2

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

lib/roo/excelx/coordinate.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
module Roo
22
class Excelx
3-
class Coordinate
4-
attr_accessor :row, :column
3+
class Coordinate < ::Array
54

65
def initialize(row, column)
7-
@row = row
8-
@column = column
6+
super() << row << column
7+
freeze
98
end
109

11-
def to_a
12-
@array ||= [row, column].freeze
10+
def row
11+
self[0]
12+
end
13+
14+
def column
15+
self[1]
1316
end
1417
end
1518
end

lib/roo/excelx/sheet_doc.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def each_cell(row_xml)
4444
# If you're sure you're not going to need this hyperlinks you can discard it
4545
coordinate = ::Roo::Utils.extract_coordinate(cell_element[COMMON_STRINGS[:r]])
4646
hyperlinks = unless @options[:no_hyperlinks]
47-
key = coordinate.to_a
48-
hyperlinks(@relationships)[key]
47+
hyperlinks(@relationships)[coordinate]
4948
end
5049

5150
yield cell_from_xml(cell_element, hyperlinks, coordinate)
@@ -200,8 +199,7 @@ def extract_cells(relationships)
200199
extracted_cells = {}
201200
doc.xpath('/worksheet/sheetData/row/c').each do |cell_xml|
202201
coordinate = ::Roo::Utils.extract_coordinate(cell_xml[COMMON_STRINGS[:r]])
203-
key = coordinate.to_a
204-
extracted_cells[key] = cell_from_xml(cell_xml, hyperlinks(relationships)[key], coordinate)
202+
extracted_cells[coordinate] = cell_from_xml(cell_xml, hyperlinks(relationships)[coordinate], coordinate)
205203
end
206204

207205
expand_merged_ranges(extracted_cells) if @options[:expand_merged_ranges]

lib/roo/utils.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Utils
44

55
LETTERS = ('A'..'Z').to_a
66

7-
def extract_coord(s, klass = Excelx::Coordinate)
7+
def extract_coordinate(s)
88
num = letter_num = 0
99
num_only = false
1010

@@ -22,20 +22,16 @@ def extract_coord(s, klass = Excelx::Coordinate)
2222
end
2323
fail ArgumentError if letter_num == 0 || !num_only
2424

25-
if klass == Array
26-
[num, letter_num]
27-
else
28-
klass.new(num, letter_num)
29-
end
25+
Excelx::Coordinate.new(num, letter_num)
3026
end
3127

32-
alias_method :extract_coordinate, :extract_coord
28+
alias_method :ref_to_key, :extract_coordinate
3329

3430
def split_coordinate(str)
35-
extract_coord(str, Array)
31+
warn "[DEPRECATION] `Roo::Utils.split_coordinate` is deprecated. Please use `Roo::Utils.extract_coordinate` instead."
32+
extract_coordinate(str)
3633
end
3734

38-
alias_method :ref_to_key, :split_coordinate
3935

4036

4137
def split_coord(str)
@@ -72,8 +68,8 @@ def num_cells_in_range(str)
7268
cells = str.split(':')
7369
return 1 if cells.count == 1
7470
raise ArgumentError.new("invalid range string: #{str}. Supported range format 'A1:B2'") if cells.count != 2
75-
x1, y1 = split_coordinate(cells[0])
76-
x2, y2 = split_coordinate(cells[1])
71+
x1, y1 = extract_coordinate(cells[0])
72+
x2, y2 = extract_coordinate(cells[1])
7773
(x2 - (x1 - 1)) * (y2 - (y1 - 1))
7874
end
7975

0 commit comments

Comments
 (0)