Skip to content
  • Jean Boussier's avatar
    6fb612b5
    Optimize ActiveRecord::QueryLogs · 6fb612b5
    Jean Boussier authored
    I noticed it showed up quite a bit on our production allocation
    profiles. I tried to not break the interface, but in reality most
    of it is private and it should only be configured through `application.config`.
    
    main:
    
    ```
    ruby 3.3.3 (2024-06-12 revision f1c7b6f435) +YJIT [arm64-darwin23]
    
    Total allocated: 1440 bytes (18 objects)
    
    Calculating -------------------------------------
                     tag    521.552k (± 2.0%) i/s -      2.652M in   5.087518s
    ```
    
    this branch:
    
    ```
    ruby 3.3.3 (2024-06-12 revision f1c7b6f435) +YJIT [arm64-darwin23]
    
    Total allocated: 840 bytes (7 objects)
    
    Calculating -------------------------------------
                     tag      1.070M (± 1.9%) i/s -      5.379M in   5.026878s
    ```
    
    Benchmark:
    
    ```ruby
    require 'bundler/inline'
    
    gemfile(true) do
      source "https://rubygems.org"
    
      gem "benchmark-ips"
      gem "memory_profiler"
      gem "rails"
    end
    
    require "active_record"
    
    ActiveRecord::QueryLogs.taggings = {
      some_handler: -> { "Handler" }
    }
    ActiveRecord::QueryLogs.tags = [
      :application,
      :some_handler,
      fixed_string: "fixed string",
      callback: ->(context) { "callback" },
    ]
    
    ActiveSupport::ExecutionContext[:application] = "SuperApp"
    
    ActiveRecord::QueryLogs.singleton_class.class_eval { public(:tag_content) }
    
    ActiveRecord::QueryLogs.tag_content(:__connection__)
    
    report = MemoryProfiler.report do
      ActiveRecord::QueryLogs.tag_content(:__connection__)
    end
    
    report.pretty_print
    
    Benchmark.ips do |x|
      x.report("tag") { ActiveRecord::QueryLogs.tag_content(:__connection__) }
    end
    ```
    6fb612b5
    Optimize ActiveRecord::QueryLogs
    Jean Boussier authored
    I noticed it showed up quite a bit on our production allocation
    profiles. I tried to not break the interface, but in reality most
    of it is private and it should only be configured through `application.config`.
    
    main:
    
    ```
    ruby 3.3.3 (2024-06-12 revision f1c7b6f435) +YJIT [arm64-darwin23]
    
    Total allocated: 1440 bytes (18 objects)
    
    Calculating -------------------------------------
                     tag    521.552k (± 2.0%) i/s -      2.652M in   5.087518s
    ```
    
    this branch:
    
    ```
    ruby 3.3.3 (2024-06-12 revision f1c7b6f435) +YJIT [arm64-darwin23]
    
    Total allocated: 840 bytes (7 objects)
    
    Calculating -------------------------------------
                     tag      1.070M (± 1.9%) i/s -      5.379M in   5.026878s
    ```
    
    Benchmark:
    
    ```ruby
    require 'bundler/inline'
    
    gemfile(true) do
      source "https://rubygems.org"
    
      gem "benchmark-ips"
      gem "memory_profiler"
      gem "rails"
    end
    
    require "active_record"
    
    ActiveRecord::QueryLogs.taggings = {
      some_handler: -> { "Handler" }
    }
    ActiveRecord::QueryLogs.tags = [
      :application,
      :some_handler,
      fixed_string: "fixed string",
      callback: ->(context) { "callback" },
    ]
    
    ActiveSupport::ExecutionContext[:application] = "SuperApp"
    
    ActiveRecord::QueryLogs.singleton_class.class_eval { public(:tag_content) }
    
    ActiveRecord::QueryLogs.tag_content(:__connection__)
    
    report = MemoryProfiler.report do
      ActiveRecord::QueryLogs.tag_content(:__connection__)
    end
    
    report.pretty_print
    
    Benchmark.ips do |x|
      x.report("tag") { ActiveRecord::QueryLogs.tag_content(:__connection__) }
    end
    ```
Loading