Re: ClassReader.readUtf and compact strings
Eirik Bjørsnøs (via asm Mailing List) <[email protected]> Sat, 6 Feb 2021 11:42:57 +0100
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <CA+pBWhsD+b50J02-DKxj5WNWUxqOR4=K=HcFnGrCL+yQk=wCDA@mail.gmail.com> |
This is a multi-part message in MIME format...
------------=_1612608190-15976-6
Content-Type: multipart/alternative; boundary="0000000000002ecb1905baa89aa3"
--0000000000002ecb1905baa89aa3
Content-Type: text/plain; charset="UTF-8"
>
> I imagine there could be a performance win of not doing this byte[] ->
> char[] -> byte[] dance, given that most class strings are ASCII anyway.
>
I did a quick implementation of this and could not observe any performance
difference. Probably the JIT optimizes much of this away, or the cost of
array copying is dominating.
The change did remove some crufty code though, which might be a win, if
only in terms of readability / maintainability.
Cheers,
Eirik.
class FastClassReader extends ClassReader {
public FastClassReader(byte[] classfileBuffer) {
super(classfileBuffer);
}
@Override
public final String readUTF8(int offset, char[] charBuffer) {
int constantPoolEntryIndex = readUnsignedShort(offset);
if (offset == 0 || constantPoolEntryIndex == 0) {
return null;
}
return fastReadUtf(constantPoolEntryIndex);
}
private String fastReadUtf(final int constantPoolEntryIndex) {
String value = constantUtf8Values[constantPoolEntryIndex];
if (value != null) {
return value;
}
return fastReadString(constantPoolEntryIndex);
}
private String fastReadString(int constantPoolEntryIndex) {
int cpInfoOffset = getItem(constantPoolEntryIndex);
return constantUtf8Values[constantPoolEntryIndex] =
new String(classFileBuffer, cpInfoOffset + 2,
readUnsignedShort(cpInfoOffset), StandardCharsets.UTF_8);
}
}
--0000000000002ecb1905baa89aa3
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_quote"><blockquote class=3D"gmail_quot=
e" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)=
;padding-left:1ex"><div dir=3D"ltr"><div>I imagine there could be a perform=
ance win of not doing this byte[] -> char[] -> byte[] dance, given th=
at most class strings=C2=A0are ASCII anyway.</div></div></blockquote><div><=
br></div><div>I did a quick implementation of this and could not observe an=
y performance difference. Probably the JIT optimizes much of this=C2=A0away=
, or the cost of array copying is dominating.</div><div><br></div><div>The =
change did remove some crufty code though, which might be a win, if only in=
terms of readability / maintainability.</div><div><br></div><div><div>Chee=
rs,</div><div>Eirik.</div></div><div><br></div><div><br></div><div>class Fa=
stClassReader extends ClassReader {<br><br>=C2=A0 =C2=A0 public FastClassRe=
ader(byte[] classfileBuffer) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 super(classfi=
leBuffer);<br>=C2=A0 =C2=A0 }<br><br>=C2=A0 =C2=A0 @Override<br>=C2=A0 =C2=
=A0 public final String readUTF8(int offset, char[] charBuffer) {<br>=C2=A0=
=C2=A0 =C2=A0 =C2=A0 int constantPoolEntryIndex =3D readUnsignedShort(offs=
et);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (offset =3D=3D 0 || constantPoolEntr=
yIndex =3D=3D 0) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return null=
;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return fa=
stReadUtf(constantPoolEntryIndex);<br>=C2=A0 =C2=A0 }<br><br>=C2=A0 =C2=A0 =
private String fastReadUtf(final int constantPoolEntryIndex) {<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 String value =3D constantUtf8Values[constantPoolEntryI=
ndex];<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (value !=3D null) {<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return value;<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 }<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return fastReadString(constantPoolEntr=
yIndex);<br>=C2=A0 =C2=A0 }<br><br>=C2=A0 =C2=A0 private String fastReadStr=
ing(int constantPoolEntryIndex) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 int cpInfo=
Offset =3D getItem(constantPoolEntryIndex);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
return constantUtf8Values[constantPoolEntryIndex] =3D<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 new String(classFileBuffer, cpInfoOf=
fset + 2, readUnsignedShort(cpInfoOffset), StandardCharsets.UTF_8);<br>=C2=
=A0 =C2=A0 }<br><br>}<br></div><div><br></div></div></div>
--0000000000002ecb1905baa89aa3--
------------=_1612608190-15976-6
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
--
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws
------------=_1612608190-15976-6--