Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Dir.glob #8081

Merged
merged 1 commit into from Aug 28, 2019

Conversation

@asterite
Copy link
Member

commented Aug 14, 2019

This PR optimized Dir.glob by avoiding unnecessary calls to File.info? for each directory entry because readdir already gives us that information.

This is similar to how Python does it since some time ago and I can imagine Ruby does that too, probably other languages as well.

I just wanted to optimize this so the public API is still the same. I just exposed internally the information of whether a dir entry is a directory or not just for this purpose.

In the future we can consider whether it makes sense to expose this information in a public API but right now I'd like to avoid huge breaking changes, or having to think about new APIs. I think the change here is pretty straight-forward and small that it's not a big deal to introduce this optimization.

I wrote this benchmark:

require "benchmark"

Benchmark.ips do |x|
  x.report("Dir.glob") do
    Dir.glob("*/*.cr") do
    end
  end
end

Results:

before: 1.73k (578.82µs) (± 2.60%)  67.3kB/op
after:  2.46k (407.11µs) (± 0.82%)  67.8kB/op
@RX14
RX14 approved these changes Aug 28, 2019
Copy link
Member

left a comment

This should really be exposed in the public API. This is really quite an especially ugly optimization.

Is this optimization worth rushing and not designing the API properly? I'm not sure. But I'm fine to approve it if an issue is created for adding the API to Dir.

@asterite asterite added this to the 0.31.0 milestone Aug 28, 2019
@asterite asterite merged commit 36a7557 into crystal-lang:master Aug 28, 2019
5 checks passed
5 checks passed
ci/circleci: check_format Your tests passed on CircleCI!
Details
ci/circleci: test_darwin Your tests passed on CircleCI!
Details
ci/circleci: test_linux Your tests passed on CircleCI!
Details
ci/circleci: test_linux32 Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.