Optimize string concatenation using << operator#617
Merged
tagliala merged 1 commit intoactiveadmin:masterfrom Sep 8, 2024
Merged
Optimize string concatenation using << operator#617tagliala merged 1 commit intoactiveadmin:masterfrom
<< operator#617tagliala merged 1 commit intoactiveadmin:masterfrom
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #617 +/- ##
==========================================
- Coverage 94.32% 94.31% -0.02%
==========================================
Files 17 17
Lines 458 457 -1
==========================================
- Hits 432 431 -1
Misses 26 26 ☔ View full report in Codecov by Sentry. |
Contributor
Author
|
I ran the activeadmin test suite against this branch, all green |
e8b873b to
6d95c4e
Compare
This change enhances the efficiency of HTML string construction, by
replacing `+=` operator with `<<`.
The shovel operator (`<<`) modifies strings in-place, reducing memory
allocation and improving overall performance compared to the `+=`
operator.
### Test Case
Ruby 3.3.5 x64 on M1 Pro
```rb
Arbre::Context.new { div { |d| d.ul { li } } }.to_s
```
#### Before
```
--- Memory Profiler ---
Total allocated: 4624 bytes (80 objects)
Total retained: 0 bytes (0 objects)
allocated memory by gem
-----------------------------------
4624 arbre/lib
allocated memory by file
-----------------------------------
2560 arbre/html/tag.rb
1120 arbre/element/builder_methods.rb
400 arbre/element_collection.rb
280 arbre/element.rb
264 arbre/context.rb
--- Benchmark: IPS ---
test 50.546k (± 1.4%) i/s - 254.550k in 5.037005s
--- Benchmark: Memory ---
test 6.208k memsize ( 0.000 retained)
99.000 objects ( 0.000 retained)
29.000 strings ( 0.000 retained)
```
#### After
```
--- Memory Profiler ---
Total allocated: 3840 bytes (64 objects)
Total retained: 0 bytes (0 objects)
allocated memory by gem
-----------------------------------
3840 arbre/lib
allocated memory by file
-----------------------------------
1776 arbre/html/tag.rb
1120 arbre/element/builder_methods.rb
400 arbre/element_collection.rb
280 arbre/element.rb
264 arbre/context.rb
--- Benchmark: IPS ---
test 52.205k (± 1.6%) i/s - 265.659k in 5.090028s
--- Benchmark: Memory ---
test 5.424k memsize ( 0.000 retained)
83.000 objects ( 0.000 retained)
19.000 strings ( 0.000 retained)
```
6d95c4e to
34dc8f2
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change enhances the efficiency of HTML string construction, by replacing
+=operator with<<.The shovel operator (
<<) modifies strings in-place, reducing memory allocation and improving overall performance compared to the+=operator.Test Case
Ruby 3.3.5 x64 on M1 Pro
Before
After