diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2012-05-09 20:24:08 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2012-05-09 20:24:08 (GMT) |
commit | ee4544c9202793aa6731d3c12c1e898b15eea94d (patch) | |
tree | 38c6248329f3e21d683538670acb1b41c771b606 /Objects/stringlib | |
parent | c1fdad3e48640e772089e0f64e5e3ccb75e72215 (diff) | |
download | cpython-ee4544c9202793aa6731d3c12c1e898b15eea94d.zip cpython-ee4544c9202793aa6731d3c12c1e898b15eea94d.tar.gz cpython-ee4544c9202793aa6731d3c12c1e898b15eea94d.tar.bz2 |
Issue #14744: Inline unicode_writer_write_char() and unicode_write_str()
Optimize also PyUnicode_Format(): call unicode_writer_prepare() only once
per argument.
Diffstat (limited to 'Objects/stringlib')
-rw-r--r-- | Objects/stringlib/unicode_format.h | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/Objects/stringlib/unicode_format.h b/Objects/stringlib/unicode_format.h index 85a29f5..cdd3b29 100644 --- a/Objects/stringlib/unicode_format.h +++ b/Objects/stringlib/unicode_format.h @@ -500,6 +500,7 @@ render_field(PyObject *fieldobj, SubString *format_spec, unicode_writer_t *write PyObject *result = NULL; PyObject *format_spec_object = NULL; PyObject *(*formatter)(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL; + Py_ssize_t len; /* If we know the type exactly, skip the lookup of __format__ and just call the formatter directly. */ @@ -533,12 +534,19 @@ render_field(PyObject *fieldobj, SubString *format_spec, unicode_writer_t *write result = PyObject_Format(fieldobj, format_spec_object); } - if (result == NULL || PyUnicode_READY(result) == -1) + if (result == NULL) + goto done; + if (PyUnicode_READY(result) == -1) goto done; - assert(PyUnicode_Check(result)); - - ok = (unicode_writer_write_str(writer, result, 0, PyUnicode_GET_LENGTH(result)) == 0); + len = PyUnicode_GET_LENGTH(result); + if (unicode_writer_prepare(writer, + len, PyUnicode_MAX_CHAR_VALUE(result)) == -1) + goto done; + copy_characters(writer->buffer, writer->pos, + result, 0, len); + writer->pos += len; + ok = 1; done: Py_XDECREF(format_spec_object); Py_XDECREF(result); @@ -873,7 +881,8 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs, SubString literal; SubString field_name; SubString format_spec; - Py_UCS4 conversion; + Py_UCS4 conversion, maxchar; + Py_ssize_t sublen; int err; MarkupIterator_init(&iter, input->str, input->start, input->end); @@ -881,11 +890,18 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs, &field_name, &format_spec, &conversion, &format_spec_needs_expanding)) == 2) { - err = unicode_writer_write_str(writer, - literal.str, literal.start, - literal.end - literal.start); - if (err == -1) - return 0; + sublen = literal.end - literal.start; + if (sublen) { + maxchar = _PyUnicode_FindMaxChar(literal.str, + literal.start, literal.end); + err = unicode_writer_prepare(writer, sublen, maxchar); + if (err == -1) + return 0; + copy_characters(writer->buffer, writer->pos, + literal.str, literal.start, sublen); + writer->pos += sublen; + } + if (field_present) if (!output_markup(&field_name, &format_spec, format_spec_needs_expanding, conversion, writer, |