Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize string concatenation using << operator #617

Merged
merged 1 commit into from
Sep 8, 2024

Conversation

tagliala
Copy link
Contributor

@tagliala tagliala commented Sep 8, 2024

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

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)

Copy link

codecov bot commented Sep 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.31%. Comparing base (64aaf01) to head (34dc8f2).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@tagliala tagliala marked this pull request as draft September 8, 2024 08:22
@tagliala
Copy link
Contributor Author

tagliala commented Sep 8, 2024

I ran the activeadmin test suite against this branch, all green

@tagliala tagliala marked this pull request as ready for review September 8, 2024 08:25
@tagliala tagliala marked this pull request as draft September 8, 2024 13:28
@tagliala tagliala marked this pull request as ready for review September 8, 2024 13:52
@javierjulio javierjulio self-requested a review September 8, 2024 16:03
Copy link
Member

@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

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)
```
@tagliala tagliala merged commit 2d5a438 into activeadmin:master Sep 8, 2024
21 checks passed
@tagliala tagliala deleted the performance/improve-indent branch September 8, 2024 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants