mitogen: Refactor scan_code_imports() as mitogen.imports.codeobj_imports()
This replaces `mitogen.master.scan_code_imports()` with `mitogen.imports.codeobj_imports()`. The Python 3.x implementation now uses `str.find()`, relying on Python >= 3.6 "widecode" format. Behaviour and semantics should be unchanged. Now implementations are approx - 1.5 x faster on Python 2.x - 2 - 3 x faster on Python 3.x Before ```console $ ./tests/bench/scan_code scan_code_imports python2.7 100 loops, best of 3: 3.19 msec per loop scan_code_imports python3.9 500 loops, best of 5: 685 usec per loop scan_code_imports python3.10 500 loops, best of 5: 727 usec per loop scan_code_imports python3.11 500 loops, best of 5: 601 usec per loop scan_code_imports python3.12 500 loops, best of 5: 609 usec per loop scan_code_imports python3.13 500 loops, best of 5: 586 usec per loop ``` After ```console codeobj_imports python2.7 1000 loops, best of 3: 1.98 msec per loop codeobj_imports python3.9 1000 loops, best of 5: 302 usec per loop codeobj_imports python3.10 1000 loops, best of 5: 297 usec per loop codeobj_imports python3.11 1000 loops, best of 5: 243 usec per loop codeobj_imports python3.12 1000 loops, best of 5: 278 usec per loop codeobj_imports python3.13 1000 loops, best of 5: 259 usec per loop ``` ```console $ uname -a Darwin kintha 24.6.0 Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:29 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6000 arm64 ```pull/1328/head
parent
3093d0bb2d
commit
0e5f47f145
@ -0,0 +1,36 @@
|
|||||||
|
# SPDX-FileCopyrightText: 2025 Mitogen authors <https://github.com/mitogen-hq>
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
# !mitogen: minify_safe
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 6):
|
||||||
|
from mitogen.imports._py36 import _code_imports
|
||||||
|
elif sys.version_info >= (2, 5):
|
||||||
|
from mitogen.imports._py2 import _code_imports_py25 as _code_imports
|
||||||
|
else:
|
||||||
|
from mitogen.imports._py2 import _code_imports_py24 as _code_imports
|
||||||
|
|
||||||
|
|
||||||
|
def codeobj_imports(co):
|
||||||
|
"""
|
||||||
|
Yield (level, modname, names) tuples by scanning the code object `co`.
|
||||||
|
|
||||||
|
Top level `import mod` & `from mod import foo` statements are matched.
|
||||||
|
Those inside a `class ...` or `def ...` block are currently skipped.
|
||||||
|
|
||||||
|
>>> co = compile('import a, b; from c import d, e as f', '<str>', 'exec')
|
||||||
|
>>> list(codeobj_imports(co)) # doctest: +ELLIPSIS
|
||||||
|
[(..., 'a', ()), (..., 'b', ()), (..., 'c', ('d', 'e'))]
|
||||||
|
|
||||||
|
:return:
|
||||||
|
Generator producing `(level, modname, names)` tuples, where:
|
||||||
|
|
||||||
|
* `level`:
|
||||||
|
-1 implicit relative (Python 2.x default)
|
||||||
|
0 absolute (Python 3.x, `from __future__ import absolute_import`)
|
||||||
|
>0 explicit relative (`from . import a`, `from ..b, import c`)
|
||||||
|
* `modname`: Name of module to import, or to import `names` from.
|
||||||
|
* `names`: tuple of names in `from mod import ..`.
|
||||||
|
"""
|
||||||
|
return _code_imports(co.co_code, co.co_consts, co.co_names)
|
||||||
@ -0,0 +1,54 @@
|
|||||||
|
# SPDX-FileCopyrightText: 2025 Mitogen authors <https://github.com/mitogen-hq>
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
# !mitogen: minify_safe
|
||||||
|
|
||||||
|
import array
|
||||||
|
import itertools
|
||||||
|
import opcode
|
||||||
|
|
||||||
|
|
||||||
|
IMPORT_NAME = opcode.opmap['IMPORT_NAME']
|
||||||
|
LOAD_CONST = opcode.opmap['LOAD_CONST']
|
||||||
|
|
||||||
|
|
||||||
|
def _opargs(code, _have_arg=opcode.HAVE_ARGUMENT):
|
||||||
|
it = iter(array.array('B', code))
|
||||||
|
nexti = it.next
|
||||||
|
for i in it:
|
||||||
|
if i >= _have_arg:
|
||||||
|
yield (i, nexti() | (nexti() << 8))
|
||||||
|
else:
|
||||||
|
yield (i, None)
|
||||||
|
|
||||||
|
|
||||||
|
def _code_imports_py25(code, consts, names):
|
||||||
|
it1, it2, it3 = itertools.tee(_opargs(code), 3)
|
||||||
|
try:
|
||||||
|
next(it2)
|
||||||
|
next(it3)
|
||||||
|
next(it3)
|
||||||
|
except StopIteration:
|
||||||
|
return
|
||||||
|
for oparg1, oparg2, (op3, arg3) in itertools.izip(it1, it2, it3):
|
||||||
|
if op3 != IMPORT_NAME:
|
||||||
|
continue
|
||||||
|
op1, arg1 = oparg1
|
||||||
|
op2, arg2 = oparg2
|
||||||
|
if op1 != LOAD_CONST or op2 != LOAD_CONST:
|
||||||
|
continue
|
||||||
|
yield (consts[arg1], names[arg3], consts[arg2] or ())
|
||||||
|
|
||||||
|
|
||||||
|
def _code_imports_py24(code, consts, names):
|
||||||
|
it1, it2 = itertools.tee(_opargs(code), 2)
|
||||||
|
try:
|
||||||
|
next(it2)
|
||||||
|
except StopIteration:
|
||||||
|
return
|
||||||
|
for oparg1, (op2, arg2) in itertools.izip(it1, it2):
|
||||||
|
if op2 != IMPORT_NAME:
|
||||||
|
continue
|
||||||
|
op1, arg1 = oparg1
|
||||||
|
if op1 != LOAD_CONST:
|
||||||
|
continue
|
||||||
|
yield (-1, names[arg2], consts[arg1] or ())
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
# SPDX-FileCopyrightText: 2025 Mitogen authors <https://github.com/mitogen-hq>
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
# !mitogen: minify_safe
|
||||||
|
|
||||||
|
import opcode
|
||||||
|
|
||||||
|
IMPORT_NAME = opcode.opmap['IMPORT_NAME']
|
||||||
|
LOAD_CONST = opcode.opmap['LOAD_CONST']
|
||||||
|
|
||||||
|
|
||||||
|
def _code_imports(code, consts, names):
|
||||||
|
start = 4
|
||||||
|
while True:
|
||||||
|
op3_idx = code.find(IMPORT_NAME, start, -1)
|
||||||
|
if op3_idx < 0:
|
||||||
|
return
|
||||||
|
if op3_idx % 2:
|
||||||
|
start = op3_idx + 1
|
||||||
|
continue
|
||||||
|
if code[op3_idx-4] != LOAD_CONST or code[op3_idx-2] != LOAD_CONST:
|
||||||
|
start = op3_idx + 2
|
||||||
|
continue
|
||||||
|
start = op3_idx + 6
|
||||||
|
arg1, arg2, arg3 = code[op3_idx-3], code[op3_idx-1], code[op3_idx+1]
|
||||||
|
yield (consts[arg1], names[arg3], consts[arg2] or ())
|
||||||
Loading…
Reference in New Issue