Skip to content

Commit 1eb33e1

Browse files
committed
remove _PyOptimizer_NewCounter
1 parent 3fecbe9 commit 1eb33e1

File tree

5 files changed

+0
-216
lines changed

5 files changed

+0
-216
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ PyAPI_FUNC(void) _Py_Executor_DependsOn(_PyExecutorObject *executor, void *obj);
116116
// Export for '_testinternalcapi' shared extension.
117117
PyAPI_FUNC(_PyOptimizerObject *) _Py_GetOptimizer(void);
118118
PyAPI_FUNC(int) _Py_SetTier2Optimizer(_PyOptimizerObject* optimizer);
119-
PyAPI_FUNC(PyObject *) _PyOptimizer_NewCounter(void);
120119
PyAPI_FUNC(PyObject *) _PyOptimizer_NewUOpOptimizer(void);
121120

122121
#define _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS 3

Lib/test/test_capi/test_opt.py

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -34,91 +34,6 @@ def clear_executors(func):
3434
func.__code__ = func.__code__.replace()
3535

3636

37-
@requires_specialization
38-
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
39-
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),
40-
"Requires optimizer infrastructure")
41-
class TestOptimizerAPI(unittest.TestCase):
42-
43-
def test_new_counter_optimizer_dealloc(self):
44-
# See gh-108727
45-
def f():
46-
_testinternalcapi.new_counter_optimizer()
47-
48-
f()
49-
50-
def test_get_set_optimizer(self):
51-
old = _testinternalcapi.get_optimizer()
52-
opt = _testinternalcapi.new_counter_optimizer()
53-
try:
54-
_testinternalcapi.set_optimizer(opt)
55-
self.assertEqual(_testinternalcapi.get_optimizer(), opt)
56-
_testinternalcapi.set_optimizer(None)
57-
self.assertEqual(_testinternalcapi.get_optimizer(), None)
58-
finally:
59-
_testinternalcapi.set_optimizer(old)
60-
61-
62-
def test_counter_optimizer(self):
63-
# Generate a new function at each call
64-
ns = {}
65-
exec(textwrap.dedent("""
66-
def loop():
67-
for _ in range(1000):
68-
pass
69-
"""), ns, ns)
70-
loop = ns['loop']
71-
72-
for repeat in range(5):
73-
opt = _testinternalcapi.new_counter_optimizer()
74-
with temporary_optimizer(opt):
75-
self.assertEqual(opt.get_count(), 0)
76-
with clear_executors(loop):
77-
loop()
78-
# Subtract because optimizer doesn't kick in sooner
79-
self.assertEqual(opt.get_count(), 1000 - TIER2_THRESHOLD)
80-
81-
def test_long_loop(self):
82-
"Check that we aren't confused by EXTENDED_ARG"
83-
84-
# Generate a new function at each call
85-
ns = {}
86-
exec(textwrap.dedent("""
87-
def nop():
88-
pass
89-
90-
def long_loop():
91-
for _ in range(20):
92-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
93-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
94-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
95-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
96-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
97-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
98-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
99-
"""), ns, ns)
100-
long_loop = ns['long_loop']
101-
102-
opt = _testinternalcapi.new_counter_optimizer()
103-
with temporary_optimizer(opt):
104-
self.assertEqual(opt.get_count(), 0)
105-
long_loop()
106-
self.assertEqual(opt.get_count(), 20 - TIER2_THRESHOLD) # Need iterations to warm up
107-
108-
def test_code_restore_for_ENTER_EXECUTOR(self):
109-
def testfunc(x):
110-
i = 0
111-
while i < x:
112-
i += 1
113-
114-
opt = _testinternalcapi.new_counter_optimizer()
115-
with temporary_optimizer(opt):
116-
testfunc(1000)
117-
code, replace_code = testfunc.__code__, testfunc.__code__.replace()
118-
self.assertEqual(code, replace_code)
119-
self.assertEqual(hash(code), hash(replace_code))
120-
121-
12237
def get_first_executor(func):
12338
code = func.__code__
12439
co_code = code.co_code
@@ -139,88 +54,6 @@ def get_opnames(ex):
13954
return list(iter_opnames(ex))
14055

14156

142-
@requires_specialization
143-
@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-
22457
@requires_specialization
22558
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds")
22659
@unittest.skipUnless(hasattr(_testinternalcapi, "get_optimizer"),

Lib/test/test_monitoring.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,35 +1959,6 @@ def callback(code, instruction_offset):
19591959
sys.monitoring.set_events(0, 0)
19601960

19611961

1962-
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
1963-
1964-
def setUp(self):
1965-
_testinternalcapi = import_module("_testinternalcapi")
1966-
if hasattr(_testinternalcapi, "get_optimizer"):
1967-
self.old_opt = _testinternalcapi.get_optimizer()
1968-
opt = _testinternalcapi.new_counter_optimizer()
1969-
_testinternalcapi.set_optimizer(opt)
1970-
super(TestOptimizer, self).setUp()
1971-
1972-
def tearDown(self):
1973-
super(TestOptimizer, self).tearDown()
1974-
import _testinternalcapi
1975-
if hasattr(_testinternalcapi, "get_optimizer"):
1976-
_testinternalcapi.set_optimizer(self.old_opt)
1977-
1978-
def test_for_loop(self):
1979-
def test_func(x):
1980-
i = 0
1981-
while i < x:
1982-
i += 1
1983-
1984-
code = test_func.__code__
1985-
sys.monitoring.set_local_events(TEST_TOOL, code, E.PY_START)
1986-
self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), E.PY_START)
1987-
test_func(1000)
1988-
sys.monitoring.set_local_events(TEST_TOOL, code, 0)
1989-
self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), 0)
1990-
19911962
class TestTier2Optimizer(CheckEvents):
19921963

19931964
def test_monitoring_already_opimized_loop(self):

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},

Python/optimizer.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,18 +1387,6 @@ PyTypeObject _PyCounterOptimizer_Type = {
13871387
.tp_dealloc = (destructor)PyObject_Free,
13881388
};
13891389

1390-
PyObject *
1391-
_PyOptimizer_NewCounter(void)
1392-
{
1393-
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&_PyCounterOptimizer_Type);
1394-
if (opt == NULL) {
1395-
return NULL;
1396-
}
1397-
opt->base.optimize = counter_optimize;
1398-
opt->count = 0;
1399-
return (PyObject *)opt;
1400-
}
1401-
14021390

14031391
/*****************************************
14041392
* Executor management

0 commit comments

Comments
 (0)