Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f89146

Browse files
committed
remove _PyCounterOptimizer_Type and _PyOptimizer_NewCounter
1 parent 910be98 commit 3f89146

File tree

7 files changed

+7
-118
lines changed

7 files changed

+7
-118
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ PyAPI_FUNC(void) _Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj);
121121
// Export for '_testinternalcapi' shared extension.
122122
PyAPI_FUNC(_PyOptimizerObject *) _Py_GetOptimizer(void);
123123
PyAPI_FUNC(int) _Py_SetTier2Optimizer(_PyOptimizerObject* optimizer);
124-
PyAPI_FUNC(PyObject *) _PyOptimizer_NewCounter(void);
125124
PyAPI_FUNC(PyObject *) _PyOptimizer_NewUOpOptimizer(void);
126125

127126
#define _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS 3
@@ -153,7 +152,6 @@ int _Py_uop_analyze_and_optimize(struct _PyInterpreterFrame *frame,
153152
_PyBloomFilter *dependencies);
154153

155154
extern PyTypeObject _PyCounterExecutor_Type;
156-
extern PyTypeObject _PyCounterOptimizer_Type;
157155
extern PyTypeObject _PyDefaultOptimizer_Type;
158156
extern PyTypeObject _PyUOpExecutor_Type;
159157
extern PyTypeObject _PyUOpOptimizer_Type;

Lib/test/test_capi/test_opt.py

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def clear_executors(func):
3636

3737
@requires_specialization
3838
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
39-
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
39+
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer") and
40+
hasattr(_testinternalcapi, "new_counter_optimizer"),
4041
"Requires optimizer infrastructure")
4142
class TestOptimizerAPI(unittest.TestCase):
4243

@@ -140,89 +141,8 @@ def get_opnames(ex):
140141

141142
@requires_specialization
142143
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
143-
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
144-
"Requires optimizer infrastructure")
145-
class TestExecutorInvalidation(unittest.TestCase):
146-
147-
def setUp(self):
148-
self.old = _testinternalcapi.get_optimizer()
149-
self.opt = _testinternalcapi.new_counter_optimizer()
150-
_testinternalcapi.set_optimizer(self.opt)
151-
152-
def tearDown(self):
153-
_testinternalcapi.set_optimizer(self.old)
154-
155-
def test_invalidate_object(self):
156-
# Generate a new set of functions at each call
157-
ns = {}
158-
func_src = "\n".join(
159-
f"""
160-
def f{n}():
161-
for _ in range(1000):
162-
pass
163-
""" for n in range(5)
164-
)
165-
exec(textwrap.dedent(func_src), ns, ns)
166-
funcs = [ ns[f'f{n}'] for n in range(5)]
167-
objects = [object() for _ in range(5)]
168-
169-
for f in funcs:
170-
f()
171-
executors = [get_first_executor(f) for f in funcs]
172-
# Set things up so each executor depends on the objects
173-
# with an equal or lower index.
174-
for i, exe in enumerate(executors):
175-
self.assertTrue(exe.is_valid())
176-
for obj in objects[:i+1]:
177-
_testinternalcapi.add_executor_dependency(exe, obj)
178-
self.assertTrue(exe.is_valid())
179-
# Assert that the correct executors are invalidated
180-
# and check that nothing crashes when we invalidate
181-
# an executor multiple times.
182-
for i in (4,3,2,1,0):
183-
_testinternalcapi.invalidate_executors(objects[i])
184-
for exe in executors[i:]:
185-
self.assertFalse(exe.is_valid())
186-
for exe in executors[:i]:
187-
self.assertTrue(exe.is_valid())
188-
189-
def test_uop_optimizer_invalidation(self):
190-
# Generate a new function at each call
191-
ns = {}
192-
exec(textwrap.dedent("""
193-
def f():
194-
for i in range(1000):
195-
pass
196-
"""), ns, ns)
197-
f = ns['f']
198-
opt = _testinternalcapi.new_uop_optimizer()
199-
with temporary_optimizer(opt):
200-
f()
201-
exe = get_first_executor(f)
202-
self.assertIsNotNone(exe)
203-
self.assertTrue(exe.is_valid())
204-
_testinternalcapi.invalidate_executors(f.__code__)
205-
self.assertFalse(exe.is_valid())
206-
207-
def test_sys__clear_internal_caches(self):
208-
def f():
209-
for _ in range(1000):
210-
pass
211-
opt = _testinternalcapi.new_uop_optimizer()
212-
with temporary_optimizer(opt):
213-
f()
214-
exe = get_first_executor(f)
215-
self.assertIsNotNone(exe)
216-
self.assertTrue(exe.is_valid())
217-
sys._clear_internal_caches()
218-
self.assertFalse(exe.is_valid())
219-
exe = get_first_executor(f)
220-
self.assertIsNone(exe)
221-
222-
223-
@requires_specialization
224-
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
225-
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
144+
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer") and
145+
hasattr(_testinternalcapi, "new_counter_optimizer"),
226146
"Requires optimizer infrastructure")
227147
class TestExecutorInvalidation(unittest.TestCase):
228148

Lib/test/test_monitoring.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from test.support.import_helper import import_module
1616

1717
_testcapi = test.support.import_helper.import_module("_testcapi")
18+
_testinternalcapi = test.support.import_helper.import_module("_testinternalcapi")
1819

1920
PAIR = (0,1)
2021

@@ -2003,10 +2004,11 @@ def callback(code, instruction_offset):
20032004
sys.monitoring.set_events(0, 0)
20042005

20052006

2007+
@unittest.skipUnless(hasattr(_testinternalcapi, "new_counter_optimizer"),
2008+
"Requires counter optimizer")
20062009
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
20072010

20082011
def setUp(self):
2009-
_testinternalcapi = import_module("_testinternalcapi")
20102012
if hasattr(_testinternalcapi, "get_optimizer"):
20112013
self.old_opt = _testinternalcapi.get_optimizer()
20122014
opt = _testinternalcapi.new_counter_optimizer()

Modules/_testinternalcapi.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -989,12 +989,6 @@ get_co_framesize(PyObject *self, PyObject *arg)
989989

990990
#ifdef _Py_TIER2
991991

992-
static PyObject *
993-
new_counter_optimizer(PyObject *self, PyObject *arg)
994-
{
995-
return _PyOptimizer_NewCounter();
996-
}
997-
998992
static PyObject *
999993
new_uop_optimizer(PyObject *self, PyObject *arg)
1000994
{
@@ -2101,7 +2095,6 @@ static PyMethodDef module_functions[] = {
21012095
#ifdef _Py_TIER2
21022096
{"get_optimizer", get_optimizer, METH_NOARGS, NULL},
21032097
{"set_optimizer", set_optimizer, METH_O, NULL},
2104-
{"new_counter_optimizer", new_counter_optimizer, METH_NOARGS, NULL},
21052098
{"new_uop_optimizer", new_uop_optimizer, METH_NOARGS, NULL},
21062099
{"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL},
21072100
{"invalidate_executors", invalidate_executors, METH_O, NULL},

Objects/object.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,6 @@ static PyTypeObject* static_types[] = {
23802380
&_PyCoroWrapper_Type,
23812381
#ifdef _Py_TIER2
23822382
&_PyCounterExecutor_Type,
2383-
&_PyCounterOptimizer_Type,
23842383
&_PyDefaultOptimizer_Type,
23852384
#endif
23862385
&_Py_GenericAliasIterType,

Python/optimizer.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,28 +1394,6 @@ static PyMethodDef counter_optimizer_methods[] = {
13941394
{ NULL, NULL },
13951395
};
13961396

1397-
PyTypeObject _PyCounterOptimizer_Type = {
1398-
PyVarObject_HEAD_INIT(&PyType_Type, 0)
1399-
.tp_name = "Counter optimizer",
1400-
.tp_basicsize = sizeof(_PyCounterOptimizerObject),
1401-
.tp_itemsize = 0,
1402-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1403-
.tp_methods = counter_optimizer_methods,
1404-
.tp_dealloc = (destructor)PyObject_Free,
1405-
};
1406-
1407-
PyObject *
1408-
_PyOptimizer_NewCounter(void)
1409-
{
1410-
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&_PyCounterOptimizer_Type);
1411-
if (opt == NULL) {
1412-
return NULL;
1413-
}
1414-
opt->base.optimize = counter_optimize;
1415-
opt->count = 0;
1416-
return (PyObject *)opt;
1417-
}
1418-
14191397

14201398
/*****************************************
14211399
* Executor management

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ Python/sysmodule.c - _PySys_ImplName -
385385
Python/sysmodule.c - whatstrings -
386386
Python/optimizer.c - _PyDefaultOptimizer_Type -
387387
Python/optimizer.c - _PyCounterExecutor_Type -
388-
Python/optimizer.c - _PyCounterOptimizer_Type -
389388
Python/optimizer.c - _PyUOpExecutor_Type -
390389
Python/optimizer.c - _PyUOpOptimizer_Type -
391390
Python/optimizer.c - _PyOptimizer_Default -

0 commit comments

Comments
 (0)