Skip to content

Commit

Permalink
Path: optimize join by precomputing capacity if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Aug 14, 2019
1 parent 59fb22d commit 6e0bc91
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,18 @@ struct Path
# Path.windows("foo\\bar").join(Path.posix("baz/baq")) # => Path.windows("foo\\bar\\baz/baq")
# ```
def join(parts : Enumerable) : Path
new_name = String.build do |str|
if parts.is_a?(Indexable)
# If we know how many parts we have we can compute an approximation of
# the string's capacity: this path's size plus the parts' size plus the
# separators between them
capacity = @name.bytesize +
parts.sum(&.to_s.bytesize) +
parts.size
else
capacity = 64
end

new_name = String.build(capacity) do |str|
str << @name
last_ended_with_separator = ends_with_separator?

Expand Down

0 comments on commit 6e0bc91

Please sign in to comment.