Commits

Constantin Veretennicov  committed 2098843

Replaced s.encode('base64') with binascii.a2b_base64(s) as it is about 14 times faster on my machine.

  • Participants
  • Parent commits 2d6d07e

Comments (0)

Files changed (1)

File app/models.py

-import re
+import binascii
 from google.appengine.ext import db
 from biginteger import BigIntegerProperty
 
   update_date_utc = db.DateTimeProperty(auto_now=True)
 
 
+# NOTES:
+# 1. key_names are prefixed with '#' due to DataStore key_name restrictions,
+#    see http://code.google.com/appengine/docs/python/datastore/keysandentitygroups.html#Kinds_Names_and_IDs
+# 2. binascii a2b_base64 and b2a_base64 are used as they are order of magnitude
+#    faster than s.encode('base64')
+
 class Torrent(db.Model):
-  # key_name is hash_info string prefixed with '#'
+  # key_name is info_hash string prefixed with '#'
   
   model_version = db.IntegerProperty(required=True, default=2)
   creation_date_utc = db.DateTimeProperty(auto_now_add=True)
   
   @property
   def info_hash(self):
-    return self.key().name()[1:].decode('base64')
+    return binascii.a2b_base64(self.key().name()[1:])
   
   @classmethod
   def get_by_info_hash(cls, info_hash):
   
   @classmethod
   def _encode_key_name(cls, info_hash):
-    return '#' + info_hash.encode('base64')
+    return '#' + binascii.b2a_base64(info_hash)
 
 
 class TorrentPeer(db.Model):
 
   @property
   def peer_id(self):
-    return self.key().name()[1:].decode('base64')[:20]
+    return binascii.a2b_base64(self.key().name()[1:])[:20]
 
   @classmethod
   def create(cls, peer_id, torrent, **kwargs):
 
   @classmethod
   def _encode_key_name(cls, peer_id, torrent_info_hash):
-    return '#' + (peer_id + torrent_info_hash).encode('base64')
+    return '#' + binascii.b2a_base64(peer_id + torrent_info_hash)