-
Notifications
You must be signed in to change notification settings - Fork 205
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
Eagerly require date
#695
Eagerly require date
#695
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any test, so it's hard to confirm it's indeed the cause.
lib/psych/class_loader.rb
Outdated
@@ -2,6 +2,8 @@ | |||
require_relative 'omap' | |||
require_relative 'set' | |||
|
|||
autoload :Date, "date" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should set consts on Object like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you rather change it to a require 'date'
statement at the top-level?
I tried to write some tests but unfortunately |
You can probably use a subprocess. |
I was able indeed to add a test case with this technique, thanks for the suggestion |
There's a test helper for that: But no worries, I'll take care of it. I need to run some errand but I'll fix this in a couple hours. Also I think we should just stop lazy requiring libraries like that, it's stupid. |
test/psych/test_string.rb
Outdated
@@ -223,6 +223,13 @@ def test_string_with_ivars | |||
assert_equal ivar, food.instance_variable_get(:@we_built_this_city) | |||
end | |||
|
|||
def test_string_matching_date | |||
assert_separately(%w[-r ./lib/psych], <<~RUBY) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_separately(%w[-r ./lib/psych], <<~RUBY) | |
assert_separately(%w[-I lib -r psych], <<~RUBY) |
I think you need to pass -I
otherwise it risks loading the extension from stdlib.
lib/psych/class_loader.rb
Outdated
@@ -1,6 +1,7 @@ | |||
# frozen_string_literal: true | |||
require_relative 'omap' | |||
require_relative 'set' | |||
require 'date' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably should declare date
in the gemspec?
a50d82e
to
3de6c27
Compare
I updated your PR. cc @tenderlove what do you think? |
3de6c27
to
b2aa003
Compare
@@ -75,6 +75,8 @@ DESCRIPTION | |||
s.add_dependency 'stringio' | |||
end | |||
|
|||
s.add_dependency 'date' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that make sense for the stdlib?
Thanks for wrapping it up |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to do this? The change seems fine, I'm just not following why.
Right, sorry Aaron, I should have explained. In elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/.match?(o)
style = Nodes::Scalar::SINGLE_QUOTED So we end up in: class_loader.date.strptime(string, '%F', Date::GREGORIAN) Which further end up trying Hence why the initial version of this PR was to move the autoload at the top level, but at that point I think we should just eager load. |
@byroot ah, gotcha. I somewhat worry this might have downstream effects (maybe on Bundler or RubyGems). But I like the change so I'll merge it. |
Fixes #694.
When picking up only the test changes (and not
require 'date'
explicitly anymore), such failures start to appear:The
autoload :Date
statement gets moved to a more appropriate file (the one actually loading the constant), and moved to the top-level so it requires::Date
and not try to requirePsych::ClassLoader::Date
Performance-wise, reusing the benchmark from 06db36f#diff-6a459e056cadf37665f54005bd2dde09d9ba8e66c9807eb0dc67145f9b841771R7, it seems to further improve performance quite unsignificantly:
With those changes:
On current master: