# HG changeset patch # User Matt Mackall # Date 1334280138 18000 # Node ID f0f7f3fab31528e75dc3bbd852fd3c23ec3316af # Parent ee163a9cf37c5783b7707f3264e3be901255ce25 util: create bytecount array just once This avoids tons of gettext calls on workloads that call bytecount a lot. diff -r ee163a9cf37c -r f0f7f3fab315 mercurial/util.py --- 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