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 ByteString#substring methods #113

Merged
merged 1 commit into from
Feb 3, 2015
Merged

Add ByteString#substring methods #113

merged 1 commit into from
Feb 3, 2015

Conversation

f2prateek
Copy link
Contributor

Ref #112

}

byte[] copy = new byte[subLen];
System.arraycopy(data, beginIndex, copy, 0, subLen);
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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?

Copy link
Collaborator

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.

swankjesse added a commit that referenced this pull request Feb 3, 2015
Add ByteString#substring methods
@swankjesse swankjesse merged commit 654ddf5 into square:master Feb 3, 2015
@swankjesse
Copy link
Collaborator

Thanks!

@f2prateek f2prateek deleted the substring branch May 31, 2015 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants