-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
cmd/compile: teach prove.go that {strings|bytes}.Index* return value in the range [-1 .. len(s)) #25862
Comments
cc'ing @aclements and @rasky , since they recently worked on |
I don't know how much we want to teach the compiler about specific std functions. It'd be nice if there were some way to infer this instead, although I don't see how. Note that if someone was playing around with these functions in the standard library and added -2 as a special signal value, any calling code might be mis-compiled. |
Probably, we could annotate return values for functions written in assembly. Such annotations may contain valid ranges for the returned function values. These annotations must be restricted only to assembly-written functions, since in theory the compiler may infer return value ranges for ordinary Go functions using the code from Additionally, I think the underlying functions for
So it would be OK adding nasty optimizations like this one for these functions.
This will violate strings.IndexByte contract, which explicitly states it returns values in the range The compiler may have a special |
Punting to 1.13, too late for anything major in 1.12. |
The issue
Currently prove.go doesn't know that functions like
strings.Index*
always return value in the range[-1 .. len(s))
wheres
- input string / byte slice. So it emits unnecessary bounds checks in the following code:go tip compiler generates the following assembly code for
nextSpace
:The solution
{strings|bytes}.Index*
return value in the range[-1 .. len(s))
&s[0] + len(s) - n
doesn't exceed max word value if0 <= n < len(s)
This should improve performance of various parsers for text-based messages (json, xml, html, http, etc.)
The text was updated successfully, but these errors were encountered: