[SPIGOT-1388] NibbleArray - unpredictable branch Created: 01/Jan/16  Updated: 11/Dec/17  Resolved: 30/Jul/16

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor
Reporter: Michael Zangl Assignee: md_5
Resolution: Fixed Votes: 0
Labels: chunks


 Description   

This removes the unpredictable branch in the NibbleArray. Since those two functions are used pretty often this branch costs pretty much.

It also reduces method code size to ease inlining.

    public int a(int i) {
        int j = this.c(i);

        // Changes start
        int shift = shift(i);
        int aval = this.a[j];
        return (aval >> shift) & 15;
        // Changes end
    }

    public void a(int i, int j) {
        int k = this.c(i);
        
        // Changes start
        int shift = shift(i);
        int aval = this.a[k];
        aval &= ~(15<<shift);
        aval |= (j & 15) << shift;
        this.a[k] = (byte) aval; // Note: Using long[] would increase performance even more.
        // Changes end
    }

    // Changes start
    private int shift(int i) {
    	return (i & 1) >> 2;
    }
    // Changes end


 Comments   
Comment by md_5 [ 30/Jul/16 ]

You were a bit off base with your shifting, but I've implemented a corrected + unit tested version.

Comment by md_5 [ 03/Jan/16 ]

Please fill in the CLA: http://spigotmc.org/go/cla

Comment by Daniel Ennis [ 01/Jan/16 ]

need to put in diff format... cant see what the changes are from>to

Generated at Sat Dec 13 17:11:05 UTC 2025 using Jira 10.3.13#10030013-sha1:56dd970ae30ebfeda3a697d25be1f6388b68a422.