Commits

Maciej Fijalkowski committed 474c71d

optimize it slightly. not look up the dictionary each time we see _digest_size

  • Participants
  • Parent commits b281579
  • Branches rgc-mem-pressure

Comments (0)

Files changed (1)

File pypy/module/_hashlib/interp_hashlib.py

 
 class W_Hash(Wrappable):
     ctx = lltype.nullptr(ropenssl.EVP_MD_CTX.TO)
+    _block_size = -1
 
     def __init__(self, space, name):
         self.name = name
+        self.digest_size = self.compute_digest_size()
 
         # Allocate a lock for each HASH object.
         # An optimization would be to not release the GIL on small requests,
         self.lock = Lock(space)
 
         ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw')
-        rgc.add_memory_pressure(HASH_MALLOC_SIZE + self._digest_size())
+        rgc.add_memory_pressure(HASH_MALLOC_SIZE + self.digest_size)
         self.ctx = ctx
 
     def initdigest(self, space, name):
         "Return the digest value as a string of hexadecimal digits."
         digest = self._digest(space)
         hexdigits = '0123456789abcdef'
-        result = StringBuilder(self._digest_size() * 2)
+        result = StringBuilder(self.digest_size * 2)
         for c in digest:
             result.append(hexdigits[(ord(c) >> 4) & 0xf])
             result.append(hexdigits[ ord(c)       & 0xf])
         return space.wrap(result.build())
 
     def get_digest_size(self, space):
-        return space.wrap(self._digest_size())
+        return space.wrap(self.digest_size)
 
     def get_block_size(self, space):
-        return space.wrap(self._block_size())
+        return space.wrap(self.compute_block_size())
 
     def _digest(self, space):
         with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
             with self.lock:
                 ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
-            digest_size = self._digest_size()
+            digest_size = self.digest_size
             with lltype.scoped_alloc(rffi.CCHARP.TO, digest_size) as digest:
                 ropenssl.EVP_DigestFinal(ctx, digest, None)
                 ropenssl.EVP_MD_CTX_cleanup(ctx)
                 return rffi.charpsize2str(digest, digest_size)
 
-    def _digest_size(self):
+    def compute_digest_size(self):
         # XXX This isn't the nicest way, but the EVP_MD_size OpenSSL
         # XXX function is defined as a C macro on OS X and would be
         # XXX significantly harder to implement in another way.
             'sha512': 64, 'SHA512': 64,
             }.get(self.name, 0)
 
-    def _block_size(self):
+    def compute_block_size(self):
+        if self._block_size != -1:
+            return self._block_size
         # XXX This isn't the nicest way, but the EVP_MD_CTX_block_size
         # XXX OpenSSL function is defined as a C macro on some systems
         # XXX and would be significantly harder to implement in
         # XXX another way.
-        return {
+        self._block_size = {
             'md5':     64, 'MD5':     64,
             'sha1':    64, 'SHA1':    64,
             'sha224':  64, 'SHA224':  64,
             'sha384': 128, 'SHA384': 128,
             'sha512': 128, 'SHA512': 128,
             }.get(self.name, 0)
+        return self._block_size
 
 W_Hash.typedef = TypeDef(
     'HASH',