Skip to content

Commit 9e2f9e8

Browse files
committed
<action dev="ggregory" type="fix" issue="CODEC-163" due-to="leo141">ColognePhonetic encoder unneccessarily creates many char arrays on every loop run.</action>
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1412589 13f79535-47bb-0310-9956-ffa450edef68
1 parent 53e90ba commit 9e2f9e8

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove.
4848
</release>
4949
-->
5050
<release version="1.8" date="TBA" description="Feature and fix release.">
51+
<action dev="ggregory" type="fix" issue="CODEC-163" due-to="leo141">ColognePhonetic encoder unneccessarily creates many char arrays on every loop run.</action>
5152
<action dev="sebb" type="fix" issue="CODEC-160">Base64.encodeBase64URLSafeString doesn't add padding characters at the end.</action>
5253
</release>
5354
<release version="1.7" date="11 September 2012" description="

src/main/java/org/apache/commons/codec/language/ColognePhonetic.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@
179179
*/
180180
public class ColognePhonetic implements StringEncoder {
181181

182+
// Predefined char arrays for better performance and less GC load
183+
private static final char[] AEIJOUY = new char[] { 'A', 'E', 'I', 'J', 'O', 'U', 'Y' };
184+
private static final char[] SCZ = new char[] { 'S', 'C', 'Z' };
185+
private static final char[] WFPV = new char[] { 'W', 'F', 'P', 'V' };
186+
private static final char[] GKQ = new char[] { 'G', 'K', 'Q' };
187+
private static final char[] CKQ = new char[] { 'C', 'K', 'Q' };
188+
private static final char[] AHKLOQRUX = new char[] { 'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X' };
189+
private static final char[] SZ = new char[] { 'S', 'Z' };
190+
private static final char[] AHOUKQX = new char[] { 'A', 'H', 'O', 'U', 'K', 'Q', 'X' };
191+
private static final char[] TDX = new char[] { 'T', 'D', 'X' };
192+
182193
/**
183194
* This class is not thread-safe; the field {@link #length} is mutable.
184195
* However, it is not shared between threads, as it is constructed on demand
@@ -331,7 +342,7 @@ public String colognePhonetic(String text) {
331342
nextChar = '-';
332343
}
333344

334-
if (arrayContains(new char[]{'A', 'E', 'I', 'J', 'O', 'U', 'Y'}, chr)) {
345+
if (arrayContains(AEIJOUY, chr)) {
335346
code = '0';
336347
} else if (chr == 'H' || chr < 'A' || chr > 'Z') {
337348
if (lastCode == '/') {
@@ -340,34 +351,33 @@ public String colognePhonetic(String text) {
340351
code = '-';
341352
} else if (chr == 'B' || (chr == 'P' && nextChar != 'H')) {
342353
code = '1';
343-
} else if ((chr == 'D' || chr == 'T') && !arrayContains(new char[]{'S', 'C', 'Z'}, nextChar)) {
354+
} else if ((chr == 'D' || chr == 'T') && !arrayContains(SCZ, nextChar)) {
344355
code = '2';
345-
} else if (arrayContains(new char[]{'W', 'F', 'P', 'V'}, chr)) {
356+
} else if (arrayContains(WFPV, chr)) {
346357
code = '3';
347-
} else if (arrayContains(new char[]{'G', 'K', 'Q'}, chr)) {
358+
} else if (arrayContains(GKQ, chr)) {
348359
code = '4';
349-
} else if (chr == 'X' && !arrayContains(new char[]{'C', 'K', 'Q'}, lastChar)) {
360+
} else if (chr == 'X' && !arrayContains(CKQ, lastChar)) {
350361
code = '4';
351362
input.addLeft('S');
352363
rightLength++;
353364
} else if (chr == 'S' || chr == 'Z') {
354365
code = '8';
355366
} else if (chr == 'C') {
356367
if (lastCode == '/') {
357-
if (arrayContains(new char[]{'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X'}, nextChar)) {
368+
if (arrayContains(AHKLOQRUX, nextChar)) {
358369
code = '4';
359370
} else {
360371
code = '8';
361372
}
362373
} else {
363-
if (arrayContains(new char[]{'S', 'Z'}, lastChar) ||
364-
!arrayContains(new char[]{'A', 'H', 'O', 'U', 'K', 'Q', 'X'}, nextChar)) {
374+
if (arrayContains(SZ, lastChar) || !arrayContains(AHOUKQX, nextChar)) {
365375
code = '8';
366376
} else {
367377
code = '4';
368378
}
369379
}
370-
} else if (arrayContains(new char[]{'T', 'D', 'X'}, chr)) {
380+
} else if (arrayContains(TDX, chr)) {
371381
code = '8';
372382
} else if (chr == 'R') {
373383
code = '7';

0 commit comments

Comments
 (0)