From 78a8e561b25d6a41e63d51bf49afb8622b278009 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 15 Nov 2021 18:03:09 +0100 Subject: [PATCH] QRegularExpression: do not mark as dirty on no-op setters Quite some code in Qt takes a copy of a QRegularExpression and then changes something on it (e.g. the case sensitivity) based on some other criteria: QRegularExpression copy = orig; if (cond) copy.setPatternOptions(copy.patternOptions() | CaseInsensitive); use(copy); This pattern can be found in QAIM, QString-related classes, and so on. The "problem" is that if the attempted modification is a no-op, we'd still invalidate the copy object (by detaching). Even if no detaches are involved, setting the same pattern or options on a QRE object shouldn't require a relatively expensive pattern recompile. In summary: don't detach/mark a QRE object as dirty if a setter didn't actually do a modification. Change-Id: Iae0ab4a5e443e7285a83d3d7e0f1dcfd66e8d51d Reviewed-by: Thiago Macieira --- src/corelib/text/qregularexpression.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 56e222fa8f..4fe86e9d6a 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -1452,6 +1452,8 @@ QString QRegularExpression::pattern() const */ void QRegularExpression::setPattern(const QString &pattern) { + if (d->pattern == pattern) + return; d.detach(); d->isDirty = true; d->pattern = pattern; @@ -1475,6 +1477,8 @@ QRegularExpression::PatternOptions QRegularExpression::patternOptions() const */ void QRegularExpression::setPatternOptions(PatternOptions options) { + if (d->patternOptions == options) + return; d.detach(); d->isDirty = true; d->patternOptions = options;