Skip to content

Commit 4c0e6c0

Browse files
committed
remove _PyCounterOptimizer_Type and _PyOptimizer_NewCounter
1 parent e4f6f46 commit 4c0e6c0

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
@@ -118,7 +118,6 @@ PyAPI_FUNC(void) _Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj);
118118
// Export for '_testinternalcapi' shared extension.
119119
PyAPI_FUNC(_PyOptimizerObject *) _Py_GetOptimizer(void);
120120
PyAPI_FUNC(int) _Py_SetTier2Optimizer(_PyOptimizerObject* optimizer);
121-
PyAPI_FUNC(PyObject *) _PyOptimizer_NewCounter(void);
122121
PyAPI_FUNC(PyObject *) _PyOptimizer_NewUOpOptimizer(void);
123122

124123
#define _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS 3
@@ -150,7 +149,6 @@ int _Py_uop_analyze_and_optimize(struct _PyInterpreterFrame *frame,
150149
_PyBloomFilter *dependencies);
151150

152151
extern PyTypeObject _PyCounterExecutor_Type;
153-
extern PyTypeObject _PyCounterOptimizer_Type;
154152
extern PyTypeObject _PyDefaultOptimizer_Type;
155153
extern PyTypeObject _PyUOpExecutor_Type;
156154
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

@@ -1959,10 +1960,11 @@ def callback(code, instruction_offset):
19591960
sys.monitoring.set_events(0, 0)
19601961

19611962

1963+
@unittest.skipUnless(hasattr(_testinternalcapi, "new_counter_optimizer"),
1964+
"Requires counter optimizer")
19621965
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
19631966

19641967
def setUp(self):
1965-
_testinternalcapi = import_module("_testinternalcapi")
19661968
if hasattr(_testinternalcapi, "get_optimizer"):
19671969
self.old_opt = _testinternalcapi.get_optimizer()
19681970
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
{
@@ -2112,7 +2106,6 @@ static PyMethodDef module_functions[] = {
21122106
#ifdef _Py_TIER2
21132107
{"get_optimizer", get_optimizer, METH_NOARGS, NULL},
21142108
{"set_optimizer", set_optimizer, METH_O, NULL},
2115-
{"new_counter_optimizer", new_counter_optimizer, METH_NOARGS, NULL},
21162109
{"new_uop_optimizer", new_uop_optimizer, METH_NOARGS, NULL},
21172110
{"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL},
21182111
{"invalidate_executors", invalidate_executors, METH_O, NULL},

Objects/object.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,6 @@ static PyTypeObject* static_types[] = {
23452345
&_PyCoroWrapper_Type,
23462346
#ifdef _Py_TIER2
23472347
&_PyCounterExecutor_Type,
2348-
&_PyCounterOptimizer_Type,
23492348
&_PyDefaultOptimizer_Type,
23502349
#endif
23512350
&_Py_GenericAliasIterType,

Python/optimizer.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,28 +1387,6 @@ static PyMethodDef counter_optimizer_methods[] = {
13871387
{ NULL, NULL },
13881388
};
13891389

1390-
PyTypeObject _PyCounterOptimizer_Type = {
1391-
PyVarObject_HEAD_INIT(&PyType_Type, 0)
1392-
.tp_name = "Counter optimizer",
1393-
.tp_basicsize = sizeof(_PyCounterOptimizerObject),
1394-
.tp_itemsize = 0,
1395-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1396-
.tp_methods = counter_optimizer_methods,
1397-
.tp_dealloc = (destructor)PyObject_Free,
1398-
};
1399-
1400-
PyObject *
1401-
_PyOptimizer_NewCounter(void)
1402-
{
1403-
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&_PyCounterOptimizer_Type);
1404-
if (opt == NULL) {
1405-
return NULL;
1406-
}
1407-
opt->base.optimize = counter_optimize;
1408-
opt->count = 0;
1409-
return (PyObject *)opt;
1410-
}
1411-
14121390

14131391
/*****************************************
14141392
* Executor management

Tools/c-analyzer/cpython/ignored.tsv

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

0 commit comments

Comments
 (0)