From b4c54774574be6c9533f31725cf2c35b51c6b8d7 Mon Sep 17 00:00:00 2001 From: TEC Date: Thu, 21 Mar 2024 13:28:50 +0800 Subject: [PATCH] Ensure that annotations are well-ordered in optim The function annotatedstring_optimize! assumes, but does not ensure that annotations are well ordered. For a small (~2%) performance cost, we can ensure that annotations are indeed well-ordered, which seems worthwhile. --- base/strings/annotated.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base/strings/annotated.jl b/base/strings/annotated.jl index a85cdf1b08bbb..23312f9e52b5f 100644 --- a/base/strings/annotated.jl +++ b/base/strings/annotated.jl @@ -242,9 +242,12 @@ AnnotatedString(s::SubString{<:AnnotatedString}) = annotatedstring(s) """ annotatedstring_optimize!(str::AnnotatedString) -Merge contiguous identical annotations in `str`. +Ensure that the annotations in `str` are well ordered, then merge contiguous +identical annotations. """ function annotatedstring_optimize!(s::AnnotatedString) + issorted(s.annotations, by=_annot_sortkey) || + sort!(s.annotations, by=_annot_sortkey) last_seen = Dict{Pair{Symbol, Any}, Int}() i = 1 while i <= length(s.annotations)