﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36896	Optimize TruncateCharsHTMLParser.process() to avoid redundant sum() calculation	Tarek Nakkouch	absyol	"The `TruncateCharsHTMLParser.process()` method in `django/utils/text.py` recalculates `sum(len(p) for p in self.output)` every time it processes a text chunk. For HTML with multiple text nodes, this repeatedly iterates over the growing output list unnecessarily.

{{{#!python
def process(self, data):
    self.processed_chars += len(data)
    if (self.processed_chars == self.length) and (
        sum(len(p) for p in self.output) + len(data) == len(self.rawdata)
    ):
        self.output.append(data)
        raise self.TruncationCompleted
    output = escape("""".join(data[: self.remaining]))
    return data, output
}}}

== Suggested optimization ==

Cache the output length as `self.output_len` and increment it when appending to `self.output`:

 * Initialize `self.output_len = 0` in `TruncateHTMLParser.__init__()`
 * Increment in `handle_starttag()`, `handle_endtag()`, `handle_data()`, `feed()`, and `process()`
 * Replace `sum(len(p) for p in self.output)` with `self.output_len`

This eliminates redundant iteration over already-processed output."	Cleanup/optimization	closed	Utilities	6.0	Normal	needsinfo			Unreviewed	1	0	0	0	0	0
