Skip to content

Commit

Permalink
Merge pull request demarches-simplifiees#9915 from colinux/fix-admin-…
Browse files Browse the repository at this point in the history
…memory-leak

Perf: remplace les `OpenStruct` par des objets plus performants
  • Loading branch information
colinux authored Jan 17, 2024
2 parents 1d0f596 + b8fdbff commit 1a8f839
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@ Performance/FixedSize:
Performance/FlatMap:
Enabled: true

Performance/OpenStruct:
Enabled: true

Performance/RangeInclude:
Enabled: true

Expand Down
1 change: 1 addition & 0 deletions app/models/KeyableModel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KeyableModel = Struct.new(:model_name, :to_key, :param_key, keyword_init: true)
1 change: 1 addition & 0 deletions app/models/label_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LabelModel = Struct.new(:id, :label, keyword_init: true)
3 changes: 2 additions & 1 deletion app/models/null_zone.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/models/procedure_detail.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,9 +7,9 @@ def spreadsheet_columns
end
end

AdministrateursCounter = Struct.new(:count)

def administrateurs
Administrateurs.new(admin_count)
AdministrateursCounter.new(admin_count)
end

Administrateurs = Struct.new(:count)
end
6 changes: 4 additions & 2 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,10 @@ def destroy_if_orphan
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?
Expand Down
2 changes: 1 addition & 1 deletion app/models/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def available_at?(date)
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

Expand Down
11 changes: 11 additions & 0 deletions lib/tasks/benchmarks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/jobs/api_entreprise/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ErrorJob < APIEntreprise::Job
def perform(error, etablissement)
@etablissement = etablissement

response = OpenStruct.new(
response = Typhoeus::Response.new(
effective_url: 'http://host.com/path',
code: '666',
body: 'body',
Expand Down

0 comments on commit 1a8f839

Please sign in to comment.