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

is it possible to provide an 'unsafe' way to get internal []byte and avoid mem copy? #404

Open
codingWhat opened this issue Nov 4, 2024 · 2 comments

Comments

@codingWhat
Copy link

codingWhat commented Nov 4, 2024

What is the issue you are having?

In some scenarios, the data retrieved from the cache will not be modified and could be directly returned.

What is BigCache doing that it shouldn't?

func readEntry(data []byte) []byte {
	length := binary.LittleEndian.Uint16(data[timestampSizeInBytes+hashSizeInBytes:])

	// copy on read
	dst := make([]byte, len(data)-int(headersSizeInBytes+length))
	copy(dst, data[headersSizeInBytes+length:])

	return dst
}

to be --- to recycle the 'dst'

func readEntry(dst, data []byte) []byte {
	length := binary.LittleEndian.Uint16(data[timestampSizeInBytes+hashSizeInBytes:])

	copy(dst, data[headersSizeInBytes+length:])

	return dst
}
@codingWhat codingWhat added the bug label Nov 4, 2024
@janisz janisz added enhancement and removed bug labels Nov 14, 2024
@janisz
Copy link
Collaborator

janisz commented Nov 14, 2024

That makes sense, so we could include Unsafe get methods. I'm curious about the best way to approach this. Instead of modifying the existing bigcache interface, maybe we could create an alternative implementation, like UnsafeBigCache, where all method signatures would remain unchanged. 🤔

However, there’s a downside to consider: this approach might lead to unintended effects when data is overwritten without a way to notify the user.

Also, I'm wondering why we need to copy to dst — can’t we just return data[headersSizeInBytes+length:] directly?

@codingWhat
Copy link
Author

codingWhat commented Nov 17, 2024

I totally agree with you .

I think adding a GetWithUnsafe is enough, and any user who calls this method must take responsibility for data security and ensure that it is not modified.

look forward to this enhancement..😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants