Skip to content

Commit 786160f

Browse files
committed
Add more memoization and caching to String Operations to increase speed of repeated function calls
1 parent 71c43c3 commit 786160f

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.stevenlagoy</groupId>
66
<artifactId>json-java-objectifier</artifactId>
7-
<version>1.0.2</version>
7+
<version>1.0.3</version>
88
<packaging>jar</packaging>
99
<name>JSON Java Objectifier</name>
1010
<description>Java tools for converting JSON into typed object structures</description>

src/main/java/core/StringOperations.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ public static int findFirstUnquotedChar(String line, char target) {
230230
* @see #isInString(String, int)
231231
*/
232232
public static String[] splitByUnquotedString(String string, String separator) {
233+
if (splitByUnquotedCache == null) splitByUnquotedCache = new HashMap<>();
234+
235+
// Check if the string and separator are already in the cache.
236+
String[] splitByUnquotedArr = splitByUnquotedCache.get(Map.of(string, separator));
237+
if (splitByUnquotedArr != null) {
238+
return splitByUnquotedArr;
239+
}
240+
241+
// Otherwise, precompute and add result to the cache.
233242
List<String> parts = new ArrayList<>();
234243
int lastSplitIndex = 0;
235244

@@ -251,8 +260,11 @@ public static String[] splitByUnquotedString(String string, String separator) {
251260
parts.add(string.substring(lastSplitIndex).trim());
252261
}
253262

263+
splitByUnquotedCache.put(Map.of(string, separator), parts.toArray(new String[0]));
254264
return parts.toArray(new String[0]);
255265
}
266+
private static Map<Map<String, String>, String[]> splitByUnquotedCache;
267+
public static void clearSplitByUnquotedCache() { splitByUnquotedCache = null; }
256268

257269
/**
258270
* Splits the input string around occurrences of the given separator string, ignoring separators that appear inside
@@ -320,6 +332,15 @@ public static String[] splitByUnquotedString(String string, String separator, in
320332
* @see #isInArray(String, int)
321333
*/
322334
public static String[] splitByStringNotInArray(String string, String separator) {
335+
if (splitByNotInArrayCache == null) splitByNotInArrayCache = new HashMap<>();
336+
337+
// Check if the string and separator is already in the cache.
338+
String[] splitByNotInArrayArr = splitByNotInArrayCache.get(Map.of(string, separator));
339+
if (splitByNotInArrayArr != null) {
340+
return splitByNotInArrayArr;
341+
}
342+
343+
// Otherwise, precompute and add result to the cache.
323344
List<String> parts = new ArrayList<>();
324345
int lastSplitIndex = 0;
325346

@@ -341,10 +362,22 @@ public static String[] splitByStringNotInArray(String string, String separator)
341362
parts.add(string.substring(lastSplitIndex).trim());
342363
}
343364

365+
splitByNotInArrayCache.put(Map.of(string, separator), parts.toArray(new String[0]));
344366
return parts.toArray(new String[0]);
345367
}
368+
private static Map<Map<String, String>, String[]> splitByNotInArrayCache;
369+
public static void clearSplitByNotInArrayCache() { splitByNotInArrayCache = null; }
346370

347371
public static String[] splitByStringNotInObject(String string, String separator) {
372+
if (splitByNotInObjectCache == null) splitByNotInObjectCache = new HashMap<>();
373+
374+
// Check if the string and separator are already in the cache.
375+
String[] splitByNotInObjectArr = splitByNotInObjectCache.get(Map.of(string, separator));
376+
if (splitByNotInObjectArr != null) {
377+
return splitByNotInObjectArr;
378+
}
379+
380+
// Otherwise, precompute and add result to the cache.
348381
List<String> parts = new ArrayList<>();
349382
int lastSplitIndex = 0;
350383

@@ -366,8 +399,11 @@ public static String[] splitByStringNotInObject(String string, String separator)
366399
parts.add(string.substring(lastSplitIndex).trim());
367400
}
368401

402+
splitByNotInObjectCache.put(Map.of(string, separator), parts.toArray(new String[0]));
369403
return parts.toArray(new String[0]);
370404
}
405+
private static Map<Map<String, String>, String[]> splitByNotInObjectCache;
406+
public static void clearSplitByNotInObjectCache() { splitByNotInObjectCache = null; }
371407

372408
/**
373409
* Splits a string by a separator only when it is not inside an object or an array.
@@ -378,6 +414,15 @@ public static String[] splitByStringNotInObject(String string, String separator)
378414
* @return
379415
*/
380416
public static String[] splitByStringNotNested(String string, String separator) {
417+
if (splitByNotNestedCache == null) splitByNotNestedCache = new HashMap<>();
418+
419+
// Check if the string and separator are already in the cache.
420+
String[] splitByNotNestedArr = splitByNotNestedCache.get(Map.of(string, separator));
421+
if (splitByNotNestedArr != null) {
422+
return splitByNotNestedArr;
423+
}
424+
425+
// Otherwise, precompute and add result to the cache.
381426
List<String> parts = new ArrayList<>();
382427
int lastSplitIndex = 0;
383428

@@ -399,8 +444,11 @@ public static String[] splitByStringNotNested(String string, String separator) {
399444
parts.add(string.substring(lastSplitIndex).trim());
400445
}
401446

447+
splitByNotNestedCache.put(Map.of(string, separator), parts.toArray(new String[0]));
402448
return parts.toArray(new String[0]);
403449
}
450+
private static Map<Map<String, String>, String[]> splitByNotNestedCache;
451+
public static void clearSplitByNotNestedCache() { splitByNotNestedCache = null; }
404452

405453
/**
406454
* Replaces the last occurrence of a specified regular expression with a replacement string in the input text.

0 commit comments

Comments
 (0)