Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 7396:526c40a74bd0
templater: return data in increasing chunk sizes
Currently hgweb is not streaming its output -- it accumulates the
entire response before sending it. This patch restores streaming
behaviour. To avoid having to synchronously write many tiny fragments,
this patch also adds buffering to the template generator. Local
testing of a fetch of a 100,000 line file with wget produces a slight
slowdown overall (up from 6.5 seconds to 7.2 seconds), but instead of
waiting 6 seconds for headers to arrive, output begins immediately.
author | Brendan Cully <brendan@kublai.com> |
---|---|
date | Fri, 21 Nov 2008 15:51:40 -0800 |
parents | 00d76fa3ffba |
children | 040484030491 |
comparison
equal
deleted
inserted
replaced
7395:e2048f5c7bf5 | 7396:526c40a74bd0 |
---|---|
287 def sort(l): | 287 def sort(l): |
288 if not isinstance(l, list): | 288 if not isinstance(l, list): |
289 l = list(l) | 289 l = list(l) |
290 l.sort() | 290 l.sort() |
291 return l | 291 return l |
292 | |
293 def increasingchunks(source, min=1024, max=65536): | |
294 '''return no less than min bytes per chunk while data remains, | |
295 doubling min after each chunk until it reaches max''' | |
296 def log2(x): | |
297 if not x: | |
298 return 0 | |
299 i = 0 | |
300 while x: | |
301 x >>= 1 | |
302 i += 1 | |
303 return i - 1 | |
304 | |
305 buf = [] | |
306 blen = 0 | |
307 for chunk in source: | |
308 buf.append(chunk) | |
309 blen += len(chunk) | |
310 if blen >= min: | |
311 if min < max: | |
312 min = min << 1 | |
313 nmin = 1 << log2(blen) | |
314 if nmin > min: | |
315 min = nmin | |
316 if min > max: | |
317 min = max | |
318 yield ''.join(buf) | |
319 blen = 0 | |
320 buf = [] | |
321 if buf: | |
322 yield ''.join(buf) | |
292 | 323 |
293 class Abort(Exception): | 324 class Abort(Exception): |
294 """Raised if a command needs to print an error and exit.""" | 325 """Raised if a command needs to print an error and exit.""" |
295 | 326 |
296 class UnexpectedOutput(Abort): | 327 class UnexpectedOutput(Abort): |