Skip to content

Commit

Permalink
Merge pull request #581 from amorde/string-allocations
Browse files Browse the repository at this point in the history
Reduce the number of string allocations
  • Loading branch information
amorde authored Jun 25, 2018
2 parents 5d9add3 + 25c7a42 commit 80a2cda
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/xcodeproj/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions lib/xcodeproj/project/object/helpers/groupable_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ def source_tree_real_path(object)
case object.source_tree
when '<group>'
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 '<absolute>'
nil
else
Expand Down
13 changes: 8 additions & 5 deletions lib/xcodeproj/project/object_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,15 @@ 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)

previous_value = send(attrb.name)
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/xcodeproj/scheme/buildable_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '.'
Expand Down
1 change: 1 addition & 0 deletions spec/scheme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#-------------------------------------------------------------------------#
Expand Down

0 comments on commit 80a2cda

Please sign in to comment.