-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Improve error messages for InvalidByteSequenceError #2814
Conversation
It's a good idea, but I'm not sure we should do this. Try this benchmark: require "benchmark"
io = MemoryIO.new
io << "foo bar"
io << "今日"
string = io.to_s
time = Time.now
a = 0
20_000_000.times do
string.each_char do |char|
a += char.ord
end
end
puts a
puts Time.now - time Before: 00:00:01.7733200 Basically, doing string interpolation and more work, even if an exception is not raised, makes the optimizer not inline some things and generate more, slower code. That's also the reason why |
Not being helpful is a terrible solution though. What if we make |
Hm, in fact, I tried adding |
Actually I'm not sure I even understand why it's slower, I mean unless the error condition is actually reached the code shouldn't run? |
Never mind. With |
Cool so merge and add the And we actually can make |
You can try, yes, but do some benchmarks to see if the performance doesn't degrade. |
Let's merge this, then I'll rebase and merge #2816, and then add the attribute. |
require "benchmark"
class Array
@[AlwaysInline]
def at_detail(index : Int)
at(index) { raise IndexError.new("Index out of bounds: #{index} is bigger than #{size-1}") }
end
end
a = [1, 2, 3]
Benchmark.ips do |x|
x.report("Array#at(1)") { a.at(1) }
x.report("Array#at_detail(1)") { a.at_detail(1) }
x.report("Array#at(4)") { a.at(4) rescue nil }
x.report("Array#at_detail(4)") { a.at_detail(4) rescue nil }
end
No significant difference in the average case, the verbose version is even a bit faster in this benchmark arrangement due to the better cache locality, the results are swapped if the order of the benchmark is swapped, all within error margin anyway. Do I still have to benchmark the other |
Looks really good! :-) 👍 |
No description provided.