summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/pivot_table.rb
diff options
context:
space:
mode:
authorPaul Kmiec <[email protected]>2023-05-05 08:58:17 -0700
committerPaul Kmiec <[email protected]>2023-05-05 09:29:58 -0700
commit67aefd7705df82e43a8670102400a5abab49f6e8 (patch)
tree6872755fccedb331f4553c72b642c6c86a700bff /lib/axlsx/workbook/worksheet/pivot_table.rb
parent1c355c83a9603f835dfe59ef4473df2b8cc3534c (diff)
downloadcaxlsx-67aefd7705df82e43a8670102400a5abab49f6e8.tar.gz
caxlsx-67aefd7705df82e43a8670102400a5abab49f6e8.zip
Pipe output directly to str and avoid additional memory allocations
Currently, there are lots of examples of code like this, ``` str << ('<tag ' << foo << ' ' << bar << '/>') ``` which create the string for the tag in memory before piping to str. We can avoid creating all of these intermediate strings by dropping the paranthesis and piping directly to str. This relies on the `str` passed around to handle lots of small appends. This is a problem when using RubyZip, but that is solved in the next commit.
Diffstat (limited to 'lib/axlsx/workbook/worksheet/pivot_table.rb')
-rw-r--r--lib/axlsx/workbook/worksheet/pivot_table.rb20
1 files changed, 10 insertions, 10 deletions
diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb
index e97b8119..27065e4b 100644
--- a/lib/axlsx/workbook/worksheet/pivot_table.rb
+++ b/lib/axlsx/workbook/worksheet/pivot_table.rb
@@ -189,10 +189,10 @@ module Axlsx
def to_xml_string(str = +'')
str << '<?xml version="1.0" encoding="UTF-8"?>'
- str << (+'<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '"' << (data.size <= 1 ? ' dataOnRows="1"' : '') << ' applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">')
+ str << '<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '"' << (data.size <= 1 ? ' dataOnRows="1"' : '') << ' applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">'
- str << (+'<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>')
- str << (+'<pivotFields count="' << header_cells_count.to_s << '">')
+ str << '<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>'
+ str << '<pivotFields count="' << header_cells_count.to_s << '">'
header_cell_values.each do |cell_value|
subtotal = !no_subtotals_on_headers.include?(cell_value)
@@ -205,12 +205,12 @@ module Axlsx
str << '<rowFields count="1"><field x="-2"/></rowFields>'
str << '<rowItems count="2"><i><x/></i> <i i="1"><x v="1"/></i></rowItems>'
else
- str << (+'<rowFields count="' << rows.size.to_s << '">')
+ str << '<rowFields count="' << rows.size.to_s << '">'
rows.each do |row_value|
- str << (+'<field x="' << header_index_of(row_value).to_s << '"/>')
+ str << '<field x="' << header_index_of(row_value).to_s << '"/>'
end
str << '</rowFields>'
- str << (+'<rowItems count="' << rows.size.to_s << '">')
+ str << '<rowItems count="' << rows.size.to_s << '">'
rows.size.times do |i|
str << '<i/>'
end
@@ -229,16 +229,16 @@ module Axlsx
str << '<colItems count="1"><i/></colItems>'
end
else
- str << (+'<colFields count="' << columns.size.to_s << '">')
+ str << '<colFields count="' << columns.size.to_s << '">'
columns.each do |column_value|
- str << (+'<field x="' << header_index_of(column_value).to_s << '"/>')
+ str << '<field x="' << header_index_of(column_value).to_s << '"/>'
end
str << '</colFields>'
end
unless pages.empty?
- str << (+'<pageFields count="' << pages.size.to_s << '">')
+ str << '<pageFields count="' << pages.size.to_s << '">'
pages.each do |page_value|
- str << (+'<pageField fld="' << header_index_of(page_value).to_s << '"/>')
+ str << '<pageField fld="' << header_index_of(page_value).to_s << '"/>'
end
str << '</pageFields>'
end