Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions Include/internal/pycore_interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ extern PyTypeObject _PyInterpolation_Type;

#define _PyInterpolation_CheckExact(op) Py_IS_TYPE((op), &_PyInterpolation_Type)

PyAPI_FUNC(PyObject *) _PyInterpolation_Build(PyObject *value, PyObject *str,
int conversion, PyObject *format_spec);
// Steals references to value, str, and format_spec (even on failure).
PyAPI_FUNC(PyObject *) _PyInterpolation_BuildSteal(PyObject *value, PyObject *str,
int conversion, PyObject *format_spec);

extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *interp);
extern PyObject *_PyInterpolation_GetValueRef(PyObject *interpolation);
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ extern PyTypeObject _PyTemplateIter_Type;

extern PyObject *_PyTemplate_Concat(PyObject *self, PyObject *other);

PyAPI_FUNC(PyObject *) _PyTemplate_Build(PyObject *strings, PyObject *interpolations);
// Steals references to strings and interpolations (even on failure).
PyAPI_FUNC(PyObject *) _PyTemplate_BuildSteal(PyObject *strings, PyObject *interpolations);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Eliminate unnecessary reference count operations in ``BUILD_INTERPOLATION``
and ``BUILD_TEMPLATE`` bytecode instructions by making
:c:func:`!_PyInterpolation_BuildSteal` and :c:func:`!_PyTemplate_BuildSteal`
steal references to their arguments.
51 changes: 10 additions & 41 deletions Modules/_testinternalcapi/test_cases.c.h

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

4 changes: 2 additions & 2 deletions Modules/_testinternalcapi/testbytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS
#include "pycore_function.h"
#include "pycore_instruments.h"
#include "pycore_interpolation.h" // _PyInterpolation_Build()
#include "pycore_interpolation.h" // _PyInterpolation_BuildSteal()
#include "pycore_intrinsics.h"
#include "pycore_long.h" // _PyLong_ExactDealloc(), _PyLong_GetZero()
#include "pycore_moduleobject.h" // PyModuleObject
Expand All @@ -29,7 +29,7 @@
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
#include "pycore_stackref.h"
#include "pycore_template.h" // _PyTemplate_Build()
#include "pycore_template.h" // _PyTemplate_BuildSteal()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_typeobject.h" // _PySuper_Lookup()

Expand Down
11 changes: 7 additions & 4 deletions Objects/interpolationobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,19 @@ _PyInterpolation_InitTypes(PyInterpreterState *interp)
}

PyObject *
_PyInterpolation_Build(PyObject *value, PyObject *str, int conversion, PyObject *format_spec)
_PyInterpolation_BuildSteal(PyObject *value, PyObject *str, int conversion, PyObject *format_spec)
{
interpolationobject *interpolation = PyObject_GC_New(interpolationobject, &_PyInterpolation_Type);
if (!interpolation) {
Py_DECREF(value);
Py_DECREF(str);
Py_DECREF(format_spec);
return NULL;
}

interpolation->value = Py_NewRef(value);
interpolation->expression = Py_NewRef(str);
interpolation->format_spec = Py_NewRef(format_spec);
interpolation->value = value;
interpolation->expression = str;
interpolation->format_spec = format_spec;
interpolation->conversion = NULL;

if (conversion == 0) {
Expand Down
18 changes: 7 additions & 11 deletions Objects/templateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ template_new_impl(PyTypeObject *type, PyObject * const *args,
PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty));
}

PyObject *template = _PyTemplate_Build(strings, interpolations);
Py_DECREF(strings);
Py_DECREF(interpolations);
return template;
return _PyTemplate_BuildSteal(strings, interpolations);
}

static void
Expand Down Expand Up @@ -304,10 +301,7 @@ template_concat_templates(templateobject *self, templateobject *other)
return NULL;
}

PyObject *newtemplate = _PyTemplate_Build(newstrings, newinterpolations);
Py_DECREF(newstrings);
Py_DECREF(newinterpolations);
return newtemplate;
return _PyTemplate_BuildSteal(newstrings, newinterpolations);
}

PyObject *
Expand Down Expand Up @@ -415,15 +409,17 @@ PyTypeObject _PyTemplate_Type = {
};

PyObject *
_PyTemplate_Build(PyObject *strings, PyObject *interpolations)
_PyTemplate_BuildSteal(PyObject *strings, PyObject *interpolations)
{
templateobject *template = PyObject_GC_New(templateobject, &_PyTemplate_Type);
if (template == NULL) {
Py_DECREF(strings);
Py_DECREF(interpolations);
return NULL;
}

template->strings = Py_NewRef(strings);
template->interpolations = Py_NewRef(interpolations);
template->strings = strings;
template->interpolations = interpolations;
PyObject_GC_Track(template);
return (PyObject *) template;
}
34 changes: 15 additions & 19 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "pycore_function.h"
#include "pycore_import.h" // _PyImport_LoadLazyImportTstate()
#include "pycore_instruments.h"
#include "pycore_interpolation.h" // _PyInterpolation_Build()
#include "pycore_interpolation.h" // _PyInterpolation_BuildSteal()
#include "pycore_intrinsics.h"
#include "pycore_lazyimportobject.h" // PyLazyImport_CheckExact()
#include "pycore_long.h" // _PyLong_ExactDealloc(), _PyLong_GetZero()
Expand All @@ -33,7 +33,7 @@
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
#include "pycore_stackref.h"
#include "pycore_template.h" // _PyTemplate_Build()
#include "pycore_template.h" // _PyTemplate_BuildSteal()
#include "pycore_tuple.h" // _PyStolenTuple_Free(), _PyTuple_ITEMS()
#include "pycore_typeobject.h" // _PySuper_Lookup()

Expand Down Expand Up @@ -2432,35 +2432,31 @@ dummy_func(
}

inst(BUILD_INTERPOLATION, (value, str, format[oparg & 1] -- interpolation)) {
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
PyObject *str_o = PyStackRef_AsPyObjectBorrow(str);
PyObject *value_o = PyStackRef_AsPyObjectSteal(value);
DEAD(value);
PyObject *str_o = PyStackRef_AsPyObjectSteal(str);
DEAD(str);
int conversion = oparg >> 2;
PyObject *format_o;
if (oparg & 1) {
format_o = PyStackRef_AsPyObjectBorrow(format[0]);
}
else {
format_o = &_Py_STR(empty);
}
PyObject *interpolation_o = _PyInterpolation_Build(value_o, str_o, conversion, format_o);
if (oparg & 1) {
PyStackRef_CLOSE(format[0]);
format_o = PyStackRef_AsPyObjectSteal(format[0]);
DEAD(format);
}
else {
format_o = Py_NewRef(&_Py_STR(empty));
DEAD(format);
}
PyStackRef_CLOSE(str);
PyStackRef_CLOSE(value);
PyObject *interpolation_o = _PyInterpolation_BuildSteal(value_o, str_o, conversion, format_o);
ERROR_IF(interpolation_o == NULL);
interpolation = PyStackRef_FromPyObjectSteal(interpolation_o);
}

inst(BUILD_TEMPLATE, (strings, interpolations -- template)) {
PyObject *strings_o = PyStackRef_AsPyObjectBorrow(strings);
PyObject *interpolations_o = PyStackRef_AsPyObjectBorrow(interpolations);
PyObject *template_o = _PyTemplate_Build(strings_o, interpolations_o);
PyStackRef_CLOSE(interpolations);
PyStackRef_CLOSE(strings);
PyObject *strings_o = PyStackRef_AsPyObjectSteal(strings);
DEAD(strings);
PyObject *interpolations_o = PyStackRef_AsPyObjectSteal(interpolations);
DEAD(interpolations);
PyObject *template_o = _PyTemplate_BuildSteal(strings_o, interpolations_o);
ERROR_IF(template_o == NULL);
template = PyStackRef_FromPyObjectSteal(template_o);
}
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "pycore_import.h" // _PyImport_IsDefaultImportFunc()
#include "pycore_instruments.h"
#include "pycore_interpframe.h" // _PyFrame_SetStackPointer()
#include "pycore_interpolation.h" // _PyInterpolation_Build()
#include "pycore_interpolation.h" // _PyInterpolation_BuildSteal()
#include "pycore_intrinsics.h"
#include "pycore_jit.h"
#include "pycore_lazyimportobject.h"
Expand All @@ -35,7 +35,7 @@
#include "pycore_setobject.h" // _PySet_Update()
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString()
#include "pycore_template.h" // _PyTemplate_Build()
#include "pycore_template.h" // _PyTemplate_BuildSteal()
#include "pycore_traceback.h" // _PyTraceBack_FromFrame
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_uop_ids.h" // Uops
Expand Down
55 changes: 9 additions & 46 deletions Python/executor_cases.c.h

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

Loading
Loading