Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

import org.glassfish.jaxb.runtime.v2.runtime.output.Pcdata;
import org.glassfish.jaxb.runtime.v2.util.FatalAdapter;
import org.glassfish.jaxb.core.v2.runtime.unmarshaller.LocatorEx;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -99,8 +100,25 @@ public void text( CharSequence pcdata ) throws SAXException {
if(buf.length<len) {
buf = new char[len];
}
for( int i=0;i<len; i++ )
buf[i] = pcdata.charAt(i); // isn't this kinda slow?

// performance optimization: use getChars()/writeTo() if possible
// note: this will become just pcdata.getChars() once minimum supported JDK version reaches 25 (cf. JDK-8343110)
if (pcdata instanceof String) {
((String) pcdata).getChars(0, len, buf, 0);
}
else if (pcdata instanceof StringBuilder) {
((StringBuilder) pcdata).getChars(0, len, buf, 0);
}
else if (pcdata instanceof StringBuffer) {
((StringBuffer) pcdata).getChars(0, len, buf, 0);
}
Comment on lines +109 to +114
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use instanceof AbstractStringBuilder since it's extented by both StringBuilder and StringBuffer ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would like to but that class is not public...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oups, yes you're right :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CharSequence is what you need, implemented by String, StringBuilder, StringBuffer...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but

  • it's already method argument and type of pcdata
  • it doesn't bring optimized method getChars

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry. getChars is there since JDK 25 only. I did miss that.

else if (pcdata instanceof Pcdata) {
((Pcdata) pcdata).writeTo(buf, 0);
Copy link
Author

@winfriedgerlach winfriedgerlach Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the implementation, this should give massive gains for Base64Data, but IntArrayData and IntData also benefit

}
else {
for (int i = 0; i < len; i++)
buf[i] = pcdata.charAt(i); // this is slow due to O(n) range checks
}

validator.characters(buf,0,len);
if(predictor.expectText())
Expand Down