From 9f1854acf8a13de98b5706d1246baa15f15f377e Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 18 Jan 2022 10:36:33 +0100 Subject: JSON: Further improve the duplicate handling in the parser Avoid some unnecessary comparisons and add more tests. Task-number: QTBUG-99799 Change-Id: I3aee9f0b62461d38dadbe8e969444e1cd1f94e68 Reviewed-by: Sona Kurazyan --- src/corelib/serialization/qjsonparser.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/corelib/serialization/qjsonparser.cpp b/src/corelib/serialization/qjsonparser.cpp index df66fc2772..d8d0ad9c2e 100644 --- a/src/corelib/serialization/qjsonparser.cpp +++ b/src/corelib/serialization/qjsonparser.cpp @@ -389,12 +389,24 @@ static Iterator customAssigningUniqueLast(Iterator first, Iterator last, if (first == last) return last; - Iterator result = first; + // After adjacent_find, we know that *first and *(first+1) compare equal, + // and that first+1 != last. + Iterator result = first++; + Q_ASSERT(compare(*result, *first)); + assign(*result, *first); + Q_ASSERT(first != last); + while (++first != last) { if (!compare(*result, *first)) ++result; - if (result != first) - assign(*result, *first); + + // Due to adjacent_find above, we know that we've at least eliminated one element. + // Therefore we have to move each further element across the gap. + Q_ASSERT(result != first); + + // We have to overwrite each element we want to eliminate, to deref() the container. + // Therefore we don't try to optimize the number of assignments here. + assign(*result, *first); } return ++result; -- cgit v1.2.3