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

Change immutable wpimath types to use records #7828

Draft
wants to merge 1 commit into
base: 2027
Choose a base branch
from

Conversation

spacey-sooty
Copy link
Contributor

No description provided.

@github-actions github-actions bot added component: wpimath Math library 2027 2027 target labels Feb 26, 2025
Signed-off-by: Jade Turner <spacey-sooty@proton.me>
Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

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

I know this is still a draft, but here's some thoughts (that you were maybe already planning to do):

  • Accessors should be changed across the board to use name() instead of getName(), including classes that aren't using records. Mixing both is just asking for confusion.
  • The implementations should consistently use either the component field directly or the accessor method, not both.

pose.getTranslation().getX(),
pose.getTranslation().getY(),
pose.getRotation().getRadians());
pose.getTranslation().y(), pose.getTranslation().x(), pose.getRotation().getRadians());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pose.getTranslation().y(), pose.getTranslation().x(), pose.getRotation().getRadians());
pose.getTranslation().x(), pose.getTranslation().y(), pose.getRotation().getRadians());

To be fair this is deprecated for removal, but still.

Comment on lines -117 to -120
@JsonProperty
public double getX() {
return m_x;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll note that the old accessor is annotated with @JsonProperty while the new accessor is annotated with @JsonProperty(required = true, value = "x") because that's what the record component is annotated with in the class definition. Not sure if that'll be an issue, but worth noting.

}

/** Translation2d protobuf for serialization. */
public static final Translation2dProto proto = new Translation2dProto();

/** Translation2d struct for serialization. */
public static final Translation2dStruct struct = new Translation2dStruct();
public static final Struct<Translation2d> struct = StructGenerator.genRecord(Translation2d.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still a little bit iffy about using the struct generator since the packing and unpacking process uses reflection (which has overhead) and struct is intended to be a simple, low-overhead logging solution. Efficiency is extra important for these geometry classes, which many other things use and can easily be published many times (think of how many poses are in a trajectory). Of course, we'll have to defer to runtime testing to see if the overhead is actually significant- It might very well not be.

However, there's also the fact that the struct implementations are already written and some of them can't be removed since not everything will be turned into records (i.e. classes with preconditions on their member variables), so I don't see much reason to change the status quo for something that's slower.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like this lowers the maintainence burden for these classes which is why I made the change.

As for performance it's a one time cost at startup so I don't think it's an issue really

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this lowers the maintainence burden for these classes which is why I made the change.

Fair. The lack of parity with the other geometry types and with C++ would be annoying, but probably manageable.

it's a one time cost at startup

The bulk of it is a one time cost at startup, but the packing and unpacking processes also use reflection:

Object componentValue = components[i].getAccessor().invoke(value);
return recordClass.getConstructor(argTypes).newInstance(args);

Copy link
Member

Choose a reason for hiding this comment

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

I don't really buy the maintenance burden argument here--these geometry classes are stable and will never accumulate additional members.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2027 2027 target component: wpimath Math library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants