Skip to content

Commit 90b68a3

Browse files
committed
optimise standard parser path
1 parent 2b8d9fd commit 90b68a3

File tree

10 files changed

+62
-252
lines changed

10 files changed

+62
-252
lines changed

lightningcsv/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@
103103
<optional>true</optional>
104104
<scope>test</scope>
105105
</dependency>
106-
<dependency>
107-
<groupId>com.boundary</groupId>
108-
<artifactId>fasttuple-core</artifactId>
109-
<version>0.1</version>
110-
<optional>true</optional>
111-
<scope>test</scope>
112-
</dependency>
113106
</dependencies>
114107

115108
<build>

lightningcsv/src/main/java/org/simpleflatmapper/lightningcsv/impl/AsmCharConsumerFactory.java

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
import org.simpleflatmapper.ow2asm.Opcodes;
1616
import org.simpleflatmapper.util.FactoryClassLoader;
1717

18+
import java.lang.invoke.MethodHandle;
19+
import java.lang.invoke.MethodHandles;
1820
import java.lang.reflect.Constructor;
21+
import java.lang.reflect.Method;
1922
import java.util.Arrays;
2023
import java.util.HashSet;
2124
import java.util.Iterator;
@@ -31,26 +34,31 @@
3134

3235
public class AsmCharConsumerFactory extends CharConsumerFactory {
3336
private static final AtomicInteger id = new AtomicInteger();
34-
37+
3538
public AbstractCharConsumer newCharConsumer(TextFormat textFormat, CharBuffer charBuffer, CellPreProcessor cellTransformer, boolean specialisedCharConsumer) {
3639
if (specialisedCharConsumer) {
37-
SpecialisationKey key = new SpecialisationKey(cellTransformer.ignoreLeadingSpace(), textFormat, cellTransformer.getClass());
3840

39-
Constructor<? extends AbstractCharConsumer> constructor;
41+
MethodHandle constructor;
4042

41-
if (key.equals(RFC4180)) {
42-
constructor = RFC4180_CC;
43-
} else {
44-
constructor = specialisedCharConsumers.get(key);
45-
if (constructor == null && specialisedCharConsumers.size() < 64) {
46-
constructor = createNewSpecialisedCharConsumer(key);
43+
if (RFC4180_CC != null && isRfc4180(cellTransformer, textFormat)) {
44+
try {
45+
return (AbstractCharConsumer) RFC4180_CC.invokeExact(charBuffer, textFormat, cellTransformer);
46+
}catch (Throwable e) {
47+
throw new RuntimeException(e.getMessage(), e);
4748
}
4849
}
4950

51+
SpecialisationKey key = new SpecialisationKey(cellTransformer.ignoreLeadingSpace(), textFormat, cellTransformer.getClass());
52+
53+
constructor = specialisedCharConsumers.get(key);
54+
if (constructor == null && specialisedCharConsumers.size() < 64) {
55+
constructor = createNewSpecialisedCharConsumer(key);
56+
}
57+
5058
if (constructor != null) {
5159
try {
52-
return constructor.newInstance(charBuffer, key.textFormat, cellTransformer);
53-
} catch (Exception e) {
60+
return (AbstractCharConsumer) constructor.invokeExact(charBuffer, key.textFormat, cellTransformer);
61+
} catch (Throwable e) {
5462
throw new RuntimeException(e.getMessage(), e);
5563
}
5664
}
@@ -60,16 +68,25 @@ public AbstractCharConsumer newCharConsumer(TextFormat textFormat, CharBuffer ch
6068

6169
}
6270

71+
private boolean isRfc4180(CellPreProcessor cellTransformer, TextFormat textFormat) {
72+
return (RFC4180.ignoreLeadingSpace == cellTransformer.ignoreLeadingSpace())
73+
&& textFormat.equals(RFC4180.textFormat)
74+
&& cellTransformer.getClass().equals(RFC4180.cellTransformer);
75+
}
76+
6377

64-
private static Constructor<? extends AbstractCharConsumer> createNewSpecialisedCharConsumer(SpecialisationKey key) {
78+
private static MethodHandle createNewSpecialisedCharConsumer(SpecialisationKey key) {
6579
synchronized (lock) {
66-
Constructor<? extends AbstractCharConsumer> constructor;
80+
MethodHandle constructor;
6781
constructor = specialisedCharConsumers.get(key);
6882
if (constructor == null) {
69-
if (specialisedCharConsumers.size() < 64) { // artificial limit to avoid DOS
70-
constructor = generateSpecialisedCharConsumer(key);
71-
if (constructor != null) {
72-
specialisedCharConsumers.put(key, constructor);
83+
if (specialisedCharConsumers.size() < 64) {
84+
try {// artificial limit to avoid DOS
85+
constructor = MethodHandles.lookup().unreflect(generateSpecialisedCharConsumer(key));
86+
if (constructor != null) {
87+
specialisedCharConsumers.put(key, constructor);
88+
}
89+
} catch (Throwable t) {
7390
}
7491
}
7592
}
@@ -78,7 +95,7 @@ private static Constructor<? extends AbstractCharConsumer> createNewSpecialisedC
7895
}
7996

8097
@SuppressWarnings("unchecked")
81-
private static Constructor<? extends AbstractCharConsumer> generateSpecialisedCharConsumer(final SpecialisationKey key) {
98+
private static Method generateSpecialisedCharConsumer(final SpecialisationKey key) {
8299
try {
83100
final String newName = "org/simpleflatmapper/lightningcsv/parser/Asm_"
84101
+ (key.ignoreLeadingSpace ? "Ils_" : "")
@@ -94,7 +111,7 @@ private static Constructor<? extends AbstractCharConsumer> generateSpecialisedCh
94111
ClassReader reader = new ClassReader(
95112
ConfigurableCharConsumer.class.getResourceAsStream("ConfigurableCharConsumer.class")
96113
);
97-
ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS |
114+
ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS |
98115
ClassWriter.COMPUTE_FRAMES);
99116
ClassVisitor visitor = new ClassVisitor(AsmUtils.API, writer) {
100117
@Override
@@ -155,8 +172,8 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
155172

156173
} else {
157174
return new MethodVisitor(AsmUtils.API, mv) {
158-
159-
175+
176+
160177
public void visitTypeInsn(int i, String s) {
161178
if (oldNames.contains(s)) {
162179
s = newName;
@@ -215,7 +232,7 @@ private String fix(String s) {
215232

216233
Class<?> clazz = classLoader.registerClass(className, bytes);
217234

218-
return (Constructor<? extends AbstractCharConsumer>) clazz.getConstructor(CharBuffer.class, TextFormat.class, CellPreProcessor.class);
235+
return clazz.getMethod("of", CharBuffer.class, TextFormat.class, CellPreProcessor.class);
219236

220237
} catch (Exception e) {
221238
// ignore
@@ -227,19 +244,25 @@ private String fix(String s) {
227244
private static final FactoryClassLoader classLoader = new FactoryClassLoader(AbstractCharConsumer.class.getClassLoader());
228245
private static final Object lock = new Object();
229246
private static final SpecialisationKey RFC4180 = new SpecialisationKey(false, new TextFormat(',', '"', '"', false), UnescapeCellPreProcessor.class);
230-
private static final Constructor<? extends AbstractCharConsumer> RFC4180_CC;
247+
private static final MethodHandle RFC4180_CC;
231248

232249
static {
233-
RFC4180_CC = generateSpecialisedCharConsumer(RFC4180);
250+
MethodHandle methodHandle = null;
251+
try {
252+
methodHandle = MethodHandles.lookup().unreflect(generateSpecialisedCharConsumer(RFC4180));
253+
} catch (Throwable t) {
254+
}
255+
RFC4180_CC = methodHandle;
234256
}
235257

236-
private static final ConcurrentHashMap<SpecialisationKey, Constructor<? extends AbstractCharConsumer>> specialisedCharConsumers =
237-
new ConcurrentHashMap<SpecialisationKey, Constructor<? extends AbstractCharConsumer>>();
258+
private static final ConcurrentHashMap<SpecialisationKey, MethodHandle> specialisedCharConsumers =
259+
new ConcurrentHashMap<SpecialisationKey, MethodHandle>();
238260

239261
private static class SpecialisationKey {
240262
final boolean ignoreLeadingSpace;
241263
final TextFormat textFormat;
242264
final Class<?> cellTransformer;
265+
243266
private SpecialisationKey(boolean ignoreLeadingSpace, TextFormat textFormat, Class<?> cellTransformer) {
244267
this.ignoreLeadingSpace = ignoreLeadingSpace;
245268
this.textFormat = textFormat;

lightningcsv/src/main/java/org/simpleflatmapper/lightningcsv/parser/ConfigurableCharConsumer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public final class ConfigurableCharConsumer extends AbstractCharConsumer {
3535
private int _currentIndex = 0;
3636
private int _currentState = NONE;
3737

38+
public static AbstractCharConsumer of(CharBuffer csvBuffer, TextFormat textFormat, CellPreProcessor cellPreProcessor) {
39+
return new ConfigurableCharConsumer(csvBuffer, textFormat, cellPreProcessor);
40+
}
41+
3842
public ConfigurableCharConsumer(CharBuffer csvBuffer, TextFormat textFormat, CellPreProcessor cellPreProcessor) {
3943
this.csvBuffer = csvBuffer;
4044
this._currentIndex = csvBuffer.rowStartMark;

sfm-csv/pom.xml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,7 @@
132132
<jdk>1.7</jdk>
133133
</activation>
134134
<dependencies>
135-
<dependency>
136-
<groupId>com.boundary</groupId>
137-
<artifactId>fasttuple-core</artifactId>
138-
<version>0.1</version>
139-
<optional>true</optional>
140-
<scope>test</scope>
141-
</dependency>
135+
142136
</dependencies>
143137
<build>
144138
<plugins>
@@ -167,13 +161,7 @@
167161
<optional>true</optional>
168162
<scope>test</scope>
169163
</dependency>
170-
<dependency>
171-
<groupId>com.boundary</groupId>
172-
<artifactId>fasttuple-core</artifactId>
173-
<version>0.1</version>
174-
<optional>true</optional>
175-
<scope>test</scope>
176-
</dependency>
164+
177165
</dependencies>
178166
</profile>
179167

@@ -190,13 +178,7 @@
190178
<optional>true</optional>
191179
<scope>test</scope>
192180
</dependency>
193-
<dependency>
194-
<groupId>com.boundary</groupId>
195-
<artifactId>fasttuple-core</artifactId>
196-
<version>0.1</version>
197-
<optional>true</optional>
198-
<scope>test</scope>
199-
</dependency>
181+
200182
</dependencies>
201183

202184
<build>

sfm-csv/src/test/java/org/simpleflatmapper/csv/test/samples/FastTupleTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

sfm-jdbc/pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,7 @@
175175
<jdk>1.7</jdk>
176176
</activation>
177177
<dependencies>
178-
<dependency>
179-
<groupId>com.boundary</groupId>
180-
<artifactId>fasttuple-core</artifactId>
181-
<version>0.1</version>
182-
<optional>true</optional>
183-
<scope>test</scope>
184-
</dependency>
178+
185179
<dependency>
186180
<groupId>org.eclipse.persistence</groupId>
187181
<artifactId>javax.persistence</artifactId>

sfm-map/pom.xml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,7 @@
177177
<jdk>1.7</jdk>
178178
</activation>
179179
<dependencies>
180-
<dependency>
181-
<groupId>com.boundary</groupId>
182-
<artifactId>fasttuple-core</artifactId>
183-
<version>0.1</version>
184-
<optional>true</optional>
185-
<scope>test</scope>
186-
</dependency>
180+
187181
</dependencies>
188182
<build>
189183
<plugins>
@@ -212,13 +206,7 @@
212206
<optional>true</optional>
213207
<scope>test</scope>
214208
</dependency>
215-
<dependency>
216-
<groupId>com.boundary</groupId>
217-
<artifactId>fasttuple-core</artifactId>
218-
<version>0.1</version>
219-
<optional>true</optional>
220-
<scope>test</scope>
221-
</dependency>
209+
222210
</dependencies>
223211
</profile>
224212

@@ -235,13 +223,7 @@
235223
<optional>true</optional>
236224
<scope>test</scope>
237225
</dependency>
238-
<dependency>
239-
<groupId>com.boundary</groupId>
240-
<artifactId>fasttuple-core</artifactId>
241-
<version>0.1</version>
242-
<optional>true</optional>
243-
<scope>test</scope>
244-
</dependency>
226+
245227
</dependencies>
246228

247229
<build>

sfm-poi/pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,7 @@
134134
<optional>true</optional>
135135
<scope>test</scope>
136136
</dependency>
137-
<dependency>
138-
<groupId>com.boundary</groupId>
139-
<artifactId>fasttuple-core</artifactId>
140-
<version>0.1</version>
141-
<optional>true</optional>
142-
<scope>test</scope>
143-
</dependency>
137+
144138
</dependencies>
145139

146140
<build>

sfm-reflect/pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,7 @@
151151
<optional>true</optional>
152152
<scope>test</scope>
153153
</dependency>
154-
<dependency>
155-
<groupId>com.boundary</groupId>
156-
<artifactId>fasttuple-core</artifactId>
157-
<version>0.1</version>
158-
<optional>true</optional>
159-
<scope>test</scope>
160-
</dependency>
154+
161155
</dependencies>
162156

163157
<build>

0 commit comments

Comments
 (0)