Skip to content

Commit ac515b8

Browse files
constant fold on classes as well
1 parent e9a08b0 commit ac515b8

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3196,10 +3196,12 @@ def f(n):
31963196
x += e.m() # _LOAD_ATTR_METHOD_LAZY_DICT
31973197
x += f.class_method() # _LOAD_ATTR
31983198
x += f.static_method() # _LOAD_ATTR
3199+
x += F.class_method() # _LOAD_ATTR
3200+
x += F.static_method() # _LOAD_ATTR
31993201
return x
32003202

32013203
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
3202-
self.assertEqual(res, 8 * TIER2_THRESHOLD)
3204+
self.assertEqual(res, 10 * TIER2_THRESHOLD)
32033205
self.assertIsNotNone(ex)
32043206
uops = get_opnames(ex)
32053207
self.assertNotIn("_LOAD_ATTR", uops)

Python/bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2728,7 +2728,7 @@ dummy_func(
27282728

27292729
macro(LOAD_ATTR) =
27302730
_SPECIALIZE_LOAD_ATTR +
2731-
_RECORD_TOS_TYPE +
2731+
_RECORD_TOS +
27322732
unused/8 +
27332733
_LOAD_ATTR;
27342734

Python/optimizer_bytecodes.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,15 @@ dummy_func(void) {
873873
}
874874

875875
op(_LOAD_ATTR, (owner -- attr, self_or_null[oparg&1])) {
876-
PyTypeObject *type = sym_get_probable_type(owner);
876+
PyObject *value = sym_get_probable_value(owner);
877+
PyTypeObject *type = NULL;
878+
if (value != NULL && PyType_Check(value)) {
879+
type = (PyTypeObject *)value;
880+
}
881+
else {
882+
type = sym_get_probable_type(owner);
883+
}
884+
877885
if (oparg & 1 && type != NULL) {
878886
PyObject *name = get_co_name(ctx, oparg >> 1);
879887
PyObject *descr = _PyType_Lookup(type, name);
@@ -890,24 +898,32 @@ dummy_func(void) {
890898
}
891899
assert(callable);
892900
bool immortal = _Py_IsImmortal(callable) || (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE);
893-
ADD_OP(_GUARD_TYPE_VERSION, 0, type->tp_version_tag);
901+
if (PyType_Check(value)) {
902+
ADD_OP(_CHECK_ATTR_CLASS, 0, type->tp_version_tag);
903+
}
904+
else {
905+
ADD_OP(_GUARD_TYPE_VERSION, 0, type->tp_version_tag);
906+
}
894907
ADD_OP(_POP_TOP, 0, 0);
895908
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE, 0, (uintptr_t)callable);
896909
if (class_method) {
897910
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)type);
898911
self_or_null[0] = sym_new_const(ctx, (PyObject *)type);
899-
} else if (static_method) {
912+
}
913+
else {
900914
ADD_OP(_PUSH_NULL, 0, 0);
901915
self_or_null[0] = sym_new_null(ctx);
902916
}
903917
attr = sym_new_const(ctx, callable);
904918
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
905919
_Py_BloomFilter_Add(dependencies, (PyTypeObject *)type);
906-
} else {
920+
}
921+
else {
907922
attr = sym_new_not_null(ctx);
908923
self_or_null[0] = sym_new_unknown(ctx);
909924
}
910-
} else {
925+
}
926+
else {
911927
attr = sym_new_not_null(ctx);
912928
}
913929
}

Python/optimizer_cases.c.h

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/record_functions.c.h

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)