Skip to content

Comments

NearestBranch Memory fix#210

Closed
Roxie222 wants to merge 1 commit intomainfrom
annika/nearestbranchfix
Closed

NearestBranch Memory fix#210
Roxie222 wants to merge 1 commit intomainfrom
annika/nearestbranchfix

Conversation

@Roxie222
Copy link
Contributor

Moved Branch Lists out of code into statics to reduce memory allocations and improve performance

@gemini-code-assist
Copy link

Summary of Changes

Hello @Roxie222, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on optimizing memory usage and improving performance within the AutoAlign subsystem. It achieves this by refactoring the creation and management of various Pose2d lists, which define different alignment targets. By making these lists static and final, they are instantiated only once, preventing redundant allocations. Additionally, the logic for finding the nearest branch or reef face has been streamlined into a single, more flexible method, simplifying the codebase.

Highlights

  • Memory Optimization: Moved various List<Pose2d> objects, representing different branch and reef face configurations, from being dynamically created within methods to private static final class members. This ensures these lists are initialized only once.
  • Performance Improvement: By reducing repeated memory allocations for these lists, the change aims to improve the overall performance of the AutoAlign subsystem, especially for frequently called methods.
  • Code Refactoring: Consolidated multiple specific getNearestXBranch and getNearestXL methods into a single, generalized getNearestBranch method. This new method accepts the appropriate static lists as arguments, enhancing code reusability and maintainability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great optimization that moves list instantiations into static final fields to improve performance and reduce memory allocations. The refactoring of getNearestBranch also simplifies the code. My feedback focuses on improving code style and maintainability by adhering to standard Java naming conventions for constants and method parameters, which will make the code more readable and consistent.

Comment on lines +185 to +261
private static final List<Pose2d> BlueBranches =
List.of(
blueBranchA,
blueBranchB,
blueBranchC,
blueBranchD,
blueBranchE,
blueBranchF,
blueBranchG,
blueBranchH,
blueBranchI,
blueBranchJ,
blueBranchK,
blueBranchL);

private static final List<Pose2d> RedBranches =
List.of(
redBranchA,
redBranchB,
redBranchC,
redBranchD,
redBranchE,
redBranchF,
redBranchG,
redBranchH,
redBranchI,
redBranchJ,
redBranchK,
redBranchL);

private static final List<Pose2d> LeftBlueReefFaces =
List.of(
lBlueReefFaceAB,
lBlueReefFaceCD,
lBlueReefFaceEF,
lBlueReefFaceGH,
lBlueReefFaceIJ,
lBlueReefFaceKL);

private static final List<Pose2d> LeftRedReefFaces =
List.of(
lRedReefFaceAB,
lRedReefFaceCD,
lRedReefFaceEF,
lRedReefFaceGH,
lRedReefFaceIJ,
lRedReefFaceKL);

private static final List<Pose2d> RightBlueReefFaces =
List.of(
rBlueReefFaceAB,
rBlueReefFaceCD,
rBlueReefFaceEF,
rBlueReefFaceGH,
rBlueReefFaceIJ,
rBlueReefFaceKL);

private static final List<Pose2d> RightRedReefFaces =
List.of(
rRedReefFaceAB,
rRedReefFaceCD,
rRedReefFaceEF,
rRedReefFaceGH,
rRedReefFaceIJ,
rRedReefFaceKL);

private static final List<Pose2d> LeftBlueBranch =
List.of(blueBranchA, blueBranchC, blueBranchE, blueBranchG, blueBranchI, blueBranchK);

private static final List<Pose2d> LeftRedBranch =
List.of(redBranchA, redBranchC, redBranchE, redBranchG, redBranchI, redBranchK);

private static final List<Pose2d> RightBlueBranch =
List.of(blueBranchB, blueBranchD, blueBranchF, blueBranchH, blueBranchJ, blueBranchL);

private static final List<Pose2d> RightRedBranch =
List.of(redBranchB, redBranchD, redBranchF, redBranchH, redBranchJ, redBranchL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to standard Java naming conventions, static final fields (constants) should be named in UPPER_SNAKE_CASE. All the newly added lists are constants and should be renamed. For example, BlueBranches should be BLUE_BRANCHES. I also suggest using plural names for lists of branches, like LEFT_BLUE_BRANCHES for LeftBlueBranch, to improve clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do recommend using plural names for lists of branches, and would at least use camelCase like the other constants above.

Comment on lines +337 to +341
case LEFTB -> getNearestBranch(pose, LeftBlueBranch, LeftRedBranch);
case RIGHTB -> getNearestBranch(pose, RightBlueBranch, RightRedBranch);
case L1LB -> getNearestBranch(pose, LeftBlueReefFaces, LeftRedReefFaces);
case L1RB -> getNearestBranch(pose, RightBlueReefFaces, RightRedReefFaces);
case ALLB -> getNearestBranch(pose, BlueBranches, RedBranches);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To be consistent with the recommended UPPER_SNAKE_CASE for constants, the usages here should be updated. I've suggested new names that use plural for branch lists (e.g., LEFT_BLUE_BRANCHES) to improve clarity.

Suggested change
case LEFTB -> getNearestBranch(pose, LeftBlueBranch, LeftRedBranch);
case RIGHTB -> getNearestBranch(pose, RightBlueBranch, RightRedBranch);
case L1LB -> getNearestBranch(pose, LeftBlueReefFaces, LeftRedReefFaces);
case L1RB -> getNearestBranch(pose, RightBlueReefFaces, RightRedReefFaces);
case ALLB -> getNearestBranch(pose, BlueBranches, RedBranches);
case LEFTB -> getNearestBranch(pose, LEFT_BLUE_BRANCHES, LEFT_RED_BRANCHES);
case RIGHTB -> getNearestBranch(pose, RIGHT_BLUE_BRANCHES, RIGHT_RED_BRANCHES);
case L1LB -> getNearestBranch(pose, LEFT_BLUE_REEF_FACES, LEFT_RED_REEF_FACES);
case L1RB -> getNearestBranch(pose, RIGHT_BLUE_REEF_FACES, RIGHT_RED_REEF_FACES);
case ALLB -> getNearestBranch(pose, BLUE_BRANCHES, RED_BRANCHES);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated to match whatever new names are chosen for the constants above.

Comment on lines +345 to 348
private static Pose2d getNearestBranch(Pose2d p, List<Pose2d> Blue, List<Pose2d> Red) {
List<Pose2d> branchPose2ds = AllianceUtils.isBlue() ? Blue : Red;
return p.nearest(branchPose2ds);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Parameter names should follow Java's lower camel case convention. Blue and Red should be renamed to something like bluePoses and redPoses respectively to improve readability and adhere to conventions.

Suggested change
private static Pose2d getNearestBranch(Pose2d p, List<Pose2d> Blue, List<Pose2d> Red) {
List<Pose2d> branchPose2ds = AllianceUtils.isBlue() ? Blue : Red;
return p.nearest(branchPose2ds);
}
private static Pose2d getNearestBranch(Pose2d p, List<Pose2d> bluePoses, List<Pose2d> redPoses) {
List<Pose2d> branchPose2ds = AllianceUtils.isBlue() ? bluePoses : redPoses;
return p.nearest(branchPose2ds);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Gemini here.

Comment on lines +185 to +261
private static final List<Pose2d> BlueBranches =
List.of(
blueBranchA,
blueBranchB,
blueBranchC,
blueBranchD,
blueBranchE,
blueBranchF,
blueBranchG,
blueBranchH,
blueBranchI,
blueBranchJ,
blueBranchK,
blueBranchL);

private static final List<Pose2d> RedBranches =
List.of(
redBranchA,
redBranchB,
redBranchC,
redBranchD,
redBranchE,
redBranchF,
redBranchG,
redBranchH,
redBranchI,
redBranchJ,
redBranchK,
redBranchL);

private static final List<Pose2d> LeftBlueReefFaces =
List.of(
lBlueReefFaceAB,
lBlueReefFaceCD,
lBlueReefFaceEF,
lBlueReefFaceGH,
lBlueReefFaceIJ,
lBlueReefFaceKL);

private static final List<Pose2d> LeftRedReefFaces =
List.of(
lRedReefFaceAB,
lRedReefFaceCD,
lRedReefFaceEF,
lRedReefFaceGH,
lRedReefFaceIJ,
lRedReefFaceKL);

private static final List<Pose2d> RightBlueReefFaces =
List.of(
rBlueReefFaceAB,
rBlueReefFaceCD,
rBlueReefFaceEF,
rBlueReefFaceGH,
rBlueReefFaceIJ,
rBlueReefFaceKL);

private static final List<Pose2d> RightRedReefFaces =
List.of(
rRedReefFaceAB,
rRedReefFaceCD,
rRedReefFaceEF,
rRedReefFaceGH,
rRedReefFaceIJ,
rRedReefFaceKL);

private static final List<Pose2d> LeftBlueBranch =
List.of(blueBranchA, blueBranchC, blueBranchE, blueBranchG, blueBranchI, blueBranchK);

private static final List<Pose2d> LeftRedBranch =
List.of(redBranchA, redBranchC, redBranchE, redBranchG, redBranchI, redBranchK);

private static final List<Pose2d> RightBlueBranch =
List.of(blueBranchB, blueBranchD, blueBranchF, blueBranchH, blueBranchJ, blueBranchL);

private static final List<Pose2d> RightRedBranch =
List.of(redBranchB, redBranchD, redBranchF, redBranchH, redBranchJ, redBranchL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do recommend using plural names for lists of branches, and would at least use camelCase like the other constants above.

Comment on lines +345 to 348
private static Pose2d getNearestBranch(Pose2d p, List<Pose2d> Blue, List<Pose2d> Red) {
List<Pose2d> branchPose2ds = AllianceUtils.isBlue() ? Blue : Red;
return p.nearest(branchPose2ds);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Gemini here.

Comment on lines +337 to +341
case LEFTB -> getNearestBranch(pose, LeftBlueBranch, LeftRedBranch);
case RIGHTB -> getNearestBranch(pose, RightBlueBranch, RightRedBranch);
case L1LB -> getNearestBranch(pose, LeftBlueReefFaces, LeftRedReefFaces);
case L1RB -> getNearestBranch(pose, RightBlueReefFaces, RightRedReefFaces);
case ALLB -> getNearestBranch(pose, BlueBranches, RedBranches);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated to match whatever new names are chosen for the constants above.

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.

3 participants