Merge pull request #9915 from colinux/fix-admin-memory-leak

Perf: remplace les `OpenStruct` par des objets plus performants
This commit is contained in:
Colin Darie 2024-01-17 08:59:01 +00:00 committed by GitHub
commit 1a8f839a7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 29 additions and 10 deletions

View file

@ -636,6 +636,9 @@ Performance/FixedSize:
Performance/FlatMap:
Enabled: true
Performance/OpenStruct:
Enabled: true
Performance/RangeInclude:
Enabled: true

View file

@ -0,0 +1 @@
KeyableModel = Struct.new(:model_name, :to_key, :param_key, keyword_init: true)

View file

@ -0,0 +1 @@
LabelModel = Struct.new(:id, :label, keyword_init: true)

View file

@ -1,12 +1,13 @@
class NullZone
include ActiveModel::Model
ReflectionAssociation = Struct.new(:class_name)
def procedures
Procedure.where(zone: nil).where.not(published_at: nil).order(published_at: :desc)
end
def self.reflect_on_association(association)
OpenStruct.new(class_name: "Procedure") if association == :procedures
ReflectionAssociation.new("Procedure") if association == :procedures
end
def label

View file

@ -1,4 +1,4 @@
class ProcedureDetail < OpenStruct
ProcedureDetail = Struct.new(:id, :libelle, :published_at, :aasm_state, :estimated_dossiers_count, :admin_count, keyword_init: true) do
include SpreadsheetArchitect
def spreadsheet_columns
@ -7,9 +7,9 @@ class ProcedureDetail < OpenStruct
end
end
def administrateurs
Administrateurs.new(admin_count)
end
AdministrateursCounter = Struct.new(:count)
Administrateurs = Struct.new(:count)
def administrateurs
AdministrateursCounter.new(admin_count)
end
end

View file

@ -614,8 +614,10 @@ class TypeDeChamp < ApplicationRecord
end
def stable_self
OpenStruct.new(to_key: [stable_id],
model_name: OpenStruct.new(param_key: model_name.param_key))
KeyableModel.new(
to_key: [stable_id],
model_name: KeyableModel.new(param_key: model_name.param_key)
)
end
def refresh_after_update?

View file

@ -19,7 +19,7 @@ class Zone < ApplicationRecord
def self.available_at(date, without_zones = [])
(Zone.all - without_zones).filter { |zone| zone.available_at?(date) }.sort_by { |zone| zone.label_at(date) }
.map do |zone|
OpenStruct.new(id: zone.id, label: zone.label_at(date))
LabelModel.new(id: zone.id, label: zone.label_at(date))
end
end

View file

@ -131,4 +131,15 @@ namespace :benchmarks do
x.compare!
end
end
desc "Inspect possible memory leaks"
task inspect_memory_leak: :environment do
10.times do
10_000.times do
# Write code here
end
puts `ps -o rss= -p #{$$}`
end
end
end

View file

@ -38,7 +38,7 @@ RSpec.describe APIEntreprise::Job, type: :job do
def perform(error, etablissement)
@etablissement = etablissement
response = OpenStruct.new(
response = Typhoeus::Response.new(
effective_url: 'http://host.com/path',
code: '666',
body: 'body',