ceylon / ceylon.language

DEPRECATED
Apache License 2.0
153 stars 57 forks source link

Resource.binaryContent() #521

Open gavinking opened 10 years ago

gavinking commented 10 years ago

Should we add:

{Byte*} binaryContent()

To Resource?

@chochos how hard would it be to implement this in JS?

How useful would it be?

chochos commented 10 years ago

I can make it work for node, not sure about browser. node has its own Buffer type, I'll check how hard it is to convert that into our Bytes.

I don't think it'll be very useful in JS because you usually don't deal directly with binary resources (images, video, whatever), you just pass their URLs to the browser. But in JVM it will surely be a lot more useful.

gavinking commented 10 years ago

Yeah, let's leave it out for now. I imagine we're going to need this as some point, but probably not right now.

quintesse commented 10 years ago

Not implementing it now would severely limit the usefulness of resources on the JVM side. I'd rather implement it there and leave the JS version unimplemented for the moment than not implement it at all.

quintesse commented 9 years ago

@tombentley I guess that to make this {Byte*} return value as efficient as possible it should be based on a ByteArray, right? Because things like for-comprehensions have special cases for them to generate better code IIRC?

tombentley commented 9 years ago

I don't think we can know what would be "as efficient as possible" without testing. And anyway I think you're thinking of the optimizations in for loops. We don't currently have any similar optimizations for comprehensions.

Using an array necessarily means eagerly allocating, whereas the type {Byte*} allows us allocate a smaller byte[] or `ByteBuffer, or whatever. If we had figures to justify it then we could use a for-loop optimizable byte[] for small resources and the buffering approach for anything over a certain threshold, but in the first instance I'd just go for the buffering approach 'cos it's simplest.

quintesse commented 9 years ago

Sorry, you're right of course, but I wasn't giving enough information. I was in this case talking about a situation where we already have the complete contents available as a byte array.

I do think that we can safely assume that some kind of optimization will help for the other "streaming" or buffered cases because otherwise we'll be doing boxing for each and every byte that comes through. And I fear the only way we can measure that is by first trying it somehow.

But for now I'd go with the simplest implementation indeed. Premature optimization and all that.

quintesse commented 9 years ago

On the mailing list @FroMage suggested this signature:

Array<Byte> binaryContent(Integer start=0, Integer length=-1)