[web-devel] Caching the System time

Simon Meier iridcode at gmail.com
Fri Aug 19 14:13:30 CEST 2011

Hi Kazu,

2011/8/19 Kazu Yamamoto <kazu at iij.ad.jp>:
> Hello Simon,
>> if you're going for such a solution, then why not use difference lists
>> or even better bytestring builders as your representation of the
>> not-yet-flushed logging journal? Bytestring builders (from the
>> blaze-builder library) support a fast append and fast serialization
>> from a number of formats to bytestring.
> Difference lists are not necessary at this moment because a list is
> generated at once in the apacheFormat:
>        http://hackage.haskell.org/packages/archive/wai-logger/0.0.0/doc/html/src/Network-Wai-Logger-Format.html#apacheFormat
> If my understanding is correct, blaze-builder does not help
> hPutLogStr. What I want to do is directly copy ByteString or List to
> *Handle's buffer*.
>        http://hackage.haskell.org/packages/archive/wai-logger/0.0.0/doc/html/src/Network-Wai-Logger-IO.html#hPutLogStr

I see. Currently, builders cannot be executed directly on a Handle's
buffer. This is functionality I wanted to have for a long time, but
have not gotten around to implement it.

Using bytestring builders you could avoid creating the intermediate
[LogStr] list. You should get a performance benefit, when describing
your log-message directly as a mapping to a builder and executing this
builder on the handle's buffer, as this avoids the indirections from
the list- and the LogStr-cells. Copying the byteStrings directly also
works for builders using the 'copyByteString' function. You would get
a further performance benefit, if you could avoid creating
intermediate String values. For example, the new builder in the
bytestring library provides functions for the decimal encoding of
numbers directly into the output buffer using a fast C-implementation.

The development repository of the new bytestring builders is available
here [1]. Its API is finished, benchmarks look good, and a
documentation draft exists. Hence, it would be cheap to give it a go
and see how fast you could produce the log-messages using the new
bytestring builders. I'd use criterion to compare

  mapM_ yourLogMessageWriter logMessageList


  whnf (L.length . toLazyByteString . mconcat . map
builderLogMessageWriter) logMessageList


  logMessageList :: [(ZonedDate, Request, Status, Maybe Integer)]
  logMessageList = replicate 10000 ( your-message-params)

This should be a fair comparison, as both implementations work on
similarly large buffers. If that shows that builders are beneficial,
then we can think about implementing output on a Handle's buffer

best regards,

PS: The new bytestring builder will very likely be released with the
next GHC in November.

[1] https://github.com/meiersi/bytestring

More information about the web-devel mailing list