Skip to content

Conversation

@nirebu
Copy link
Contributor

@nirebu nirebu commented Mar 6, 2025

Please keep these instructions in mind so we can review it more efficiently:

  • Add the references of all the related issues/PRs in the description
  • Whether it's a new feature or a bug fix, make sure they're covered by new test cases
  • If this PR contains any refactoring work, please give it its own commit(s)
  • Finally, please add an entry to the corresponding changelog

Other Notes

  • We squash all commits before merging
  • We generally review new PRs within a week
  • If you have any question, you can ask for feedback in our discord community first

Description

Describe your changes:

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.

This is a pretty hot path, and the tests pass. Only difference I can think of is returning a frozen empty string, which could cause issues downstream. Let me know if it should be dup-ed!

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

@nirebu nirebu force-pushed the nirebu/stop-byteslicing-nil-breadcrumbs branch from a67564c to f1a4da4 Compare March 10, 2025 13:06
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
@nirebu nirebu force-pushed the nirebu/stop-byteslicing-nil-breadcrumbs branch from f1a4da4 to 23eeaba Compare March 10, 2025 13:40
@solnic solnic merged commit 0e38647 into getsentry:master Mar 10, 2025
133 checks passed
@codecov
Copy link

codecov bot commented Mar 10, 2025

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 64.52%. Comparing base (db975df) to head (23eeaba).
Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
sentry-ruby/lib/sentry/breadcrumb.rb 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2574      +/-   ##
==========================================
+ Coverage   64.11%   64.52%   +0.40%     
==========================================
  Files         123      123              
  Lines        4724     4679      -45     
==========================================
- Hits         3029     3019      -10     
+ Misses       1695     1660      -35     
Components Coverage Δ
sentry-ruby 60.17% <81.81%> (+0.32%) ⬆️
sentry-rails 59.82% <ø> (+0.60%) ⬆️
sentry-sidekiq 97.16% <ø> (ø)
sentry-resque 94.44% <100.00%> (+1.58%) ⬆️
sentry-delayed_job 95.65% <ø> (ø)
sentry-opentelemetry 99.31% <ø> (ø)
Files with missing lines Coverage Δ
sentry-ruby/lib/sentry/breadcrumb.rb 46.87% <0.00%> (+1.42%) ⬆️

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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