Skip to content

Commit caf8ed2

Browse files
unmarshaller performance: use bulk operations where possible
1 parent 78a4691 commit caf8ed2

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/unmarshaller/ValidatingUnmarshaller.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.glassfish.jaxb.runtime.v2.runtime.unmarshaller;
1212

13+
import org.glassfish.jaxb.runtime.v2.runtime.output.Pcdata;
1314
import org.glassfish.jaxb.runtime.v2.util.FatalAdapter;
1415
import org.glassfish.jaxb.core.v2.runtime.unmarshaller.LocatorEx;
1516
import org.xml.sax.SAXException;
@@ -99,8 +100,25 @@ public void text( CharSequence pcdata ) throws SAXException {
99100
if(buf.length<len) {
100101
buf = new char[len];
101102
}
102-
for( int i=0;i<len; i++ )
103-
buf[i] = pcdata.charAt(i); // isn't this kinda slow?
103+
104+
// performance optimization: use getChars()/writeTo() if possible
105+
// note: this will become just pcdata.getChars() once minimum supported JDK version reaches 25 (cf. JDK-8343110)
106+
if (pcdata instanceof String) {
107+
((String) pcdata).getChars(0, len, buf, 0);
108+
}
109+
else if (pcdata instanceof StringBuilder) {
110+
((StringBuilder) pcdata).getChars(0, len, buf, 0);
111+
}
112+
else if (pcdata instanceof StringBuffer) {
113+
((StringBuffer) pcdata).getChars(0, len, buf, 0);
114+
}
115+
else if (pcdata instanceof Pcdata) {
116+
((Pcdata) pcdata).writeTo(buf, 0);
117+
}
118+
else {
119+
for (int i = 0; i < len; i++)
120+
buf[i] = pcdata.charAt(i); // this is slow due to O(n) range checks
121+
}
104122

105123
validator.characters(buf,0,len);
106124
if(predictor.expectText())

0 commit comments

Comments
 (0)