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

Skip to content

GH-106008: Make implicit boolean conversions explicit #106003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Specialize UNARY_NOT
  • Loading branch information
brandtbucher committed Jun 6, 2023
commit 5289c7f69e7e2ba75cafa076da55dd9f179cae7c
7 changes: 7 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ typedef struct {

#define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache)

typedef struct {
uint16_t counter;
} _PyUnaryNotCache;

#define INLINE_CACHE_ENTRIES_UNARY_NOT CACHE_ENTRIES(_PyUnaryNotCache)

// Borrowed references to common callables:
struct callable_cache {
PyObject *isinstance;
Expand Down Expand Up @@ -246,6 +252,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
int oparg);
extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr);
extern void _Py_Specialize_UnaryNot(PyObject *value, _Py_CODEUNIT *instr);

/* Finalizer function for static codeobjects used in deepfreeze.py */
extern void _PyStaticCode_Fini(PyCodeObject *co);
Expand Down
21 changes: 11 additions & 10 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ def pseudo_op(name, op, real_ops):
"SEND": [
"SEND_GEN",
],
"UNARY_NOT": [
"UNARY_NOT_BOOL",
"UNARY_NOT_INT",
"UNARY_NOT_LIST",
"UNARY_NOT_NONE",
"UNARY_NOT_STR",
]
}
_specialized_instructions = [
opcode for family in _specializations.values() for opcode in family
Expand Down Expand Up @@ -492,6 +499,9 @@ def pseudo_op(name, op, real_ops):
"JUMP_BACKWARD": {
"counter": 1,
},
"UNARY_NOT": {
"counter": 1,
},
}

_inline_cache_entries = [
Expand Down
212 changes: 106 additions & 106 deletions Lib/test/test_dis.py

Large diffs are not rendered by default.

78 changes: 77 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,26 @@ dummy_func(
ERROR_IF(res == NULL, error);
}

inst(UNARY_NOT, (value -- res)) {
family(unary_not, INLINE_CACHE_ENTRIES_UNARY_NOT) = {
UNARY_NOT,
UNARY_NOT_BOOL,
UNARY_NOT_INT,
UNARY_NOT_LIST,
UNARY_NOT_NONE,
UNARY_NOT_STR,
};

inst(UNARY_NOT, (unused/1, value -- res)) {
#if ENABLE_SPECIALIZATION
_PyUnaryNotCache *cache = (_PyUnaryNotCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
next_instr--;
_Py_Specialize_UnaryNot(value, next_instr);
DISPATCH_SAME_OPARG();
}
STAT_INC(UNARY_NOT, deferred);
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */
int err = PyObject_IsTrue(value);
DECREF_INPUTS();
ERROR_IF(err < 0, error);
Expand All @@ -272,6 +291,63 @@ dummy_func(
}
}

inst(UNARY_NOT_BOOL, (unused/1, value -- res)) {
DEOPT_IF(!PyBool_Check(value), UNARY_NOT);
STAT_INC(UNARY_NOT, hit);
if (Py_IsFalse(value)) {
res = Py_True;
}
else {
res = Py_False;
}
}

inst(UNARY_NOT_INT, (unused/1, value -- res)) {
DEOPT_IF(!PyLong_CheckExact(value), UNARY_NOT);
STAT_INC(UNARY_NOT, hit);
if (_PyLong_IsZero((PyLongObject *)value)) {
assert(_Py_IsImmortal(value));
res = Py_True;
}
else {
DECREF_INPUTS();
res = Py_False;
}
}

inst(UNARY_NOT_LIST, (unused/1, value -- res)) {
DEOPT_IF(!PyList_CheckExact(value), UNARY_NOT);
STAT_INC(UNARY_NOT, hit);
if (!Py_SIZE(value)) {
res = Py_True;
}
else {
res = Py_False;
}
DECREF_INPUTS();
}

inst(UNARY_NOT_NONE, (unused/1, value -- res)) {
// This one is a bit weird, because we expect *some* failures:
DEOPT_IF(!Py_IsNone(value), UNARY_NOT);
STAT_INC(UNARY_NOT, hit);
res = Py_True;
}

inst(UNARY_NOT_STR, (unused/1, value -- res)) {
DEOPT_IF(!PyUnicode_CheckExact(value), UNARY_NOT);
STAT_INC(UNARY_NOT, hit);
if (Py_Is(value, &_Py_STR(empty))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use value == &_Py_STR(empty). The semantics is value == "", not value is "".
In general I wouldn't use Py_Is except when you want the exact semantics of Python's x is y.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I mean, we are checking for the identity of the singleton string here. I'll just change it, though.

Copy link
Member

@markshannon markshannon Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hypothetically we could have more than one "" object. I don't think that " ".strip() is "" is part of the language spec, so Py_Is doesn't add any safety, just obfuscation.

We can also potentially have tagged ints, in which case Py_Is would need to become a lot more complex, but value == &_Py_STR(empty) would remain efficient.

assert(_Py_IsImmortal(value));
res = Py_True;
}
else {
assert(Py_SIZE(value));
DECREF_INPUTS();
res = Py_False;
}
}

inst(UNARY_INVERT, (value -- res)) {
res = PyNumber_Invert(value);
DECREF_INPUTS();
Expand Down
Loading