source: josm/trunk/src/org/openstreetmap/josm/tools/AlphanumComparator.java@ 18973

Last change on this file since 18973 was 18973, checked in by taylor.smock, 3 months ago

Fix #23468: Improve performance in the Validator tree window

A large number of entries in the validator tree would cause the UI to lock for
significant periods of time when switching to a layer with many errors. Most of
the time spent was in AlphanumComparator.compare.

We need to use AlphanumComparator since String.compare would improperly sort
strings with accented characters.

The performance optimizations for this patch come from the following locations:

  • Extracting string chunk comparison to its own method (which can be compiled to native code)
  • Using String.substring instead of a StringBuilder when getting a string chunk for comparison

Both of those methods may be compiled to native code, but absent code compilation,
the performance improvements are as follows (as measured using an overpass
download of Mesa County, Colorado):

  • -86.4% CPU usage (3.366s to 0.459s)
  • -99.9% memory allocations (2.37 GB to 2.07 MB)
  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4/*
5 * The Alphanum Algorithm is an improved sorting algorithm for strings
6 * containing numbers. Instead of sorting numbers in ASCII order like a standard
7 * sort, this algorithm sorts numbers in numeric order.
8 *
9 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
10 *
11 * Released under the MIT License - https://opensource.org/licenses/MIT
12 *
13 * Copyright 2007-2017 David Koelle
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included
23 * in all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
29 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
30 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
31 * USE OR OTHER DEALINGS IN THE SOFTWARE.
32 */
33import java.io.Serializable;
34import java.text.Collator;
35import java.util.Comparator;
36
37/**
38 * The Alphanum Algorithm is an improved sorting algorithm for strings
39 * containing numbers: Instead of sorting numbers in ASCII order like a standard
40 * sort, this algorithm sorts numbers in numeric order.
41 *
42 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
43 *
44 * This is an updated version with enhancements made by Daniel Migowski, Andre
45 * Bogus, David Koelle and others.
46 *
47 */
48public final class AlphanumComparator implements Comparator<String>, Serializable {
49
50 private static final long serialVersionUID = 1L;
51
52 private static final AlphanumComparator INSTANCE = new AlphanumComparator();
53
54 /**
55 * Replies the unique instance.
56 * @return the unique instance
57 */
58 public static AlphanumComparator getInstance() {
59 return INSTANCE;
60 }
61
62 /**
63 * Constructs a new Alphanum Comparator.
64 */
65 private AlphanumComparator() {
66 }
67
68 /**
69 * Returns an alphanum chunk.
70 * Length of string is passed in for improved efficiency (only need to calculate it once).
71 * @param s string
72 * @param slength string length
73 * @param marker position
74 * @return alphanum chunk found at given position
75 */
76 private static String getChunk(String s, int slength, int marker) {
77 final int startMarker = marker;
78 char c = s.charAt(marker);
79 marker++;
80 if (Character.isDigit(c)) {
81 while (marker < slength) {
82 c = s.charAt(marker);
83 if (!Character.isDigit(c)) {
84 break;
85 }
86 marker++;
87 }
88 } else {
89 while (marker < slength) {
90 c = s.charAt(marker);
91 if (Character.isDigit(c)) {
92 break;
93 }
94 marker++;
95 }
96 }
97 return s.substring(startMarker, marker);
98 }
99
100 /**
101 * Check if a string is ASCII only
102 * @param string The string to check
103 * @param stringLength The length of the string (for performance reasons)
104 * @return {@code true} if the string only contains ascii characters
105 */
106 private static boolean isAscii(String string, int stringLength) {
107 for (int i = 0; i < stringLength; i++) {
108 char c = string.charAt(i);
109 if (c >= 128) {
110 return false;
111 }
112 }
113 return true;
114 }
115
116 /**
117 * Compare two string chunks
118 * @param thisChunk The first chunk to compare
119 * @param thisChunkLength The length of the first chunk (for performance reasons)
120 * @param thatChunk The second chunk to compare
121 * @param thatChunkLength The length of the second chunk (for performance reasons)
122 * @return The {@link Comparator} result
123 */
124 private static int compareChunk(String thisChunk, int thisChunkLength, String thatChunk, int thatChunkLength) {
125 int result;
126 if (Character.isDigit(thisChunk.charAt(0)) && Character.isDigit(thatChunk.charAt(0))) {
127 // Simple chunk comparison by length.
128 result = thisChunkLength - thatChunkLength;
129 // If equal, the first different number counts
130 if (result == 0) {
131 for (int i = 0; i < thisChunkLength; i++) {
132 result = thisChunk.charAt(i) - thatChunk.charAt(i);
133 if (result != 0) {
134 return result;
135 }
136 }
137 }
138 } else {
139 // Check if both chunks are ascii only
140 if (isAscii(thisChunk, thisChunkLength) && isAscii(thatChunk, thatChunkLength)) {
141 return thisChunk.compareTo(thatChunk);
142 }
143 // Instantiate the collator
144 Collator compareOperator = Collator.getInstance();
145 // Compare regardless of accented letters
146 compareOperator.setStrength(Collator.SECONDARY);
147 result = compareOperator.compare(thisChunk, thatChunk);
148 }
149 return result;
150 }
151
152 @Override
153 public int compare(String s1, String s2) {
154 if (s1 == null && s2 == null) {
155 return 0;
156 } else if (s1 == null) {
157 return -1;
158 } else if (s2 == null) {
159 return 1;
160 }
161
162 int thisMarker = 0;
163 int thatMarker = 0;
164 int s1Length = s1.length();
165 int s2Length = s2.length();
166
167 while (thisMarker < s1Length && thatMarker < s2Length) {
168 final String thisChunk = getChunk(s1, s1Length, thisMarker);
169 final int thisChunkLength = thisChunk.length();
170 thisMarker += thisChunkLength;
171
172 String thatChunk = getChunk(s2, s2Length, thatMarker);
173 final int thatChunkLength = thatChunk.length();
174 thatMarker += thatChunkLength;
175
176 // If both chunks contain numeric characters, sort them numerically
177 int result = compareChunk(thisChunk, thisChunkLength, thatChunk, thatChunkLength);
178
179 if (result != 0) {
180 return result;
181 }
182 }
183
184 return s1Length - s2Length;
185 }
186}
Note: See TracBrowser for help on using the repository browser.