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 Buf <-> Array[Byte] bijection #208

Merged
merged 3 commits into from
May 6, 2015
Merged

Conversation

dschobel
Copy link
Contributor

This PR defines a bijection between Array[Byte] and com.twitter.io.Buf with a bonus version bump of the util-core lib.


implicit def byteArrayBufBijection: Bijection[Array[Byte], Buf] =
new AbstractBijection[Array[Byte], Buf] {
override def apply(bytes: Array[Byte]) = Buf.ByteArray.Owned(bytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk should this be owned or shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ChannelBuffer bijection wraps the byte array on application and copies on extraction so this jives with the wrapping half but not the extraction.

I can see a case for defining both versions and letting folks import the semantics they prefer? otoh, if users care about defensive copies, they can fairly trivially compose that onto any use of this version.

I do think making the shared impl the default is a mistake since this code tends to be used for serialization in batch jobs and as the hyper-doop folks showed is frequently a performance bottleneck.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe we should namespace them slightly differently and supply both? I feel like the point of Owned vs Shared was that we wanted folks to have to think about what it was OK to do with the data, so maybe we should do it here too (since we intend for this to be an extension of our API).

@ianoc
Copy link
Collaborator

ianoc commented Apr 21, 2015

Looks fine to me, but best to resolve with Moses about Owned or Shared, I don't have enough context on Buf to know the best answer there.

@@ -122,6 +123,22 @@ trait UtilBijections {
override def apply(pool: FuturePool) = new TwitterExecutionContext(pool)
override def invert(context: ExecutionContext) = new ScalaFuturePool(context)
}

object Owned {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get some comments here to explain the differences? I guess in one case (shared?) you are copying the underlying buffer, but in the other you are not, which only makes it safe if you "own" the Buf/Array[Byte] in question. Is that right?

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's right, it describes ownership of the underlying byte array with implications of defensive copying. I'll add a note.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we now have objects Owned and shared under twitter_util, the namespacing there doesn't make too much sense. A sub package or object to put them into a Buf namespace so it aligns more with util I think would be good here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, could we add Shared / Owned type aliases, and then use different type classes for .as[Shared] vs .as[Owned] or is scala too smart for that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that'll work anyway -- You could though add them in as AnyVal case classes here, and make the injections be to those. It would be a similar strategy as to how the Base64String stuff is done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like:

case class Owned(bytes: Array[Byte]) extends AnyVal
case class Shared(bytes: Array[Byte]) extends AnyVal

then you can do buf.as[Shared].bytes if we want to go that way. I kind of like usage of types for this kind of decoration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those wouldn't work for APIs that need to take Bufs though, right? You'd need to do bytes.as[Owned].unwrap, which is practically as bad as

val Buf.Owned(bytes) = buf
val buf = Buf.Owned(bytes)

I think @ianoc's idea for a Buf.Owned namespace is reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mosesn / @johnynek / @ianoc everybody happy with a com.twitter.bijection.twitter_util.Buf namespace to hold the Shared and Owned objects?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, good with me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me

johnynek added a commit that referenced this pull request May 6, 2015
add Buf <-> Array[Byte] bijection
@johnynek johnynek merged commit 512ed54 into twitter:develop May 6, 2015
@johnynek
Copy link
Collaborator

johnynek commented May 6, 2015

Thanks @dschobel ! we're going to get this out in the next few days.

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.

None yet

4 participants