-
Notifications
You must be signed in to change notification settings - Fork 1.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
add method for zero alloc byte slice to str #2141
Conversation
Very cool! Do you foresee any dangers with this approach? Does it make it easier to accidentally modify the chunk data? |
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.
immutabilityProperty := func(b []byte) bool {
if len(b) == 0 {
return BytesToString(b) == string(b)
}
s := BytesToString(b)
b[0] = 0 // modify byte slice
return s == BytesToString(b)
}
Isn't this the opposite of immutability? If you create a string from a slice, and then you modify the slice, and then you create another string from the modified slice, the "new" string equals the "old" string. Since your slice modification wasn't a no-op, this means that the original string you created somehow changed. Right?
Yea that was poorly named. It should've been something closer to |
I don't think this would be significantly more dangerous than what we currently have. Given the memory savings it seems like a worthwhile tradeoff imo. |
I am extremely wary of this. You've created a new, boring-looking function with an extremely non-obvious restriction on its use - namely, that the passed-in slice must not be mutated at all after the function returns. (This restriction is so non-obvious that you've even written a test that explicitly violates it. Other maintainers don't stand a chance!) And the consequences of breaking this restriction are unknowable and potentially catastrophic, because breaking it means breaking internal assumptions that the go runtime makes. A function like that might be possible to get away with if it were only called from a small number of very tightly controlled places. But it's not - this function was created to be used in the part of the application that is largely community-written and -managed. This feels like passing out lighters during a fireworks factory tour. |
Description:
Replace all detector byte slice to string operations with a zero alloc alternative.
![Screenshot 2023-11-06 at 6 57 43 AM](https://private-user-images.githubusercontent.com/21311841/287113422-138ba149-a015-44ca-a6af-d4260c87b300.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk5Mjg4OTcsIm5iZiI6MTczOTkyODU5NywicGF0aCI6Ii8yMTMxMTg0MS8yODcxMTM0MjItMTM4YmExNDktYTAxNS00NGNhLWE2YWYtZDQyNjBjODdiMzAwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE5VDAxMjk1N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTc1ZmRmZDgzNzZmZGNhNGI2MzY2ZDI5YzBhZDNhNmZjYzk5YzhkODQ2NWI1Nzk0YTE0Yjc4NGMzMWEzY2YyMmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Y_Jb7VqHPCjpoxeYM8TMGy4nKzghoiRvsxtwg2-USc4)
Checklist:
make test-community
)?make lint
this requires golangci-lint)?