[web-devel] Caching the System time
Simon Meier
iridcode at gmail.com
Mon Aug 22 13:00:37 CEST 2011
Hi Kazu, Piyush,
the internal interface of builders allows you to check the free buffer
space and flush early, if required. This works as long as you have
some external means to compute an upper bound on the number of bytes
that might be written. In fact, the 'Write' type in blaze-builder (in
the new bytestring builder better described as bounded-length
encodings [2]) abstracts exactly this situation. For log-files, you
could create the log-line as a 'Write' value and execute it afterwards
using 'fromWrite'. This guarantees the line-flush behaviour even for
block-buffering.
If you cannot compute an a-priori bound on the number of bytes
written, there is no way around forcing the value. If you use the
public interface of builders, this results in a number of lazy
bytestrings, which have to be concatenated afterwards with the
appropriate flushes in between. Depending on your application you may
be able to do it all in one pass using a builder-transformer that
intercepts the internal protocol between the buildsteps and the
buildstep-driver that provides the free buffers.
For example, the HTTP chunked-transfer-encoding is implemented as a
builder-transformer [2]. Before running the inner buildstep, the
builder-transformer reserves enough space to write the maximal length
of the output. Then, it starts the inner buildstep. Once the inner
buildstep returns, the correct size is written at the reserved space,
padding the hex-representation with zeroes, if necessary. Would
someting like this work in your use-case @Piyush?
best regards,
Simon
[1] https://github.com/meiersi/bytestring/blob/master/Data/ByteString/Lazy/Builder/BoundedEncoding.hs
[2] https://github.com/meiersi/blaze-builder/blob/master/Blaze/ByteString/Builder/HTTP.hs
2011/8/22 Piyush P Kurur <ppk at cse.iitk.ac.in>:
> On Mon, Aug 22, 2011 at 03:44:02PM +0900, Kazu Yamamoto wrote:
>> Simon,
>>
>> Thank you for your explanation.
>
>
>> I'm not sure that we can obtain the length of Builder without
>> building(copying).
>
> I too am interested in knowing whether the length of a builder (exact
> length) can be computed without forcing computation. The context that
> I need this is different (an asn1 library). The way I solved is to
> keep track of the length explicitly. But if builder can do that for me
> that is very good.
>
> Regards
>
> ppk
>
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel
>
More information about the web-devel
mailing list