-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix limit memory usage of XML::Error.errors
#14966
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
Closed
straight-shoota
wants to merge
5
commits into
crystal-lang:master
from
straight-shoota:fix/xml-errors-limit
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a25c59
Fix limit memory usage of `XML::Error.errors`
straight-shoota 2de986a
Resolve `class_property` to avoid `Deprecated` annotation on the clas…
straight-shoota 7f54377
Test spec
straight-shoota b889c0f
Fix test
straight-shoota 2e1e8bd
make format
straight-shoota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -11,19 +11,36 @@ class XML::Error < Exception | |
| super(message) | ||
| end | ||
|
|
||
| @@errors = [] of self | ||
| @@max_error_capacity = 5 | ||
|
|
||
| @[Deprecated("This class property is deprecated. XML errors are accessible directly in the respective context via `XML::Reader#errors` and `XML::Node#errors`.")] | ||
| def self.max_error_capacity=(@@max_error_capacity) | ||
| end | ||
|
|
||
| @[Deprecated("This class property is deprecated. XML errors are accessible directly in the respective context via `XML::Reader#errors` and `XML::Node#errors`.")] | ||
beta-ziliani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def self.max_error_capacity | ||
| @@max_error_capacity | ||
| end | ||
|
|
||
| @@errors = Deque(self).new(max_error_capacity) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: this initializer will run before user code, won't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argh, yeah it will 🤦 |
||
|
|
||
| # :nodoc: | ||
| protected def self.add_errors(errors) | ||
| @@errors.concat(errors) | ||
| new_errors_size = errors.size.clamp(..max_error_capacity) | ||
| remaining_size = max_error_capacity - new_errors_size | ||
| (@@errors.size - remaining_size).times { @@errors.shift } | ||
|
|
||
| errors.to_unsafe.to_slice(errors.size)[-new_errors_size, new_errors_size].each do |error| | ||
| @@errors.push error | ||
| end | ||
| end | ||
|
|
||
| @[Deprecated("This class accessor is deprecated. XML errors are accessible directly in the respective context via `XML::Reader#errors` and `XML::Node#errors`.")] | ||
| def self.errors : Array(XML::Error)? | ||
| if @@errors.empty? | ||
| nil | ||
| else | ||
| errors = @@errors.dup | ||
| errors = @@errors.to_a | ||
| @@errors.clear | ||
| errors | ||
| end | ||
|
|
||
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.
Question: if I want to disable the behavior, which is the way forward, I have to set it this value to 0, but then I get a deprecation notice 😕
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.
Hm, I don't think you should actually need to disable it explicitly.
You should of course be able to do that if you want. And in that case this method should not raise a deprecation message. But we want to get rid of this method as well, so it should still be deprecated.
Maybe we should mark it as
Experimentalinstead? 🤔Or don't annotate it at all but document it that it should always be guarded by
compare_version(Crystal::VERSION, "1.13.2") > 0) && compare_version(Crystal::VERSION, "2.0.0") < 0).