8000 Remove special-casing from `FORMAT_VALUE` opcode · Issue #102598 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Remove special-casing from FORMAT_VALUE opcode #102598
Closed
@sobolevn

Description

@sobolevn

Right now there's a special case in handling this opcode:

cpython/Python/bytecodes.c

Lines 3060 to 3073 in 5ffdaf7

8000
/* If value is a unicode object, and there's no fmt_spec,
then we know the result of format(value) is value
itself. In that case, skip calling format(). I plan to
move this optimization in to PyObject_Format()
itself. */
if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
/* Do nothing, just transfer ownership to result. */
result = value;
} else {
/* Actually call format(). */
result = PyObject_Format(value, fmt_spec);
Py_DECREF(value);
Py_XDECREF(fmt_spec);
ERROR_IF(result == NULL, error);

It comes from older version of ceval.c:

cpython/Python/ceval.c

Lines 4379 to 4395 in 2810787

/* If value is a unicode object, and there's no fmt_spec,
then we know the result of format(value) is value
itself. In that case, skip calling format(). I plan to
move this optimization in to PyObject_Format()
itself. */
if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
/* Do nothing, just transfer ownership to result. */
result = value;
} else {
/* Actually call format(). */
result = PyObject_Format(value, fmt_spec);
Py_DECREF(value);
Py_XDECREF(fmt_spec);
if (result == NULL) {
goto error;
}
}

But, I think that the needed optimizaion is there:

cpython/Objects/abstract.c

Lines 774 to 777 in 5ffdaf7

if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
if (PyUnicode_CheckExact(obj)) {
return Py_NewRef(obj);
}

So, I think this can be safely removed.
I will send a PR.

Linked PRs

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0