Skip to content

Commit 0e38647

Browse files
authored
Stop byteslicing empty strings in breadcrumbs (#2574)
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
1 parent f11a7aa commit 0e38647

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
- Use `File.open` in `LineCache` ([#2566](https://github.com/getsentry/sentry-ruby/pull/2566))
1616
- Update java backtrace regexp ([#2567](https://github.com/getsentry/sentry-ruby/pull/2567))
17+
- Stop byteslicing empty strings in breadcrumbs ([#2574](https://github.com/getsentry/sentry-ruby/pull/2574))
1718

1819
### Miscellaneous
1920

sentry-ruby/lib/sentry/breadcrumb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def to_hash
4747
# @param message [String]
4848
# @return [void]
4949
def message=(message)
50-
@message = (message || "").byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
50+
@message = message ? message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES) : ""
5151
end
5252

5353
# @param level [String]

0 commit comments

Comments
 (0)