diff options
| author | Paul Kmiec <[email protected]> | 2023-05-05 08:58:17 -0700 |
|---|---|---|
| committer | Paul Kmiec <[email protected]> | 2023-05-05 09:29:58 -0700 |
| commit | 67aefd7705df82e43a8670102400a5abab49f6e8 (patch) | |
| tree | 6872755fccedb331f4553c72b642c6c86a700bff /lib/axlsx/content_type | |
| parent | 1c355c83a9603f835dfe59ef4473df2b8cc3534c (diff) | |
| download | caxlsx-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/content_type')
| -rw-r--r-- | lib/axlsx/content_type/abstract_content_type.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/content_type/content_type.rb | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/lib/axlsx/content_type/abstract_content_type.rb b/lib/axlsx/content_type/abstract_content_type.rb index 58ca8b2c..bca08b7e 100644 --- a/lib/axlsx/content_type/abstract_content_type.rb +++ b/lib/axlsx/content_type/abstract_content_type.rb @@ -24,7 +24,7 @@ module Axlsx # Serialize the contenty type to xml def to_xml_string(node_name = '', str = +'') str << "<#{node_name} " - str << Axlsx.instance_values_for(self).map { |key, value| Axlsx::camel(key) << '="' << value.to_s << '"' }.join(' ') + Axlsx.instance_values_for(self).each { |key, value| str << Axlsx::camel(key) << '="' << value.to_s << '" ' } str << '/>' end end diff --git a/lib/axlsx/content_type/content_type.rb b/lib/axlsx/content_type/content_type.rb index 15751b17..22763bfb 100644 --- a/lib/axlsx/content_type/content_type.rb +++ b/lib/axlsx/content_type/content_type.rb @@ -16,7 +16,7 @@ module Axlsx # @return [String] def to_xml_string(str = +'') str << '<?xml version="1.0" encoding="UTF-8"?>' - str << (+'<Types xmlns="' << XML_NS_T << '">') + str << '<Types xmlns="' << XML_NS_T << '">' each { |type| type.to_xml_string(str) } str << '</Types>' end |
