Skip to content
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

Closed
wants to merge 7 commits into from

Conversation

ahrav
Copy link
Collaborator

@ahrav ahrav commented Dec 1, 2023

Description:

Replace all detector byte slice to string operations with a zero alloc alternative.
Screenshot 2023-11-06 at 6 57 43 AM

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@ahrav ahrav marked this pull request as ready for review December 1, 2023 00:43
@ahrav ahrav requested review from a team as code owners December 1, 2023 00:43
@mcastorina
Copy link
Collaborator

Very cool! Do you foresee any dangers with this approach? Does it make it easier to accidentally modify the chunk data?

Copy link
Collaborator

@rosecodym rosecodym left a 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?

@ahrav
Copy link
Collaborator Author

ahrav commented Dec 1, 2023

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 TestBytesToString_SharedDataConsistency .

@ahrav ahrav requested a review from rosecodym December 1, 2023 14:47
@ahrav
Copy link
Collaborator Author

ahrav commented Dec 1, 2023

Very cool! Do you foresee any dangers with this approach? Does it make it easier to accidentally modify the chunk data?

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.

@rosecodym
Copy link
Collaborator

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.

@ahrav ahrav closed this Feb 11, 2024
@ahrav ahrav deleted the zero-alloc-byte-slice-to-string branch February 11, 2024 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants