From 5ec9070bd84d8699e8b4a836c01be610a408b1e4 Mon Sep 17 00:00:00 2001 From: Jacobo Bouzas Quiroga Date: Thu, 5 Dec 2024 12:12:17 +0100 Subject: [PATCH 1/2] fix: improve `fetch_for_repository` performance by preventing repository classes constantizing --- lib/resource_registry/registry.rb | 15 ++++----------- lib/resource_registry/resource.rb | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/resource_registry/registry.rb b/lib/resource_registry/registry.rb index d958a14..c043fc8 100644 --- a/lib/resource_registry/registry.rb +++ b/lib/resource_registry/registry.rb @@ -1,5 +1,5 @@ +# typed: strict # frozen_string_literal: true -# typed: false require_relative "resource" require_relative "capabilities/capability_config" @@ -49,9 +49,11 @@ def fetch!(identifier) ).returns(T.nilable(Resource)) end def fetch_for_repository(repository_class) - fetch_all.values.find { |r| r.repository == repository_class } + fetch_all.values.find { |r| r.repository_raw == repository_class.to_s } end + alias find_for_repository fetch_for_repository + sig { returns(T::Hash[String, Resource]) } def fetch_all resources @@ -71,15 +73,6 @@ def fetch_with_capabilities(*capabilities) end end - sig do - params( - repository: T::Class[ResourceRegistry::Repositories::Base[T.untyped]] - ).returns(T.nilable(Resource)) - end - def find_by_repository(repository) - fetch_all.values.find { |resource| resource.repository == repository } - end - private sig { returns(T::Hash[String, Resource]) } diff --git a/lib/resource_registry/resource.rb b/lib/resource_registry/resource.rb index 7edd957..bd14260 100644 --- a/lib/resource_registry/resource.rb +++ b/lib/resource_registry/resource.rb @@ -210,7 +210,7 @@ def verbs_except(except) def dump { "identifier" => identifier, - "repository" => repository.to_s, + "repository" => repository_raw, "description" => description, "relationships" => relationships.values.map(&:dump), "capabilities" => From 18f343779bc4fe0bb0ff68d1feae5500c20138db Mon Sep 17 00:00:00 2001 From: Jacobo Bouzas Quiroga Date: Thu, 5 Dec 2024 12:18:47 +0100 Subject: [PATCH 2/2] improvement: make method more scalable --- lib/resource_registry/registry.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/resource_registry/registry.rb b/lib/resource_registry/registry.rb index c043fc8..f50be28 100644 --- a/lib/resource_registry/registry.rb +++ b/lib/resource_registry/registry.rb @@ -49,7 +49,7 @@ def fetch!(identifier) ).returns(T.nilable(Resource)) end def fetch_for_repository(repository_class) - fetch_all.values.find { |r| r.repository_raw == repository_class.to_s } + resources_by_raw_repository[repository_class.to_s] end alias find_for_repository fetch_for_repository @@ -78,6 +78,15 @@ def fetch_with_capabilities(*capabilities) sig { returns(T::Hash[String, Resource]) } attr_accessor :resources + sig { returns(T::Hash[String, Resource]) } + def resources_by_raw_repository + @resources_by_raw_repository ||= + T.let( + resources.values.index_by(&:repository_raw), + T.nilable(T::Hash[String, Resource]) + ) + end + sig { params(resources: T::Array[Resource]).returns(T::Boolean) } def duplicated_identifier?(resources) resources.map(&:identifier).uniq.size != resources.size