From 503c7b62bd658c2c9d54ae334779e9b4ce096fc0 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 30 Dec 2025 13:04:13 +0100 Subject: [PATCH] Some improvements in cachedmethods --- data/txt/sha256sums.txt | 4 ++-- lib/core/decorators.py | 40 +++++++++++++++++++++++----------------- lib/core/settings.py | 4 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/data/txt/sha256sums.txt b/data/txt/sha256sums.txt index c56075bf3..18f41e881 100644 --- a/data/txt/sha256sums.txt +++ b/data/txt/sha256sums.txt @@ -171,7 +171,7 @@ f6062e324fdeaacf9df0a289fc3f12f755143e3876a70cb65b38aa2e690f73c1 lib/core/commo 39ea62d4224be860befeffb3843c150f2343b64555ad8c438a400222056f6cc0 lib/core/convert.py ae500647c4074681749735a4f3b17b7eca44868dd3f39f9cab0a575888ba04a1 lib/core/data.py ffae7cfe9f9afb92e887b9a8dbc1630d0063e865f35984ae417b04a4513e5024 lib/core/datatype.py -322978f03cd69f7c98f2ea2cbe7567ab4f386b6c0548dcdf09064a6e9c393383 lib/core/decorators.py +a3979f1b8f8b407d033c2c39d3e8242f0151886703bb4ab8c8f5a13b8079271c lib/core/decorators.py d573a37bb00c8b65f75b275aa92549683180fb209b75fd0ff3870e3848939900 lib/core/defaults.py bb7e6521edad1cbfffa89fd7d5e255ed4ff148d984ffadbeac8d42baa2d76dea lib/core/dicts.py 20a6edda1d57a7564869e366f57ed7b2ab068dd8716cf7a10ef4a02d154d6c80 lib/core/dump.py @@ -188,7 +188,7 @@ c4bfb493a03caf84dd362aec7c248097841de804b7413d0e1ecb8a90c8550bc0 lib/core/readl d1bd70c1a55858495c727fbec91e30af267459c8f64d50fabf9e4ee2c007e920 lib/core/replication.py 1d0f80b0193ac5204527bfab4bde1a7aee0f693fd008e86b4b29f606d1ef94f3 lib/core/revision.py d2eb8e4b05ac93551272b3d4abfaf5b9f2d3ac92499a7704c16ed0b4f200db38 lib/core/session.py -632de5ad548d5d44af0477d22b43fccd1ccb52eb503bb34ed597738b6ee0219d lib/core/settings.py +9ffdf1b4e618a2d335948321e592dbf02000e6bda5381bcfb96dba42142d08d1 lib/core/settings.py 1c5eab9494eb969bc9ce118a2ea6954690c6851cbe54c18373c723b99734bf09 lib/core/shell.py 4eea6dcf023e41e3c64b210cb5c2efc7ca893b727f5e49d9c924f076bb224053 lib/core/subprocessng.py cdd352e1331c6b535e780f6edea79465cb55af53aa2114dcea0e8bf382e56d1a lib/core/target.py diff --git a/lib/core/decorators.py b/lib/core/decorators.py index 201abac75..dbbc23580 100644 --- a/lib/core/decorators.py +++ b/lib/core/decorators.py @@ -42,28 +42,34 @@ def cachedmethod(f): @functools.wraps(f) def _f(*args, **kwargs): - parts = ( - f.__module__ + "." + f.__name__, - "^".join(repr(a) for a in args), - "^".join("%s=%r" % (k, kwargs[k]) for k in sorted(kwargs)) - ) try: - key = struct.unpack(">Q", hashlib.md5("`".join(parts).encode(UNICODE_ENCODING)).digest()[:8])[0] & 0x7fffffffffffffff - except (struct.error, ValueError): # https://github.com/sqlmapproject/sqlmap/issues/4281 (NOTE: non-standard Python behavior where hexdigest returns binary value) - result = f(*args, **kwargs) - else: - lock, cache = _method_locks[f], _cache[f] + # NOTE: fast-path + if kwargs: + key = hash((f, args, tuple(map(type, args)), frozenset(kwargs.items()))) & 0x7fffffffffffffff + else: + key = hash((f, args, tuple(map(type, args)))) & 0x7fffffffffffffff + except TypeError: + # NOTE: failback slow-path + parts = ( + f.__module__ + "." + f.__name__, + "^".join(repr(a) for a in args), + "^".join("%s=%r" % (k, kwargs[k]) for k in sorted(kwargs)) + ) + try: + key = struct.unpack(">Q", hashlib.md5("`".join(parts).encode(UNICODE_ENCODING)).digest()[:8])[0] & 0x7fffffffffffffff + except (struct.error, ValueError): + return f(*args, **kwargs) - with lock: - if key in cache: - return cache[key] + lock, cache = _method_locks[f], _cache[f] - result = f(*args, **kwargs) + with lock: + if key in cache: + return cache[key] - with lock: - cache[key] = result + result = f(*args, **kwargs) - return result + with lock: + cache[key] = result return result diff --git a/lib/core/settings.py b/lib/core/settings.py index d085cdb51..5a8c63626 100644 --- a/lib/core/settings.py +++ b/lib/core/settings.py @@ -19,7 +19,7 @@ from lib.core.enums import OS from thirdparty import six # sqlmap version (...) -VERSION = "1.9.12.34" +VERSION = "1.9.12.35" TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable" TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34} 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 @@ MAX_TECHNIQUES_PER_VALUE = 2 MAX_BUFFERED_PARTIAL_UNION_LENGTH = 1024 # Maximum size of cache used in @cachedmethod decorator -MAX_CACHE_ITEMS = 256 +MAX_CACHE_ITEMS = 1024 # Suffix used for naming meta databases in DBMS(es) without explicit database name METADB_SUFFIX = "_masterdb"