Ticket #2310 enhancement closed fixed
twisted.spread.banana.b1282int could be faster
| Reported by: | exarkun | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | |
| Component: | pb | Keywords: | |
| Cc: | catlee | Branch: | branches/faster-b1282int-2310 |
| Author: | catlee | Launchpad Bug: |
Description
Banana calls this function a lot. It might be a bottleneck, who knows. Someone should profile it. I forget if I did. I found some code that apparently I wrote a while ago, though (patch reversed):
Index: twisted/spread/banana.py
===================================================================
--- twisted/spread/banana.py (revision 18957)
+++ twisted/spread/banana.py (working copy)
@@ -35,19 +35,18 @@
stream(chr(integer & 0x7f))
integer = integer >> 7
-def b1282int(st):
- oneHundredAndTwentyEight = 128l
+
+def b1282int(st, _powersOfOneTwentyEight = []):
i = 0
- place = 0
- for char in st:
+ if len(st) > len(_powersOfOneTwentyEight):
+ _powersOfOneTwentyEight.extend([
+ 128 ** n for n in xrange(len(_powersOfOneTwentyEight), len(st))])
+ for place, char in enumerate(st):
num = ord(char)
- i = i + (num * (oneHundredAndTwentyEight ** place))
- place = place + 1
- if i <= 2147483647:
- return int(i)
- else:
- return i
+ i = i + (num * _powersOfOneTwentyEight[place])
+ return i
+
# delimiter characters.
LIST = chr(0x80)
INT = chr(0x81)
The strategy is simple: cache powers instead of recomputing them all the time. This drastically speeds up the function for multiple invocations.
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

