Skip to content

Commit

Permalink
Stop byteslicing empty strings in breadcrumbs
Browse files Browse the repository at this point in the history
If we don't pass a message to the breadcrumb message, we default to an
empty string. However we can avoid byteslicing it, which allocates a new
empty string every time regardless.

```rb

require 'benchmark-memory'

MAX_MESSAGE_SIZE_IN_BYTES = 1024 * 8

def current(message)
  (message || "").byteslice(0..MAX_MESSAGE_SIZE_IN_BYTES)
end

def updated(message)
  message ? message.byteslice(0..MAX_MESSAGE_SIZE_IN_BYTES) : ""
end

Benchmark.memory do |x|
  x.report("current") do
    current(nil)
  end

  x.report("updated") do
    updated(nil)
  end

  x.compare!
end
```

Calculating -------------------------------------
             current    80.000  memsize (     0.000  retained)
                         2.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
             updated     0.000  memsize (     0.000  retained)
                         0.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

Comparison:
             updated:          0 allocated
             current:         80 allocated - Infx more
  • Loading branch information
nirebu committed Mar 6, 2025
1 parent f11a7aa commit a67564c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/breadcrumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def to_hash
# @param message [String]
# @return [void]
def message=(message)
@message = (message || "").byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
@message = message ? message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES) : ""
end

# @param level [String]
Expand Down

0 comments on commit a67564c

Please sign in to comment.