From 25c7a4267db2b93f2e0db08b08e9d5c0243eaed9 Mon Sep 17 00:00:00 2001 From: Eric Amorde Date: Sat, 23 Jun 2018 16:13:08 -0700 Subject: [PATCH] Reduce the number of string allocations --- lib/xcodeproj/project.rb | 5 +++++ .../project/object/helpers/groupable_helper.rb | 4 ++-- lib/xcodeproj/project/object_attributes.rb | 13 ++++++++----- lib/xcodeproj/scheme/buildable_reference.rb | 2 +- spec/scheme_spec.rb | 1 + 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/xcodeproj/project.rb b/lib/xcodeproj/project.rb index 853f37a6f..db3befecc 100644 --- a/lib/xcodeproj/project.rb +++ b/lib/xcodeproj/project.rb @@ -46,6 +46,10 @@ class Project # attr_reader :path + # @return [Pathname] the directory of the project + # + attr_reader :project_dir + # @param [Pathname, String] path @see path # The path provided will be expanded to an absolute path. # @param [Bool] skip_initialization @@ -64,6 +68,7 @@ class Project # def initialize(path, skip_initialization = false, object_version = Constants::DEFAULT_OBJECT_VERSION) @path = Pathname.new(path).expand_path + @project_dir = @path.dirname @objects_by_uuid = {} @generated_uuids = [] @available_uuids = [] diff --git a/lib/xcodeproj/project/object/helpers/groupable_helper.rb b/lib/xcodeproj/project/object/helpers/groupable_helper.rb index 12ae2e338..344f327e2 100644 --- a/lib/xcodeproj/project/object/helpers/groupable_helper.rb +++ b/lib/xcodeproj/project/object/helpers/groupable_helper.rb @@ -116,12 +116,12 @@ def source_tree_real_path(object) case object.source_tree when '' if parent(object).isa == 'PBXProject' - object.project.path.dirname + object.project.project_dir else real_path(parent(object)) end when 'SOURCE_ROOT' - object.project.path.dirname + object.project.project_dir when '' nil else diff --git a/lib/xcodeproj/project/object_attributes.rb b/lib/xcodeproj/project/object_attributes.rb index 82ce44e82..e532565a9 100644 --- a/lib/xcodeproj/project/object_attributes.rb +++ b/lib/xcodeproj/project/object_attributes.rb @@ -354,6 +354,7 @@ def has_one(singular_name, isas) # 1.9.2 fix, see https://github.com/CocoaPods/Xcodeproj/issues/40. public(attrb.name) + variable_name = :"@#{attrb.name}" define_method("#{attrb.name}=") do |value| attrb.validate_value(value) @@ -361,7 +362,7 @@ def has_one(singular_name, isas) return value if previous_value == value mark_project_as_dirty! previous_value.remove_referrer(self) if previous_value - instance_variable_set("@#{attrb.name}", value) + instance_variable_set(variable_name, value) value.add_referrer(self) if value end end @@ -392,12 +393,13 @@ def has_many(plural_name, isas) attrb.classes = isas add_attribute(attrb) + variable_name = :"@#{attrb.name}" define_method(attrb.name) do # Here we are in the context of the instance - list = instance_variable_get("@#{attrb.name}") + list = instance_variable_get(variable_name) unless list list = ObjectList.new(attrb, self) - instance_variable_set("@#{attrb.name}", list) + instance_variable_set(variable_name, list) end list end @@ -428,12 +430,13 @@ def has_many_references_by_keys(plural_name, classes_by_key) attrb.classes_by_key = classes_by_key add_attribute(attrb) + variable_name = :"@#{attrb.name}" define_method(attrb.name) do # Here we are in the context of the instance - list = instance_variable_get("@#{attrb.name}") + list = instance_variable_get(variable_name) unless list list = ObjectList.new(attrb, self) - instance_variable_set("@#{attrb.name}", list) + instance_variable_set(variable_name, list) end list end diff --git a/lib/xcodeproj/scheme/buildable_reference.rb b/lib/xcodeproj/scheme/buildable_reference.rb index c731dc9f0..754d95af3 100644 --- a/lib/xcodeproj/scheme/buildable_reference.rb +++ b/lib/xcodeproj/scheme/buildable_reference.rb @@ -116,7 +116,7 @@ def construct_referenced_container_uri(target, root_project = nil) path = if !root_project_dir_path.to_s.empty? root_project.path + root_project_dir_path else - root_project.path.dirname + root_project.project_dir end relative_path = target_project.path.relative_path_from(path).to_s relative_path = target_project.path.basename if relative_path == '.' diff --git a/spec/scheme_spec.rb b/spec/scheme_spec.rb index bb12736a4..0472d01f7 100644 --- a/spec/scheme_spec.rb +++ b/spec/scheme_spec.rb @@ -9,6 +9,7 @@ module ProjectSpecs describe Xcodeproj::XCScheme do before do @project.stubs(:path).returns(Pathname.new('path/Cocoa Application.xcodeproj')) + @project.stubs(:project_dir).returns(Pathname.new('path/')) end #-------------------------------------------------------------------------#