From 0c74d2b0564ec011759e97c99b3886bb4ddf145b Mon Sep 17 00:00:00 2001 From: Vincent Prigent Date: Mon, 7 Oct 2024 11:29:53 +1300 Subject: [PATCH] Memoize call_stack registry for same key entry 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) ``` --- lib/bullet/registry/call_stack.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bullet/registry/call_stack.rb b/lib/bullet/registry/call_stack.rb index ee32b748..8a4c1b14 100644 --- a/lib/bullet/registry/call_stack.rb +++ b/lib/bullet/registry/call_stack.rb @@ -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