Skip to content

Fixed Unnecessary Computation Issue Caused by String.format() #3444

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

Closed
wants to merge 7 commits into from

Conversation

dukbong
Copy link
Contributor

@dukbong dukbong commented Apr 23, 2024

This PR improved the code by using Assert methods instead of throwing IllegalArgumentException in if-else statements.

  • 33eb67a : fixed an issue where the computation was processed immediately due to instant evaluation, regardless of the Assert outcome.

  • You have read the Spring Data contribution guidelines.

  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.

  • You submit test cases (unit or integration tests) that back your changes.

  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 23, 2024
@dukbong dukbong changed the title Improve the code using Assert methods. Fixed Unnecessary Computation Issue Caused by 'String.format()' Apr 23, 2024
@dukbong dukbong changed the title Fixed Unnecessary Computation Issue Caused by 'String.format()' Fixed Unnecessary Computation Issue Caused by 'String.format() ` Apr 23, 2024
@dukbong dukbong changed the title Fixed Unnecessary Computation Issue Caused by 'String.format() ` Fixed Unnecessary Computation Issue Caused by String.format() Apr 23, 2024
Copy link
Member

@mp911de mp911de left a comment

Choose a reason for hiding this comment

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

Excessive switching from if statements that throw an exception into Assert utility usage is counterintuitive in many places. There are also occurrences that change the thrown exception type.

It makes sense to revisit Assert usages in combination with String.format to defer message creation. However, in places that explicitly throw exceptions, we would like to keep the code as-is.
Once an exception is being thrown, we need to construct the message and that is a fine arrangement already.

}

throw new IllegalStateException(String.format("No annotated query found for query method %s", getName()));
Assert.notNull(query, () -> String.format("No annotated query found for query method %s", getName()));
Copy link
Member

Choose a reason for hiding this comment

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

This change changes behavior into throwing IllegalArgumentException instead of IllegalStateException. At that point, throwing an explicit IllegalStateException is fine so we should not change this method.


throw new IllegalArgumentException(
String.format("%s managed by more than one EntityManagers: %s", type.getName(), candidates));
Assert.isTrue(candidates.size() == 1, () -> String.format("%s managed by more than one EntityManagers: %s", type.getName(), candidates));
Copy link
Member

Choose a reason for hiding this comment

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

One thought for here: The control flow is being replaced with calls to Assert. While assertions are fine for method-entry checks, usage here is kind-of unexpected. Also, we're always creating a capturing lambda leading to a bit more GC pressure.

if (this.idType == null) {
throw new IllegalStateException("Cannot resolve Id type from " + type);
}
Assert.notNull(this.idType, () -> String.format("Cannot resolve Id type from %s", type));
Copy link
Member

Choose a reason for hiding this comment

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

This change changes IllegalStateException to IllegalArgumentException

if (mi == null)
throw new IllegalStateException(
"No MethodInvocation found: Check that an AOP invocation is in progress, and that the "
Assert.notNull(mi, "No MethodInvocation found: Check that an AOP invocation is in progress, and that the "
Copy link
Member

Choose a reason for hiding this comment

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

This change changes behavior into throwing IllegalArgumentException instead of IllegalStateException.

@dukbong
Copy link
Contributor Author

dukbong commented Apr 24, 2024

Excessive switching from if statements that throw an exception into Assert utility usage is counterintuitive in many places. There are also occurrences that change the thrown exception type.

It makes sense to revisit Assert usages in combination with String.format to defer message creation. However, in places that explicitly throw exceptions, we would like to keep the code as-is. Once an exception is being thrown, we need to construct the message and that is a fine arrangement already.

I apologize for negatively impacting the open-source project management.
I will close this PR and resubmit it with only the necessary operations from commit 33eb67a.

Before sending the PR, I will ensure that it is thoroughly checked multiple times.

I'm sorry again for the inconvenience caused.

@dukbong dukbong closed this Apr 24, 2024
@dukbong dukbong deleted the lambda branch April 24, 2024 13:17
@dukbong
Copy link
Contributor Author

dukbong commented Apr 24, 2024

Excessive switching from if statements that throw an exception into Assert utility usage is counterintuitive in many places. There are also occurrences that change the thrown exception type.
It makes sense to revisit Assert usages in combination with String.format to defer message creation. However, in places that explicitly throw exceptions, we would like to keep the code as-is. Once an exception is being thrown, we need to construct the message and that is a fine arrangement already.

I apologize for negatively impacting the open-source project management. I will close this PR and resubmit it with only the necessary operations from commit 33eb67a.

Before sending the PR, I will ensure that it is thoroughly checked multiple times.

I'm sorry again for the inconvenience caused.

#3445

@mp911de
Copy link
Member

mp911de commented Apr 24, 2024

First of all, do not worry, and there's no need to apologize. We're happy to provide guidance.

Please also note that you can force-push into a pull request until the final merge so that you can reuse already open pull requests.

@dukbong
Copy link
Contributor Author

dukbong commented Apr 24, 2024

Oh, I wasn't aware of that. I have now created a separate PR for the parts that urgently need to be fixed. I'll keep this in mind and use it more effectively going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants