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
Redo finer-grained inline constant cache invalidation #5716
Merged
XrXr
merged 2 commits into
ruby:master
from
kddnewton:revert-5715-revert-5433-constant-invalidation
Apr 1, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$VERBOSE = nil | ||
|
||
CONSTANT1 = 1 | ||
CONSTANT2 = 1 | ||
CONSTANT3 = 1 | ||
CONSTANT4 = 1 | ||
CONSTANT5 = 1 | ||
|
||
def constants | ||
[CONSTANT1, CONSTANT2, CONSTANT3, CONSTANT4, CONSTANT5] | ||
end | ||
|
||
500_000.times do | ||
constants | ||
|
||
# With previous behavior, this would cause all of the constant caches | ||
# associated with the constant lookups listed above to invalidate, meaning | ||
# they would all have to be fetched again. With current behavior, it only | ||
# invalidates when a name matches, so the following constant set shouldn't | ||
# impact the constant lookups listed above. | ||
INVALIDATE = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
# Constant lookup is cached. | ||
assert_equal '1', %q{ | ||
CONST = 1 | ||
|
||
def const | ||
CONST | ||
end | ||
|
||
const | ||
const | ||
} | ||
|
||
# Invalidate when a constant is set. | ||
assert_equal '2', %q{ | ||
CONST = 1 | ||
|
||
def const | ||
CONST | ||
end | ||
|
||
const | ||
|
||
CONST = 2 | ||
|
||
const | ||
} | ||
|
||
# Invalidate when a constant of the same name is set. | ||
assert_equal '1', %q{ | ||
CONST = 1 | ||
|
||
def const | ||
CONST | ||
end | ||
|
||
const | ||
|
||
class Container | ||
CONST = 2 | ||
end | ||
|
||
const | ||
} | ||
|
||
# Invalidate when a constant is removed. | ||
assert_equal 'missing', %q{ | ||
class Container | ||
CONST = 1 | ||
|
||
def const | ||
CONST | ||
end | ||
|
||
def self.const_missing(name) | ||
'missing' | ||
end | ||
|
||
new.const | ||
remove_const :CONST | ||
end | ||
|
||
Container.new.const | ||
} | ||
|
||
# Invalidate when a constant's visibility changes. | ||
assert_equal 'missing', %q{ | ||
class Container | ||
CONST = 1 | ||
|
||
def self.const_missing(name) | ||
'missing' | ||
end | ||
end | ||
|
||
def const | ||
Container::CONST | ||
end | ||
|
||
const | ||
|
||
Container.private_constant :CONST | ||
|
||
const | ||
} | ||
|
||
# Invalidate when a constant's visibility changes even if the call to the | ||
# visibility change method fails. | ||
assert_equal 'missing', %q{ | ||
class Container | ||
CONST1 = 1 | ||
|
||
def self.const_missing(name) | ||
'missing' | ||
end | ||
end | ||
|
||
def const1 | ||
Container::CONST1 | ||
end | ||
|
||
const1 | ||
|
||
begin | ||
Container.private_constant :CONST1, :CONST2 | ||
rescue NameError | ||
end | ||
|
||
const1 | ||
} | ||
|
||
# Invalidate when a module is included. | ||
assert_equal 'INCLUDE', %q{ | ||
module Include | ||
CONST = :INCLUDE | ||
end | ||
|
||
class Parent | ||
CONST = :PARENT | ||
end | ||
|
||
class Child < Parent | ||
def const | ||
CONST | ||
end | ||
|
||
new.const | ||
|
||
include Include | ||
end | ||
|
||
Child.new.const | ||
} | ||
|
||
# Invalidate when const_missing is hit. | ||
assert_equal '2', %q{ | ||
module Container | ||
Foo = 1 | ||
Bar = 2 | ||
|
||
class << self | ||
attr_accessor :count | ||
|
||
def const_missing(name) | ||
@count += 1 | ||
@count == 1 ? Foo : Bar | ||
end | ||
end | ||
|
||
@count = 0 | ||
end | ||
|
||
def const | ||
Container::Baz | ||
end | ||
|
||
const | ||
const | ||
} | ||
|
||
# Invalidate when the iseq gets cleaned up. | ||
assert_equal '2', %q{ | ||
CONSTANT = 1 | ||
|
||
iseq = RubyVM::InstructionSequence.compile(<<~RUBY) | ||
CONSTANT | ||
RUBY | ||
|
||
iseq.eval | ||
iseq = nil | ||
|
||
GC.start | ||
CONSTANT = 2 | ||
} | ||
|
||
# Invalidate when the iseq gets cleaned up even if it was never in the cache. | ||
assert_equal '2', %q{ | ||
CONSTANT = 1 | ||
|
||
iseq = RubyVM::InstructionSequence.compile(<<~RUBY) | ||
CONSTANT | ||
RUBY | ||
|
||
iseq = nil | ||
|
||
GC.start | ||
CONSTANT = 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If
:CONST1
and:CONST2
have the positions inverted,const1
in line 108 will return1
.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.
Right,
CONST2
doesn't exist, so when it's reversedprivate_constant
raisesNameError
before changing the visibility ofCONST1
. On Ruby 3.1private_constant
behaves like this too.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.
Gotcha. Thank you for the explanation.