-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
vlib: refactor empty string checks #21300
vlib: refactor empty string checks #21300
Conversation
The problem with this is that string comparisons are much slower than int comparisons. This check is very quick, but will still be slower with the change. And lots of little slowdowns can add up to a large slowdown overall. |
I couldn't find the check for |
No doubt it makes sense when the string needs to be a certain length to check for then |
As I said, it's a very small difference, but it is there. Just look at line 732 in First, it checks to see if the All that, to compare 2 empty strings. Whereas a comparison of 2 ints is... simple and straight-forward. A single CPU instruction on most systems. |
As far as I can interpret it's made sure that the string len is compared when using the import time
str := ''
non_empty := 'This is my non empty string'
sw := time.new_stopwatch()
for _ in 0 .. 1_000_000 {
if str.len == 0 {
assert true
}
if non_empty.len != 0 {
assert true
}
}
dump(sw.elapsed())
sw2 := time.new_stopwatch()
for _ in 0 .. 1_000_000 {
if str == '' {
assert true
}
if non_empty != '' {
assert true
}
}
dump(sw2.elapsed()) |
It would require an extended test to tell if there really is significance to it. The one I wrote is to simple. If there is performance difference and it is just some nano seconds the benefits of string comparison might still outweigh it. |
A decent optimizer will reduce both checks to almost nothing... if possible. It still might be useful to do the same checks with |
The conditions generate the same C 0 len check... There can't be a difference. |
How about other backends? |
Need to check. If the optimal is not generated for other backends what I already think is that we should address it in the backends. |
The I agree, that if it is not, it should be done for the other backends too. Another alternative, is that it can be reimplemented in the transformer stage, since it is a very mechanical and easy to automate rule. Then all backends will get it. |
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.
Good work.
@medvednikov what do you think of having a vet rule, for finding |
Instead of checking empty strings via
str_var.len > 0
,str_var.len == 0
,str_var.len < 1
the PR moves towards using
str_var != ''
andstr_var == ''
.The former is longer to write, has no performance benefit, and, being less expressive, it comes with more mental overhead and requires more context to comprehend what (type) the condition checks. So imho making it unnecessary hard.
If a PR for
v vet
has a chance, I'd give it a shot implementing it. Imho it would be fitting to notify about this.