Skip to content

Commit 503c7b6

Browse files
committed
Some improvements in cachedmethods
1 parent 1614084 commit 503c7b6

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

data/txt/sha256sums.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ f6062e324fdeaacf9df0a289fc3f12f755143e3876a70cb65b38aa2e690f73c1 lib/core/commo
171171
39ea62d4224be860befeffb3843c150f2343b64555ad8c438a400222056f6cc0 lib/core/convert.py
172172
ae500647c4074681749735a4f3b17b7eca44868dd3f39f9cab0a575888ba04a1 lib/core/data.py
173173
ffae7cfe9f9afb92e887b9a8dbc1630d0063e865f35984ae417b04a4513e5024 lib/core/datatype.py
174-
322978f03cd69f7c98f2ea2cbe7567ab4f386b6c0548dcdf09064a6e9c393383 lib/core/decorators.py
174+
a3979f1b8f8b407d033c2c39d3e8242f0151886703bb4ab8c8f5a13b8079271c lib/core/decorators.py
175175
d573a37bb00c8b65f75b275aa92549683180fb209b75fd0ff3870e3848939900 lib/core/defaults.py
176176
bb7e6521edad1cbfffa89fd7d5e255ed4ff148d984ffadbeac8d42baa2d76dea lib/core/dicts.py
177177
20a6edda1d57a7564869e366f57ed7b2ab068dd8716cf7a10ef4a02d154d6c80 lib/core/dump.py
@@ -188,7 +188,7 @@ c4bfb493a03caf84dd362aec7c248097841de804b7413d0e1ecb8a90c8550bc0 lib/core/readl
188188
d1bd70c1a55858495c727fbec91e30af267459c8f64d50fabf9e4ee2c007e920 lib/core/replication.py
189189
1d0f80b0193ac5204527bfab4bde1a7aee0f693fd008e86b4b29f606d1ef94f3 lib/core/revision.py
190190
d2eb8e4b05ac93551272b3d4abfaf5b9f2d3ac92499a7704c16ed0b4f200db38 lib/core/session.py
191-
632de5ad548d5d44af0477d22b43fccd1ccb52eb503bb34ed597738b6ee0219d lib/core/settings.py
191+
9ffdf1b4e618a2d335948321e592dbf02000e6bda5381bcfb96dba42142d08d1 lib/core/settings.py
192192
1c5eab9494eb969bc9ce118a2ea6954690c6851cbe54c18373c723b99734bf09 lib/core/shell.py
193193
4eea6dcf023e41e3c64b210cb5c2efc7ca893b727f5e49d9c924f076bb224053 lib/core/subprocessng.py
194194
cdd352e1331c6b535e780f6edea79465cb55af53aa2114dcea0e8bf382e56d1a lib/core/target.py

lib/core/decorators.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,34 @@ def cachedmethod(f):
4242

4343
@functools.wraps(f)
4444
def _f(*args, **kwargs):
45-
parts = (
46-
f.__module__ + "." + f.__name__,
47-
"^".join(repr(a) for a in args),
48-
"^".join("%s=%r" % (k, kwargs[k]) for k in sorted(kwargs))
49-
)
5045
try:
51-
key = struct.unpack(">Q", hashlib.md5("`".join(parts).encode(UNICODE_ENCODING)).digest()[:8])[0] & 0x7fffffffffffffff
52-
except (struct.error, ValueError): # https://github.com/sqlmapproject/sqlmap/issues/4281 (NOTE: non-standard Python behavior where hexdigest returns binary value)
53-
result = f(*args, **kwargs)
54-
else:
55-
lock, cache = _method_locks[f], _cache[f]
56-
57-
with lock:
58-
if key in cache:
59-
return cache[key]
46+
# NOTE: fast-path
47+
if kwargs:
48+
key = hash((f, args, tuple(map(type, args)), frozenset(kwargs.items()))) & 0x7fffffffffffffff
49+
else:
50+
key = hash((f, args, tuple(map(type, args)))) & 0x7fffffffffffffff
51+
except TypeError:
52+
# NOTE: failback slow-path
53+
parts = (
54+
f.__module__ + "." + f.__name__,
55+
"^".join(repr(a) for a in args),
56+
"^".join("%s=%r" % (k, kwargs[k]) for k in sorted(kwargs))
57+
)
58+
try:
59+
key = struct.unpack(">Q", hashlib.md5("`".join(parts).encode(UNICODE_ENCODING)).digest()[:8])[0] & 0x7fffffffffffffff
60+
except (struct.error, ValueError):
61+
return f(*args, **kwargs)
62+
63+
lock, cache = _method_locks[f], _cache[f]
6064

61-
result = f(*args, **kwargs)
65+
with lock:
66+
if key in cache:
67+
return cache[key]
6268

63-
with lock:
64-
cache[key] = result
69+
result = f(*args, **kwargs)
6570

66-
return result
71+
with lock:
72+
cache[key] = result
6773

6874
return result
6975

lib/core/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from thirdparty import six
2020

2121
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
22-
VERSION = "1.9.12.34"
22+
VERSION = "1.9.12.35"
2323
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2424
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2525
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)
@@ -191,7 +191,7 @@
191191
MAX_BUFFERED_PARTIAL_UNION_LENGTH = 1024
192192

193193
# Maximum size of cache used in @cachedmethod decorator
194-
MAX_CACHE_ITEMS = 256
194+
MAX_CACHE_ITEMS = 1024
195195

196196
# Suffix used for naming meta databases in DBMS(es) without explicit database name
197197
METADB_SUFFIX = "_masterdb"

0 commit comments

Comments
 (0)