-
Notifications
You must be signed in to change notification settings - Fork 70
Fix frozen string literal warning in magic detection #123
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
base: main
Are you sure you want to change the base?
Conversation
Tests pass on my machine and GH Actions, the Buildkite build erroring out seems unrelated and not fixable from this PR. |
@@ -119,7 +119,7 @@ def self.magic_match(io, method) | |||
|
|||
io.binmode if io.respond_to?(:binmode) | |||
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding) | |||
buffer = "".encode(Encoding::BINARY) | |||
buffer = (+"").encode(Encoding::BINARY) |
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.
Perhaps this is better for readability:
buffer = (+"").encode(Encoding::BINARY) | |
buffer = String.new.encode(Encoding::BINARY) |
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 see from the bench-marking that you did already consider that option. Also this is more succinct hence better.
buffer = (+"").encode(Encoding::BINARY) | |
buffer = String.new(encoding: Encoding::BINARY) |
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.
Please disregard!
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 think what you want here is to declare a mutable string with binary encoding. That's
String.new(encoding: Encoding::BINARY)
I’m not sure whether this actually fixes the issue I was trying to fix — tried to bundle install marcel from my repo and it still appeared — but this should still be a slight improvement |
As mentioned in the comments, rails/marcel#123 PR should make this go away. This is just for cosmetic reasons. The warning makes the test suite output look untidy.
@@ -119,7 +119,7 @@ def self.magic_match(io, method) | |||
|
|||
io.binmode if io.respond_to?(:binmode) | |||
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding) | |||
buffer = "".encode(Encoding::BINARY) | |||
buffer = (+"").encode(Encoding::BINARY) |
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.
buffer = (+"").encode(Encoding::BINARY) | |
buffer = String.new(encoding: Encoding::BINARY) |
With latest Ruby 3.4.x, using marcel emits the following warning:
lib/ruby/gems/3.4.0/gems/marcel-1.0.4/lib/marcel/magic.rb:120: warning: literal string will be frozen in the future
This occurs because the code was creating an empty string literal used as mutable buffer for I/O operations.
This pull request replaces
"".dup.encode(Encoding::BINARY)
with(+"").encode(Encoding::BINARY)
in the buffer creation. Using the unary plus operator to explicitly create a mutable string benchmarked slightly faster than.dup.encode()
on my local machine.Benchmark Results
String Creation Performance (1M iterations)
"".dup.encode()
(+"").encode()
String.new(encoding:)
Memory Allocation (100K iterations)
"".dup.encode()
(+"").encode()
String.new(encoding:)
Real-world Usage (10K magic detection operations)
"".dup.encode()
(+"").encode()
String.new(encoding:)