Skip to content

Commit 571ce7b

Browse files
committed
Small fixes and memory optimizations
1 parent 6ae07d3 commit 571ce7b

File tree

7 files changed

+151
-24
lines changed

7 files changed

+151
-24
lines changed

benchmarks/auto_width.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative 'init'
2+
3+
HEADERS = ["id", "name", "age", "date"]
4+
5+
DATA = []
6+
1000.times do |n|
7+
DATA << [n, "String string #{n}", (n * rand * 10).round, Time.at(n * 1000 + 1492922688)]
8+
end
9+
10+
Benchmark.ips do |x|
11+
x.config(time: 10, warmup: 2)
12+
13+
x.report("Normal") do
14+
workbook = FastExcel.open(constant_memory: true)
15+
worksheet = workbook.add_worksheet("benchmark")
16+
17+
worksheet.write_row(0, HEADERS)
18+
DATA.each_with_index do |row, i|
19+
worksheet.write_row(i + 1, row)
20+
end
21+
workbook.read_string
22+
end
23+
24+
x.report("With auto_width") do
25+
workbook = FastExcel.open(constant_memory: true)
26+
worksheet = workbook.add_worksheet("benchmark")
27+
worksheet.auto_width = true
28+
29+
worksheet.write_row(0, HEADERS)
30+
DATA.each_with_index do |row, i|
31+
worksheet.write_row(i + 1, row)
32+
end
33+
workbook.read_string
34+
end
35+
36+
x.compare!
37+
end

benchmarks/init.rb

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
require 'write_xlsx'
1111
require 'process_memory'
1212

13-
require_relative 'init'
14-
1513
def write_fast_excel_20k
1614
workbook = FastExcel.open(constant_memory: true)
1715
worksheet = workbook.add_worksheet("benchmark")

benchmarks/profiler.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative '../lib/fast_excel'
2+
require 'ruby-prof'
3+
4+
DATA = []
5+
1000.times do |n|
6+
DATA << [n, "String string #{n}", (n * rand * 10).round, Time.at(n * 1000 + 1492922688)]
7+
end
8+
9+
RubyProf.start
10+
11+
100.times do
12+
workbook = FastExcel.open(constant_memory: true)
13+
worksheet = workbook.add_worksheet("benchmark")
14+
worksheet.auto_width = true
15+
16+
DATA.each_with_index do |row, i|
17+
worksheet.write_row(i + 1, row)
18+
end
19+
workbook.read_string
20+
21+
print '.'
22+
end
23+
result = RubyProf.stop
24+
25+
# print a flat profile to text
26+
printer = RubyProf::FlatPrinter.new(result)
27+
printer.print(STDOUT)

benchmarks/write_value.rb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require_relative '../lib/fast_excel'
2+
require "benchmark/memory"
3+
require "benchmark/ips"
4+
5+
DATA = []
6+
1000.times do |n|
7+
DATA << [n, "String string #{n}", (n * rand * 10).round, Time.at(n * 1000 + 1492922688)]
8+
end
9+
10+
5.times do
11+
workbook = FastExcel.open(constant_memory: true)
12+
worksheet = workbook.add_worksheet("benchmark")
13+
14+
DATA.each_with_index do |row, row_num|
15+
row.each_with_index do |val, cell_num|
16+
worksheet.write_value(row_num + 1, cell_num + 1, val)
17+
end
18+
end
19+
workbook.read_string
20+
end
21+
22+
23+
workbook = FastExcel.open(constant_memory: true)
24+
worksheet = workbook.add_worksheet("benchmark")
25+
26+
DATA.each_with_index do |row, row_num|
27+
row.each_with_index do |val, cell_num|
28+
worksheet.write_value(row_num + 1, cell_num + 1, val)
29+
end
30+
end
31+
workbook.read_string
32+
33+
Benchmark.ips do |x|
34+
#x.config(time: 10, warmup: 2)
35+
36+
x.report("Normal") do
37+
workbook = FastExcel.open(constant_memory: true)
38+
worksheet = workbook.add_worksheet("benchmark")
39+
40+
DATA.each_with_index do |row, row_num|
41+
row.each_with_index do |val, cell_num|
42+
worksheet.write_value(row_num + 1, cell_num + 1, val)
43+
end
44+
end
45+
workbook.read_string
46+
end
47+
48+
x.report("Auto-width") do
49+
workbook = FastExcel.open(constant_memory: true)
50+
worksheet = workbook.add_worksheet("benchmark")
51+
worksheet.auto_width = true
52+
53+
DATA.each_with_index do |row, row_num|
54+
row.each_with_index do |val, cell_num|
55+
worksheet.write_value(row_num + 1, cell_num + 1, val)
56+
end
57+
end
58+
workbook.read_string
59+
end
60+
61+
x.compare!
62+
end

lib/fast_excel.rb

+10-22
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ module WorksheetExt
386386
def initialize(struct)
387387
@is_open = true
388388
@col_formats = {}
389+
@last_row_number = -1
389390
super(struct)
390391
end
391392

@@ -410,16 +411,16 @@ def auto_width=(v)
410411

411412
def write_value(row_number, cell_number, value, format = nil)
412413

413-
if workbook.constant_memory? && row_number < last_row_number
414+
if workbook.constant_memory? && row_number < @last_row_number
414415
raise ArgumentError, "Can not write to saved row in constant_memory mode (attempted row: #{row_number}, last saved row: #{last_row_number})"
415416
end
416417

417418
if value.is_a?(Numeric)
418419
write_number(row_number, cell_number, value, format)
419-
elsif defined?(DateTime) && value.is_a?(DateTime)
420-
write_datetime(row_number, cell_number, FastExcel.lxw_datetime(value), format)
421420
elsif value.is_a?(Time)
422-
write_datetime(row_number, cell_number, FastExcel.lxw_time(value), format)
421+
write_number(row_number, cell_number, FastExcel.date_num(value), format)
422+
elsif defined?(DateTime) && value.is_a?(DateTime)
423+
write_number(row_number, cell_number, FastExcel.date_num(value), format)
423424
elsif value.is_a?(Formula)
424425
write_formula(row_number, cell_number, value.fml, format)
425426
else
@@ -428,7 +429,7 @@ def write_value(row_number, cell_number, value, format = nil)
428429
add_text_width(value, format, cell_number) if auto_width?
429430
end
430431

431-
@last_row_number = row_number > last_row_number ? row_number : last_row_number
432+
@last_row_number = row_number > @last_row_number ? row_number : @last_row_number
432433
end
433434

434435
def add_text_width(value, format, cell_number)
@@ -444,14 +445,14 @@ def add_text_width(value, format, cell_number)
444445
end
445446

446447
if font_size == 0
447-
workbook.default_format.font_size
448+
font_size = workbook.default_format.font_size
448449
end
449450

450451
font_size = 13 if font_size == nil || font_size == 0
451452

452453
font_family = ''
453454
if format
454-
font_size = format.font_family
455+
font_family = format.font_family
455456
end
456457

457458
if font_family == ''
@@ -482,11 +483,11 @@ def append_row(values, formats = nil)
482483
end
483484

484485
def last_row_number
485-
defined?(@last_row_number) ? @last_row_number : -1
486+
@last_row_number
486487
end
487488

488489
def increment_last_row_number!
489-
@last_row_number = last_row_number + 1
490+
@last_row_number += 1
490491
end
491492

492493
def set_column(start_col, end_col, width, format = nil)
@@ -531,15 +532,6 @@ def close
531532
module FormatExt
532533
include AttributeHelper
533534

534-
[:font_size, :underline, :font_script, :rotation, :indent, :pattern, :border].each do |prop|
535-
define_method(prop) do
536-
self[prop]
537-
end
538-
define_method("#{prop}=") do |value|
539-
send("set_#{prop}", value)
540-
end
541-
end
542-
543535
[:bold, :italic, :font_outline, :font_shadow, :hidden, :text_wrap, :font_strikeout, :shrink, :text_justlast].each do |prop|
544536
define_method(prop) do
545537
self[prop]
@@ -553,10 +545,6 @@ module FormatExt
553545
define_method(prop) do
554546
self[prop].to_ptr.read_string
555547
end
556-
557-
define_method("#{prop}=") do |value|
558-
send("set_#{prop}", value)
559-
end
560548
end
561549

562550
ALIGN_ENUM = Libxlsxwriter.enum_type(:format_alignments)

lib/fast_excel/binding/format.rb

+7
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,13 @@ def set_reading_order(value)
684684
def set_theme(value)
685685
Libxlsxwriter.format_set_theme(self, value)
686686
end
687+
688+
[:font_size, :underline, :font_script, :rotation, :indent, :pattern, :border, :num_format, :font_name].each do |prop|
689+
alias :"#{prop}=" :"set_#{prop}"
690+
define_method(prop) do
691+
self[prop]
692+
end
693+
end
687694
end
688695

689696
class Format < FFI::Struct

test/format_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,12 @@
161161
assert_equal(:border_thin, @format.border_bottom)
162162
end
163163

164+
it "should define aliases" do
165+
@format.font_size = 20
166+
assert_equal(@format.font_size, 20)
167+
168+
@format.font_name = "XXX"
169+
assert_equal(@format.font_name, "XXX")
170+
end
171+
164172
end

0 commit comments

Comments
 (0)