Optimization of java.io.DataInputStream
Alex Yursha
alexyursha at gmail.com
Tue Nov 12 14:51:38 UTC 2013
I have no idea whether JIT compiler will eliminate these nop shifts, but as
I said each such shift brings two redundant bytecode instructions for the
virtual machine. It think this tiny optimization can benefit both
interpreter and class file size.
On Tue, Nov 12, 2013 at 5:45 PM, Vitaly Davidovich <vitalyd at gmail.com>wrote:
> Interesting - was this file codegen'd or something? Why are these nop
> shifts there?
>
> Anyway, I'd expect JIT does the right thing here and doesn't actually
> issue this instruction. Are you optimizing for interpreter or meta data
> size then?
>
> Sent from my phone
> On Nov 12, 2013 3:45 AM, "Alex Yursha" <alexyursha at gmail.com> wrote:
>
>> The following methods in java.io.DataInputStream perform right shift to
>> zero positions in their implementations:
>> - short readShort()
>> - int readUnsignedShort()
>> - char readChar()
>> - int readInt()
>> - long readLong()
>> - String readUTF(DataInput in)
>>
>> For example:
>>
>> public final short readShort() throws IOException {
>> int ch1 = in.read();
>> int ch2 = in.read();
>> if ((ch1 | ch2) < 0)
>> throw new EOFException();
>> return (short)((ch1 << 8) + *(ch2 << 0)*);
>>
>> }
>>
>> It can be optimizied as follows:
>>
>> public final short readShort() throws IOException {
>> int ch1 = in.read();
>> int ch2 = in.read();
>> if ((ch1 | ch2) < 0)
>> throw new EOFException();
>> return (short)((ch1 << 8) + *ch2*);
>>
>> }
>>
>>
>> This optimization saves 2 bytecode instructions in the class file code
>> arrays of each method mentioned above.
>>
>
More information about the core-libs-dev
mailing list