From 1cc26e0f0e2ef62fecb688e45bc51c66c8fdf0a7 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 22 Dec 2015 13:02:00 +0100 Subject: [PATCH] Improve performance of `StringPath` --- lib/gitlab/string_path.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index 493fceb256d..1eb8162f805 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -5,15 +5,15 @@ module Gitlab # This is IO-operations safe class, that does similar job to # Ruby's Pathname but without the risk of accessing filesystem. # - # TODO: better support for './' and '../' + # TODO: better support for '../' and './' # class StringPath attr_reader :path, :universe def initialize(path, universe) @path = prepare(path) - @universe = universe.map { |entry| prepare(entry) } - @universe.unshift('./') unless @universe.include?('./') + @universe = Set.new(universe.map { |entry| prepare(entry) }) + @universe.add('./') end def to_s @@ -64,7 +64,10 @@ module Gitlab end def children - descendants.select { |descendant| descendant.parent == self } + return [] unless directory? + return @children if @children + children = @universe.select { |entry| entry =~ %r{^#{@path}[^/]+/?$} } + @children = children.map { |path| new(path) } end def directories @@ -85,6 +88,10 @@ module Gitlab @path == other.path && @universe == other.universe end + def inspect + "#{self.class.name}: #{@path}" + end + private def new(path)