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

Reduce allocations in turbo_stream_action_tag #415

Merged

Conversation

jonathanhefner
Copy link
Contributor

This commit eliminates a few unnecessary allocations in turbo_stream_action_tag:

Benchmark

# frozen_string_literal: true
require "benchmark/memory"

class Helpers
  include Turbo::Streams::ActionHelper
end

class Message
  include ActiveModel::API
  def to_key() = [1]
end

helpers = Helpers.new
message = Message.new

Benchmark.memory do |x|
  helpers.turbo_stream_action_tag("remove", target: message) # warmup

  x.report("turbo_stream_action_tag") do
    helpers.turbo_stream_action_tag("remove", target: message)
  end
end

Before

Calculating -------------------------------------
turbo_stream_action_tag
                         2.554k memsize (     0.000  retained)
                        35.000  objects (     0.000  retained)
                        12.000  strings (     0.000  retained)

After

Calculating -------------------------------------
turbo_stream_action_tag
                         2.138k memsize (     0.000  retained)
                        31.000  objects (     0.000  retained)
                        12.000  strings (     0.000  retained)

This commit eliminates a few unnecessary allocations in
`turbo_stream_action_tag`:

**Benchmark**

  ```ruby
  # frozen_string_literal: true
  require "benchmark/memory"

  class Helpers
    include Turbo::Streams::ActionHelper
  end

  class Message
    include ActiveModel::API
    def to_key() = [1]
  end

  helpers = Helpers.new
  message = Message.new

  Benchmark.memory do |x|
    helpers.turbo_stream_action_tag("remove", target: message) # warmup

    x.report("turbo_stream_action_tag") do
      helpers.turbo_stream_action_tag("remove", target: message)
    end
  end
  ```

**Before**

  ```
  Calculating -------------------------------------
  turbo_stream_action_tag
                           2.554k memsize (     0.000  retained)
                          35.000  objects (     0.000  retained)
                          12.000  strings (     0.000  retained)
  ```

**After**

  ```
  Calculating -------------------------------------
  turbo_stream_action_tag
                           2.138k memsize (     0.000  retained)
                          31.000  objects (     0.000  retained)
                          12.000  strings (     0.000  retained)
  ```
@jonathanhefner
Copy link
Contributor Author

Build failure seems to be unrelated. The failure first appeared at 627a0cd, though I'm not sure that commit is related either because the tests were passing for 6e35878 (the commit it merged).

@dhh dhh merged commit a10ea27 into hotwired:main Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants