Skip to content

Commit f3542b6

Browse files
committed
remove _PyCounterOptimizer_Type and _PyOptimizer_NewCounter
1 parent 68dd2b1 commit f3542b6

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
@@ -37,7 +37,8 @@ def clear_executors(func):
3737

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

@@ -141,89 +142,8 @@ def get_opnames(ex):
141142

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

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

@@ -2085,10 +2086,11 @@ def callback(code, instruction_offset):
20852086
sys.monitoring.set_events(0, 0)
20862087

20872088

2089+
@unittest.skipUnless(hasattr(_testinternalcapi, "new_counter_optimizer"),
2090+
"Requires counter optimizer")
20882091
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
20892092

20902093
def setUp(self):
2091-
_testinternalcapi = import_module("_testinternalcapi")
20922094
if hasattr(_testinternalcapi, "get_optimizer"):
20932095
self.old_opt = _testinternalcapi.get_optimizer()
20942096
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
@@ -2381,7 +2381,6 @@ static PyTypeObject* static_types[] = {
23812381
&_PyCoroWrapper_Type,
23822382
#ifdef _Py_TIER2
23832383
&_PyCounterExecutor_Type,
2384-
&_PyCounterOptimizer_Type,
23852384
&_PyDefaultOptimizer_Type,
23862385
#endif
23872386
&_Py_GenericAliasIterType,

Python/optimizer.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,28 +1400,6 @@ static PyMethodDef counter_optimizer_methods[] = {
14001400
{ NULL, NULL },
14011401
};
14021402

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

14261404
/*****************************************
14271405
* 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)