Skip to content

Commit 5301a40

Browse files
committed
Selective interning
1 parent 2351c17 commit 5301a40

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

onebusaway-csv-entities/src/main/java/org/onebusaway/csv_entities/CsvEntityReader.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import java.io.Reader;
2222
import java.util.ArrayList;
2323
import java.util.HashMap;
24+
import java.util.HashSet;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Set;
28+
import java.util.function.Predicate;
2629
import java.util.zip.ZipFile;
2730
import org.onebusaway.csv_entities.exceptions.CsvEntityIOException;
2831
import org.onebusaway.csv_entities.exceptions.MissingRequiredEntityException;
@@ -50,6 +53,8 @@ public class CsvEntityReader {
5053

5154
private boolean _internStrings = false;
5255

56+
private Predicate<Class> _internStringsDisabled;
57+
5358
private Map<String, String> _stringTable = new HashMap<String, String>();
5459

5560
/**
@@ -96,6 +101,15 @@ public void setInternStrings(boolean internStrings) {
96101
_internStrings = internStrings;
97102
}
98103

104+
public void disableInternStringsForEntities(Class<?> ... entityClasses) {
105+
Set<Class> set = new HashSet<>();
106+
for (Class<?> entityClass : entityClasses) {
107+
set.add(entityClass);
108+
}
109+
110+
this._internStringsDisabled = (c) -> set.contains(c);
111+
}
112+
99113
public void readEntities(Class<?> entityClass) throws IOException {
100114
readEntities(entityClass, _source);
101115
}
@@ -132,13 +146,16 @@ public void readEntities(Class<?> entityClass, Reader reader)
132146
String line = null;
133147
int lineNumber = 1;
134148

149+
boolean internStrings =
150+
_internStrings && (_internStringsDisabled == null || !_internStringsDisabled.test(entityClass));
151+
135152
try {
136153
while ((line = lineReader.readLine()) != null) {
137154
if (line.isEmpty()) continue;
138155
// TODO: This is a hack of sorts to deal with a malformed data file...
139156
if (line.length() == 1 && line.charAt(0) == 26) continue;
140157
List<String> values = _tokenizerStrategy.parse(line);
141-
if (_internStrings) internStrings(values);
158+
if (internStrings) internStrings(values);
142159
entityLoader.handleLine(values);
143160
lineNumber++;
144161
}

onebusaway-jmh/src/main/java/org/onebusaway/jmh/gtfs/memory/AbstractParsePrintMemory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public static GtfsRelationalDaoImpl run(boolean internStrings) throws Exception
4444
"abcd",
4545
internStrings,
4646
entityStore,
47-
null);
47+
null,
48+
false);
4849

4950
System.out.println("Read done.");
5051

onebusaway-jmh/src/main/java/org/onebusaway/jmh/gtfs/shape/ShapeSingleShotBenchmark.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.util.concurrent.TimeUnit;
5+
56
import org.onebusaway.csv_entities.CsvInputSource;
67
import org.onebusaway.gtfs.impl.GtfsDaoImpl;
78
import org.onebusaway.gtfs.model.ShapePoint;
@@ -47,45 +48,65 @@ public ThreadState() {
4748
@Benchmark
4849
public Object testParse(ThreadState state) throws Exception {
4950
return processWithGtfsReader(
50-
new File(directory), "abcd", false, state.reader, ShapePoint.class);
51+
new File(directory), "abcd", false, state.reader, ShapePoint.class, false);
5152
}
5253

5354
@Benchmark
5455
public Object testParseStringInterning(ThreadState state) throws Exception {
55-
return processWithGtfsReader(new File(directory), "abcd", true, state.reader, ShapePoint.class);
56+
return processWithGtfsReader(
57+
new File(directory), "abcd", true, state.reader, ShapePoint.class, false);
5658
}
5759

5860
@Benchmark
5961
public Object testParseLegacy(ThreadState state) throws Exception {
6062
return processWithGtfsReader(
61-
new File(directory), "abcd", false, state.reader, LegacyShapePoint.class);
63+
new File(directory), "abcd", false, state.reader, LegacyShapePoint.class, false);
6264
}
6365

6466
@Benchmark
6567
public Object testParseLegacyStringInterning(ThreadState state) throws Exception {
6668
return processWithGtfsReader(
67-
new File(directory), "abcd", true, state.reader, LegacyShapePoint.class);
69+
new File(directory), "abcd", true, state.reader, LegacyShapePoint.class, false);
70+
}
71+
72+
@Benchmark
73+
public Object testParseStringSelectiveInterning(ThreadState state) throws Exception {
74+
return processWithGtfsReader(
75+
new File(directory),
76+
"abcd",
77+
true,
78+
state.reader,
79+
ShapePoint.class,
80+
true);
6881
}
6982

7083
public static GtfsReader processWithEntityStore(
7184
File resourcePath,
7285
String agencyId,
7386
boolean internStrings,
7487
GenericMutableDao entityStore,
75-
Class<?> cls)
88+
Class<?> cls,
89+
boolean disableInternStrings)
7690
throws Exception {
7791
GtfsReader reader = new GtfsReader();
7892
reader.setEntityStore(entityStore);
79-
return processWithGtfsReader(resourcePath, agencyId, internStrings, reader, cls);
93+
return processWithGtfsReader(resourcePath, agencyId, internStrings, reader, cls, disableInternStrings);
8094
}
8195

8296
public static GtfsReader processWithGtfsReader(
83-
File resourcePath, String agencyId, boolean internStrings, GtfsReader reader, Class<?> cls)
97+
File resourcePath,
98+
String agencyId,
99+
boolean internStrings,
100+
GtfsReader reader,
101+
Class<?> cls,
102+
boolean selectivelyDisableInternStrings)
84103
throws Exception {
85104

86105
reader.setDefaultAgencyId(agencyId);
87106
reader.setInternStrings(internStrings);
88-
107+
if(selectivelyDisableInternStrings) {
108+
reader.disableInternStringsForEntities(cls);
109+
}
89110
reader.setInputLocation(resourcePath);
90111

91112
CsvInputSource inputSource = reader.getInputSource();

onebusaway-jmh/src/main/java/org/onebusaway/jmh/gtfs/shape/memory/AbstractParseShapePrintMemory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public static GtfsRelationalDaoImpl run(boolean internStrings, Class<?> cls) thr
4040
"abcd",
4141
internStrings,
4242
entityStore,
43-
cls);
43+
cls,
44+
false);
4445

4546
System.out.println("Read file.");
4647
System.out.println("Memory after parsing:");

0 commit comments

Comments
 (0)