[web-devel] Caching the System time

Kazu Yamamoto ( 山本和彦 ) kazu at iij.ad.jp
Wed Aug 10 05:17:54 CEST 2011


Hello web-devel!

Sorry for the delay. I was on vacation.

> Kazu profiled his code and found that using the current haskell date/time
> libraries was extremely expensive. He solved the issues by writing optimized
> date code. It is unclear how much benefit there would be to caching after
> switching to his optimized date code. He is planning to release his logging
> code as a separate package.

Let me explain what I have done in Mighttpd.

1) The http-date library.

  This is for HTTP date-related fields (including Last-Modified:), not
  *logging*. To my profile, Data.Time is really slow. This library is
  based on the fact that HTTP date-related fields is *GMT only* and
  the field value is exactly 29 bytes.

2) Logging

  For the Apache style logging, we need *zoned* time. I did not want
  to implement it from scratch. So, Mighttpd issues getZonedTime every
  second, formats it with Data.Time, and caches ByteString
  representation. Yes, both getZonedTime and Data.Time are really
  slow, but Mighttpd uses them just once for every second. The caches
  is stored in IORef with atomicModifyIORef which is faster than MVar.

  Every user-thread of Mighttpd records a log message with Handle.
  Since Handle is protected with MVar, serialization is done. Multiple
  log messages are buffered in the buffer of Handle to reduce the
  number of the write system call. Moreover, I re-implemented hPut to
  reduce unnecessary intermediate data to compose a log message.

  I will try to strip this code from Mighttpd and make it as a WAI
  application(?) today.

--Kazu



More information about the web-devel mailing list