Mercurial > hg
changeset 16397:f0f7f3fab315
util: create bytecount array just once
This avoids tons of gettext calls on workloads that call bytecount a lot.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Thu, 12 Apr 2012 20:22:18 -0500 |
parents | ee163a9cf37c |
children | def789752b60 |
files | mercurial/util.py |
diffstat | 1 files changed, 14 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/util.py Tue Apr 10 16:53:29 2012 -0500 +++ b/mercurial/util.py Thu Apr 12 20:22:18 2012 -0500 @@ -1167,23 +1167,23 @@ except (UnicodeDecodeError, UnicodeEncodeError): return _ellipsis(text, maxlength)[0] +_byteunits = ( + (100, 1 << 30, _('%.0f GB')), + (10, 1 << 30, _('%.1f GB')), + (1, 1 << 30, _('%.2f GB')), + (100, 1 << 20, _('%.0f MB')), + (10, 1 << 20, _('%.1f MB')), + (1, 1 << 20, _('%.2f MB')), + (100, 1 << 10, _('%.0f KB')), + (10, 1 << 10, _('%.1f KB')), + (1, 1 << 10, _('%.2f KB')), + (1, 1, _('%.0f bytes')), + ) + def bytecount(nbytes): '''return byte count formatted as readable string, with units''' - units = ( - (100, 1 << 30, _('%.0f GB')), - (10, 1 << 30, _('%.1f GB')), - (1, 1 << 30, _('%.2f GB')), - (100, 1 << 20, _('%.0f MB')), - (10, 1 << 20, _('%.1f MB')), - (1, 1 << 20, _('%.2f MB')), - (100, 1 << 10, _('%.0f KB')), - (10, 1 << 10, _('%.1f KB')), - (1, 1 << 10, _('%.2f KB')), - (1, 1, _('%.0f bytes')), - ) - - for multiplier, divisor, format in units: + for multiplier, divisor, format in _byteunits: if nbytes >= divisor * multiplier: return format % (nbytes / float(divisor)) return units[-1][2] % nbytes