Skip to content

Optimize Vector Object Usage#2312

Open
Axionize wants to merge 23 commits into
GrimAnticheat:2.0from
Axionize:optimize/vector-objects
Open

Optimize Vector Object Usage#2312
Axionize wants to merge 23 commits into
GrimAnticheat:2.0from
Axionize:optimize/vector-objects

Conversation

@Axionize
Copy link
Copy Markdown
Contributor

Replace swathes of unnecessary object allocation with faster primitive logic and other minor optimizations

@Axionize
Copy link
Copy Markdown
Contributor Author

@ManInMyVan can I get a review?

Comment thread common/src/main/java/ac/grim/grimac/checks/impl/breaking/WrongBreak.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/checks/impl/breaking/WrongBreak.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/checks/impl/breaking/FastBreak.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/checks/impl/breaking/WrongBreak.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/events/packets/CheckManagerListener.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/nmsutil/ReachUtils.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/change/BlockModification.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/change/BlockModification.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/events/packets/PacketServerTeleport.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/events/packets/PacketServerTeleport.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/data/TeleportData.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/nmsutil/JumpPower.java
Comment thread common/src/main/java/ac/grim/grimac/utils/math/VectorUtils.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/math/VectorUtils.java Outdated
Comment thread common/src/main/java/ac/grim/grimac/utils/math/Vec2d.java Outdated
@ManInMyVan
Copy link
Copy Markdown
Contributor

please address my 27 comments before I review this again

@Axionize Axionize force-pushed the optimize/vector-objects branch from a763af1 to cad28d6 Compare October 30, 2025 17:42
@Axionize
Copy link
Copy Markdown
Contributor Author

@ManInMyVan I believe I've addressed all your existing code review concerns.

Comment on lines +498 to +500
// Previously we called Vector3dm.add(player.likelyExplosions.vector) after checking (player.firstBreadExplosion != null)
// which would've thrown an NPE if player.likelyExplosions could be null here, we can ignore this
// empirically this means when firstBreadExplosion != null then likelyExplosions is also != null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't really like comments referencing removed code, can you reword this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can't think of a way how to explain this effectively without it...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you could just skip the explanation and say it shouldn't be null

Comment on lines 951 to 954
public Vector3dm getMovementResultFromInput(GrimPlayer player, double x, double y, double z, float f, float f2) {
float f2InRadians = GrimMath.radians(f2);
float f3 = player.trigHandler.sin(f2InRadians);
float f4 = player.trigHandler.cos(f2InRadians);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

might as well deobf aswell

Suggested change
public Vector3dm getMovementResultFromInput(GrimPlayer player, double x, double y, double z, float f, float f2) {
float f2InRadians = GrimMath.radians(f2);
float f3 = player.trigHandler.sin(f2InRadians);
float f4 = player.trigHandler.cos(f2InRadians);
public Vector3dm getMovementResultFromInput(GrimPlayer player, double x, double y, double z, float speed, float yaw) {
float yawRadians = GrimMath.radians(yaw);
float yawSin = player.trigHandler.sin(yawRadians);
float yawCos = player.trigHandler.cos(yawRadians);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you're certain these are correct I'll apply them

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it's based on a usage's inputs

Comment on lines +31 to +34
vectorData.vectorX *= swimmingFriction;
vectorData.vectorY *= 0.8F;
vectorData.vectorZ *= swimmingFriction;
vectorData.vectorY = FluidFallingAdjustedMovement.getAdjustedY(player, playerGravity, isFalling, vectorData.vectorY);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
vectorData.vectorX *= swimmingFriction;
vectorData.vectorY *= 0.8F;
vectorData.vectorZ *= swimmingFriction;
vectorData.vectorY = FluidFallingAdjustedMovement.getAdjustedY(player, playerGravity, isFalling, vectorData.vectorY);
vectorData.vectorX *= swimmingFriction;
vectorData.vectorY = FluidFallingAdjustedMovement.getAdjustedY(player, playerGravity, isFalling, vectorData.vectorY * 0.8F);
vectorData.vectorZ *= swimmingFriction;

Comment on lines +186 to +187
data.vectorX = data.vectorX + player.trigHandler.sin(GrimMath.radians(-player.yaw)) * f;
data.vectorZ = data.vectorZ + (double) (player.trigHandler.cos(GrimMath.radians(player.yaw)) * f);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
data.vectorX = data.vectorX + player.trigHandler.sin(GrimMath.radians(-player.yaw)) * f;
data.vectorZ = data.vectorZ + (double) (player.trigHandler.cos(GrimMath.radians(player.yaw)) * f);
data.vectorX += player.trigHandler.sin(GrimMath.radians(-player.yaw)) * f;
data.vectorZ += (double) (player.trigHandler.cos(GrimMath.radians(player.yaw)) * f);

Comment on lines +183 to +185
// TODO (Mutation) (Confirm if Correct)
// It seems possible for applyStuckSpeed = 0, and 1 to add the same vector to the set twice
// since we're mutating data.vectorX, Y, Z which is very likely inorrect but I don't know enough to say for certain
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we could check if the player has a stuck speed before looping both if that's the issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The problem is that the data reference is the same, so if you modify data.vectorX it'll be applied to both instances of data in the list

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you could make VectorData cloneable and add this before you modify it

if (!player.isForceStuckSpeed()) {
    data = data.clone();
}

Copy link
Copy Markdown
Contributor Author

@Axionize Axionize Dec 17, 2025

Choose a reason for hiding this comment

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

@SamB440 @AoElite I need your input on this. Should we clone the reference or preserve the exiting mutation behavior?

The behavior here seems like a bug, but could it might not be and I don't know how to tell/test? What are the chances we can ask @MWHunter ?

@andreasdc
Copy link
Copy Markdown

I was wondering if there’s an update on whether this PR is expected to be merged, it could be a nice optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

3 participants