Can access by local method variables be faster then access by member variables ?

Ulf Zibis Ulf.Zibis at gmx.de
Fri Aug 15 15:45:31 PDT 2008


I don't think, that allocation of the object makes the difference, but 
the repeated access to the local/member variables in the for-loop which 
were accessed for each char in the possibly long to be encoded char 
buffer/array.

-Ulf


Am 16.08.2008 00:26, Xiaobin Lu schrieb:
> I suspect that adding two members to the object would increase the 
> object size which might affect the efficiency of object allocation. 
> Have you had a chance to look at the generated code, it might be 
> possible that the method itself is inlined so the pop/push operations 
> have been completely eliminated?
>
> -Xiaobin
>
> Ulf Zibis wrote:
>> Hi experts,
>>
>> to avoid stack push + pop of src, dst I have refactored my code in 
>> moving the local method variables (sp, ...) to member variables 
>> (srcPos, ...).
>>
>> After this I have experienced that my code was little slower.
>>
>> For full source code see:
>> https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/trunk/src/sun/nio/cs/SingleByteEncoder.java?rev=291&view=markup 
>>
>> https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/trunk/src/sun/nio/cs/SingleByteEncoder.java?rev=302&view=markup 
>>
>>
>> ======================================== revisions 291 
>> ========================================
>>
>>    private CoderResult encodeArraysLoop(CharBuffer src, ByteBuffer 
>> dst) {
>>        char[] sa = src.array();
>>        int sp = src.arrayOffset() + src.position();
>>        int sl = src.arrayOffset() + src.limit();
>>        assert (sp <= sl);
>>        byte[] da = dst.array();
>>        int dp = dst.arrayOffset() + dst.position();
>>        int dl = dst.arrayOffset() + dst.limit();
>>        assert (dp <= dl);
>>        try {
>>            for (; sp < sl; sp++) {
>>                current = sa[sp];
>>                byte b = encodeSingle();
>>                if (dp >= dl)
>>                    return CoderResult.OVERFLOW;
>>                da[dp++] = b;
>>            }
>>            return CoderResult.UNDERFLOW;
>>        } catch (UnmappableCharacterException e) {
>>            if (Surrogate.isLow(current) || current >= '\uFFFE')
>>                return CODERRESULT_MALFORMED;
>>            if (Surrogate.isHigh(current))
>>                try {
>>                    if (sp+1 >= sl)
>>                        return CoderResult.UNDERFLOW;
>>                    encodeSurrogate(sa[sp+1]);
>>                } catch (UnmappableCharacterException e2) {
>>                    return CODERRESULT_UNMAPPABLE2;
>>                } catch (MalformedInputException e2) {
>>                    return CODERRESULT_MALFORMED2;
>>                }
>>            return CODERRESULT_UNMAPPABLE;
>>        } finally {
>>            src.position(sp - src.arrayOffset());
>>            dst.position(dp - dst.arrayOffset());
>>        }
>>    }
>>
>> ======================================== revisions 302 
>> ========================================
>>
>>    private CoderResult encodeArraysLoop() throws RuntimeException, 
>> UnmappableCharacterException {
>>        for (; srcPos < srcLimit; srcPos++) {
>>            current = srcArray[srcPos];
>>            byte b = encodeSingle();
>>            if (dstPos < dstLimit)
>>                dstArray[dstPos++] = b;
>>            else
>>                return CoderResult.OVERFLOW;
>>        }
>>        return CoderResult.UNDERFLOW;
>>    }
>>
>> =============================================================================================== 
>>
>>
>>
>> Thanks for your explanations,
>>
>> Ulf
>>
>>
>>
>>
>
>




More information about the hotspot-dev mailing list