Author: Maciej Fijalkowski <[email protected]>
Branch: rgc-mem-pressure
Changeset: r48697:92885c7cf7b6
Date: 2011-11-03 15:02 +0100
http://bitbucket.org/pypy/pypy/changeset/92885c7cf7b6/
Log: optimize it slightly. not look up the dictionary each time we see
_digest_size
diff --git a/pypy/module/_hashlib/interp_hashlib.py
b/pypy/module/_hashlib/interp_hashlib.py
--- a/pypy/module/_hashlib/interp_hashlib.py
+++ b/pypy/module/_hashlib/interp_hashlib.py
@@ -21,9 +21,11 @@
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,
@@ -31,7 +33,7 @@
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):
@@ -75,29 +77,29 @@
"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.
@@ -111,12 +113,14 @@
'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,
@@ -124,6 +128,7 @@
'sha384': 128, 'SHA384': 128,
'sha512': 128, 'SHA512': 128,
}.get(self.name, 0)
+ return self._block_size
W_Hash.typedef = TypeDef(
'HASH',
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit