-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
bytes, strings: optimize Contains with fast-path for sub-slices #24979
Comments
Are we okay with using Also, I wonder how many instances like these are out in the wild ? I would guess that the programmer would already know that the slice is a child of the parent slice ? |
The
I was looking at a piece of code where the a certain string sometimes was sub-sliced from the parent, and sometimes it wasn't. At the time that the check happened, it didn't know. It's possible to add more book-keeping to hold this information, but that seems silly. |
If i recall correctly bytealg uses unsafe for linknames and to get the offset into structs that is then used in assembler code. There does not seem to be any unsafe imports in bytes. I do not think we should start using unsafe for performance improvements in normal standard lib functions and i think code reviews have been rejecting the use of unsafe for performance in normal (not reflect, runtime or similar) go std lib code in the past. I do not think this improvement should introduce the precedence to use unsafe to cast slice header internals outside of asm code and reflect/runtime packages. I would also expect the additional computation and condition check to slow down the case where it is not a subslice slightly so it might not always be a performance improvement depending on the call site. However i have no idea/data how common either case is. |
I agree this case can be made more efficient, but at some overhead to all other cases. How often does the situation arise in practice? Asking if a is inside b when a was made from b seems unnecessary in the first place. |
Consider the following:
On my machine, this prints:
In this situation, the substring is sliced out of the parent slice. It should be know that the parent contains the substring in O(1) with something similar to:
(the above code is not correct as there is special consideration to manipulating
unsafe.Pointer
, but the general approach is the same)The text was updated successfully, but these errors were encountered: