Skip to content

Commit 02ec191

Browse files
committed
v1.4.5: performance for n.s.e.EasyML.deserialize()
1 parent fff132f commit 02ec191

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ for a higher control compared to the EasyML Facade.
6060

6161
### Release Notes
6262

63+
!Release 1.4.5
64+
- performance: improved n.s.e.EasyML.deserialize() speed by reusing the
65+
XmlPullParser when available.
66+
- refactor: minor code improvements.
67+
68+
6369
!Release 1.4.4
6470
- refactor: made n.s.e.m.CompositeStrategy.unmarshalInit return type more
6571
loose to better support readResolve in n.s.e.m.j.i.ExternalizableStrategy.

easyml/src/net/sourceforge/easyml/XMLReader.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
*
7777
* @author Victor Cordis ( cordis.victor at gmail.com)
7878
* @since 1.0
79-
* @version 1.4.4
79+
* @version 1.4.5
8080
*/
8181
public class XMLReader implements Closeable {
8282

@@ -1089,12 +1089,20 @@ private boolean isRootEnd() {
10891089
* @param parser to use, null if default
10901090
*/
10911091
public void reset(Reader reader, XmlPullParser parser) {
1092-
this.driver = parser != null ? new XMLReaderTextDriver(this, reader, parser)
1093-
: new XMLReaderTextDriver(this, reader);
1092+
if (isReusableXMLReaderTextDriver()) {
1093+
((XMLReaderTextDriver) this.driver).reset(reader, parser);
1094+
} else {
1095+
this.driver = parser != null ? new XMLReaderTextDriver(this, reader, parser)
1096+
: new XMLReaderTextDriver(this, reader);
1097+
}
10941098
this.decoded.clear();
10951099
this.beforeRoot = true;
10961100
}
10971101

1102+
private boolean isReusableXMLReaderTextDriver() {
1103+
return this.driver != null && this.driver.getClass() == XMLReaderTextDriver.class;
1104+
}
1105+
10981106
/**
10991107
* Resets this instance, setting it to the new <code>in</code> stream.
11001108
*

easyml/src/net/sourceforge/easyml/XMLReaderTextDriver.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @author Victor Cordis ( cordis.victor at gmail.com)
3535
* @since 1.1.0
36-
* @version 1.3.3
36+
* @version 1.4.5
3737
*/
3838
/* default */ final class XMLReaderTextDriver extends XMLReader.Driver {
3939

@@ -59,8 +59,12 @@ public XMLReaderTextDriver(XMLReader target, Reader in) {
5959
*/
6060
public XMLReaderTextDriver(XMLReader target, Reader in, XmlPullParser parser) {
6161
super(target);
62+
this.parser = parser;
63+
this.init(in);
64+
}
65+
66+
private void init(Reader in) {
6267
try {
63-
this.parser = parser;
6468
this.parser.setInput(in);
6569
this.readerToClose = in;
6670
} catch (XmlPullParserException xppX) {
@@ -204,6 +208,23 @@ protected void consumeFully() {
204208
}
205209
}
206210

211+
/**
212+
* Resets this current instance to read using the given reader and,
213+
* optionally, to parse using the given custom pull-parser. This method is
214+
* needed for performance reasons since the pull-parsers can be expensive to
215+
* recreate and should be reused.
216+
*
217+
* @param in the required input reader
218+
* @param parser optional custom parser to replace the default pull-parse
219+
* implementation
220+
*/
221+
public void reset(Reader in, XmlPullParser parser) {
222+
if (parser != null) {
223+
this.parser = parser;
224+
}
225+
this.init(in);
226+
}
227+
207228
/**
208229
* Closes the underlying reader.
209230
*/

easyml/src/net/sourceforge/easyml/XMLWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,6 @@ public final void flush() {
10211021
if (!this.isInitialState()) {
10221022
this.driver.endElement(); // DTD.ELEMENT_EASYML.
10231023
this.driver.flush();
1024-
this.driver.state = XMLWriter.Driver.STATE_INITIAL;
10251024
this.encoded.clear();
10261025
}
10271026
}

easyml/src/net/sourceforge/easyml/util/ReflectionUtil.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Victor Cordis ( cordis.victor at gmail.com)
3131
* @since 1.0
32-
* @version 1.3.8
32+
* @version 1.4.5
3333
*/
3434
public final class ReflectionUtil {
3535

@@ -223,7 +223,7 @@ public static <T> T instantiateInner(Class<T> c, Object outer)
223223
* @return new instance of c
224224
*/
225225
public static <T> T instantiateUnsafely(Class<T> c) {
226-
return Unsafe.instantiator.instantiate(c);
226+
return UnsafeInstantiator.instantiator.instantiate(c);
227227
}
228228

229229
/**
@@ -242,20 +242,13 @@ public static Class classForName(String typeName) throws ClassNotFoundException
242242
}
243243

244244
/**
245-
* UnsafeInstantiator interface defines the API for instantiating classes
246-
* with no default (zero-arg) constructors.
247-
*/
248-
private interface UnsafeInstantiator {
249-
250-
<T> T instantiate(Class<T> c);
251-
}
252-
253-
/**
254-
* Unsafe class lazy inits the JVM dependent unsafe instantiator.
245+
* UnsafeInstantiator lazy inits the JVM-dependent unsafe instantiator and
246+
* provides the API for instantiating classes with no default (zero-arg)
247+
* constructors.
255248
*/
256-
private static final class Unsafe {
249+
private static abstract class UnsafeInstantiator {
257250

258-
private static final UnsafeInstantiator instantiator = Unsafe.create();
251+
private static final UnsafeInstantiator instantiator = create();
259252

260253
private static UnsafeInstantiator create() {
261254
// if available, use sun.misc.Unsafe:
@@ -323,8 +316,10 @@ public <T> T instantiate(Class<T> c) {
323316
};
324317
}
325318

326-
private Unsafe() {
319+
private UnsafeInstantiator() {
327320
}
321+
322+
public abstract <T> T instantiate(Class<T> c);
328323
}
329324

330325
/**

easyml/test/net/sourceforge/easyml/FacadeMain.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class FacadeMain {
2727
// public static void main(String[] args) {
2828
// final Person in = new Person(1, "da");
2929
//
30-
// final EasyML easyml = new EasyML();
31-
// easyml.setStyle(EasyML.Style.PRETTY);
32-
//// easyml.alias(Person.class, "Persoana");
30+
// final EasyML easyml = new EasyMLBuilder()
31+
// .withStyle(EasyML.Style.PRETTY)
32+
// .build();
3333
// long time = System.currentTimeMillis();
3434
// for (int i = 0; i < 100000; i++) {
3535
// easyml.deserialize(easyml.serialize(in));

0 commit comments

Comments
 (0)