Thu, 31 Jan
Why PSGI uses an array reference instead of a string in a response body?
I tweeted this question and got an answer from the designer, @miyagawa.
@kzys read PSGI::FAQ?
— Tatsuhiko Miyagawa (@miyagawa) January 31, 2013
@miyagawa Sorry. I want to know the motivation to choose an arrayref instead of a string in the protocol.
— Kato Kazuyoshi (@kzys) January 31, 2013
@kzys there's not much, except a) you can make sure it's an array ref by just checking ref eq 'ARRAY' (in case of strings ref is '')
— Tatsuhiko Miyagawa (@miyagawa) January 31, 2013
@kzys b) there's an optimization chance for sending large chunks without concatenating (like you suggested)
— Tatsuhiko Miyagawa (@miyagawa) January 31, 2013
@miyagawa Make sense. Thanks.
— Kato Kazuyoshi (@kzys) January 31, 2013
In WSGI (PEP 333), an response body is an iterable.
When called by the server, the application object must return an iterable yielding zero or more strings. This can be accomplished in a variety of ways, such as by returning a list of strings, or by the application being a generator function that yields strings, or by the application being a class whose instances are iterable. Regardless of how it is accomplished, the application object must always return an iterable yielding zero or more strings.
And the spec recommends a single-element list containg a single string.
The corresponding approach in WSGI is for the application to simply return a single-element iterable (such as a list) containing the response body as a single string. This is the recommended approach for the vast majority of application functions, that render HTML pages whose text easily fits in memory.
mod_wsgi explains what happens when you return a string instead of a list.
The worst case of this is where instead of returning a list containing strings, a single string is returned. The problem with a string is that when it is iterated over, a single character of the string is yielded each time.
The Twitter Stack
Johan Oskarsson explained about their stack from Finagle to jvmgcprof.