socketry / async-http

MIT License
298 stars 45 forks source link

Should chunks be frozen by default? #11

Closed ioquatix closed 6 years ago

ioquatix commented 6 years ago

@janko-m I've been thinking about https://github.com/socketry/falcon/issues/12 and the potential solution of freezing the buffer.

I wonder if all chunks traversing through body instances should be frozen by default.

This is especially relevant w.r.t. rewindable input. If you can rewind the input but the chunks are modified, it might be unexpected. We don't want users unexpectedly modifying source buffers which might be reused.

One option would be to freeze all input to Body::Writable by default. In terms of the contract of that method, once you pass your string into it, it's no longer yours and you shouldn't touch it in any way. That would be enforced by calling freeze.

It seems like doing this would also reduce the chance for extra hidden allocations in things like #byteslice and friends.

Thoughts?

ioquatix commented 6 years ago

Okay, I've come to the conclusion that this isn't a good idea, but in practice there are some areas where we can leverage Ruby's shared strings/COW by #freeze before #byteslice.