-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 ByteString#substring methods #113
Conversation
} | ||
|
||
byte[] copy = new byte[subLen]; | ||
System.arraycopy(data, beginIndex, copy, 0, subLen); |
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.
Since these are immutable we should just return a view of the original data rather than require a copy.
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.
That could cause a memory leak by keeping the potentially huge and unwanted original array around.
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.
The inverse is also true: this can cause massive amounts of useless objects by copying data that we know will never change.
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.
True, this will be a much bigger change in that case, since we'll have to keep track of the length and offset on the underlying array manually.
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.
I think there's advantages in re-using the underlying storage. For example, we read headers using a ByteString
and it would be advantageous to do a zero-copy "split" it into name and value. Perhaps we document the fact that it's a view and require you to call .clone()
if you want a defensive copy?
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.
For what it's worth, I think the copy is less likely to surprise.
Add ByteString#substring methods
Thanks! |
Ref #112