Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use static annotation to optimize Bitboard Magic and Attacks #623

Conversation

lenguyenthanh
Copy link
Member

Using @static annotation to compile all private val field as static field. The benchmark is staying the same or increasing so little probably because jit inlines those functions anyway. But lets do this to avoid any doubt.

here is the decompiled code before and after the change:
before:

public final class Attacks {
    public static long[] ATTACKS() {
        return Attacks$.MODULE$.ATTACKS();
    }

    public static long[][] BETWEEN() {
        return Attacks$.MODULE$.BETWEEN();
    }

    public static long[] BLACK_PAWN_ATTACKS() {
        return Attacks$.MODULE$.BLACK_PAWN_ATTACKS();
    }

    public static long[] FILES() {
        return Attacks$.MODULE$.FILES();
    }

    public static long[] KING_ATTACKS() {
        return Attacks$.MODULE$.KING_ATTACKS();
    }

    public static long[] KNIGHT_ATTACKS() {
        return Attacks$.MODULE$.KNIGHT_ATTACKS();
    }

    public static long[] RANKS() {
        return Attacks$.MODULE$.RANKS();
    }

    public static long[][] RAYS() {
        return Attacks$.MODULE$.RAYS();
    }

    public static long[] WHITE_PAWN_ATTACKS() {
        return Attacks$.MODULE$.WHITE_PAWN_ATTACKS();
    }
}

after

public final class Attacks {
    public static final long chess$bitboard$Attacks$$$all = -1L;
    public static final long[] RANKS = (long[])Array$.MODULE$.fill(8, Attacks$::chess$bitboard$Attacks$$_$$anonfun$1, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final long[] FILES = (long[])Array$.MODULE$.fill(8, Attacks$::chess$bitboard$Attacks$$_$$anonfun$2, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final long[][] BETWEEN = new long[64][64];
    public static final long[][] RAYS = new long[64][64];
    public static final long[] ATTACKS = (long[])Array$.MODULE$.fill(88772, Attacks$::chess$bitboard$Attacks$$_$$anonfun$3, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final long[] KNIGHT_ATTACKS = (long[])Array$.MODULE$.fill(64, Attacks$::chess$bitboard$Attacks$$_$$anonfun$4, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final long[] KING_ATTACKS = (long[])Array$.MODULE$.fill(64, Attacks$::chess$bitboard$Attacks$$_$$anonfun$5, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final long[] WHITE_PAWN_ATTACKS = (long[])Array$.MODULE$.fill(64, Attacks$::chess$bitboard$Attacks$$_$$anonfun$6, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final long[] BLACK_PAWN_ATTACKS = (long[])Array$.MODULE$.fill(64, Attacks$::chess$bitboard$Attacks$$_$$anonfun$7, ClassTag$.MODULE$.apply(Long.TYPE));
    public static final int[] chess$bitboard$Attacks$$$KNIGHT_DELTAS = new int[]{17, 15, 10, 6, -17, -15, -10, -6};
    public static final int[] chess$bitboard$Attacks$$$BISHOP_DELTAS = new int[]{7, -7, 9, -9};
    public static final int[] chess$bitboard$Attacks$$$ROOK_DELTAS = new int[]{1, -1, 8, -8};
    public static final int[] chess$bitboard$Attacks$$$KING_DELTAS = new int[]{1, 7, 8, 9, -1, -7, -8, -9};
    public static final int[] chess$bitboard$Attacks$$$WHITE_PAWN_DELTAS = new int[]{7, 9};
    public static final int[] chess$bitboard$Attacks$$$BLACK_PAWN_DELTAS = new int[]{-7, -9};
}

@lenguyenthanh lenguyenthanh merged commit ffecc87 into lichess-org:master Feb 17, 2025
3 checks passed
@lenguyenthanh lenguyenthanh deleted the optimize-attacks-initialize-with-inlines branch February 17, 2025 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant