Optimization of java.io.DataInputStream

Vitaly Davidovich vitalyd at gmail.com
Tue Nov 12 15:15:13 UTC 2013


So the locals can be called hi and lo, and it's very readable.  Adding nop
shifts would only be ok, IMHO, if it had zero cost all around (other than
source being slightly larger).

Sent from my phone
On Nov 12, 2013 10:12 AM, "Alex Yursha" <alexyursha at gmail.com> wrote:

> Well, I agree that the code is more readable when we use shift
> consistently, if you think it is worth some redundant bytes in a class file.
>
>
> On Tue, Nov 12, 2013 at 6:00 PM, Vitaly Davidovich <vitalyd at gmail.com>wrote:
>
>> Roger,
>>
>> I think a simple comment is better than leaving head scratching code
>> behind, especially since it'll impact interpreter and byte code size.
>>
>> Sent from my phone
>> On Nov 12, 2013 9:58 AM, "roger riggs" <roger.riggs at oracle.com> wrote:
>>
>> > Hi,
>> >
>> > At the source level the style makes it clear where the data is to be
>> > aligned.
>> > There many other places that use that style "<< 0" to indicate alignment
>> > of the data
>> > when assembling a whole from parts.
>> > It is assumed the compiler/runtime will omit unnecessary operations; the
>> > compiler does not.
>> >
>> > $.02, Roger
>> >
>> > On 11/12/2013 9:45 AM, Vitaly Davidovich 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