Skip to content

Commit

Permalink
Memoize call_stack registry for same key entry
Browse files Browse the repository at this point in the history
With this simple trick, a local rails server with less than a dozen SQL 
queries using AR drastically reduces the amount of memory allocated by 
bullet.
before:
```
allocated memory by gem
-----------------------------------
    796102  activesupport-7.2.1
    726428  bullet/lib
```

after:
```
allocated memory by gem
-----------------------------------
    794350  activesupport-7.2.1
    313180  sprockets-4.2.1
    259688  activerecord-7.2.1
    204936  bullet/lib
```

Using the patched perf/benchmark.rb file to work with sqlite instead of 
mysql, I saw reproductible numbers:

main
```
                                                                        
user     system      total        real
Querying & Iterating 1000 Posts with 10000 Comments and 100 Users       
14.503545   0.638605  15.142150 ( 15.201747)
```

patch
```
                                                                        
user     system      total        real
Querying & Iterating 1000 Posts with 10000 Comments and 100 Users       
12.781415   0.664749  13.446164 ( 13.533855)
```
  • Loading branch information
vprigent committed Oct 6, 2024
1 parent 3193a7d commit 0c74d2b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bullet/registry/call_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Registry
class CallStack < Base
# remembers found association backtrace
def add(key)
@registry[key] = Thread.current.backtrace
@registry[key] ||= Thread.current.backtrace
end
end
end
Expand Down

0 comments on commit 0c74d2b

Please sign in to comment.