Skip to content

Use substring instead of replaceFirst in OAuth2AuthorizationConsent #1223

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 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Set<String> getScopes() {
Set<String> authorities = new HashSet<>();
for (GrantedAuthority authority : getAuthorities()) {
if (authority.getAuthority().startsWith(AUTHORITIES_SCOPE_PREFIX)) {
authorities.add(authority.getAuthority().replaceFirst(AUTHORITIES_SCOPE_PREFIX, ""));
authorities.add(authority.getAuthority().substring(AUTHORITIES_SCOPE_PREFIX.length()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I conducted a simple test and achieved a 40 times performance improvement.

How did you conduct the test?

Copy link
Contributor Author

@heartape heartape May 27, 2023

Choose a reason for hiding this comment

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

This is all the testing information:

public static void main(String[] args) {
    String scope = "SCOPE_";
    String email = "SCOPE_email";
    int count = 10000000;

    long start1 = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        String s = email.replaceFirst(scope, "");
    }
    long end1 = System.currentTimeMillis();
    System.out.println("replaceFirst : " + (end1 - start1));

    long start2 = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        String s = email.substring(scope.length());
    }
    long end2 = System.currentTimeMillis();
    System.out.println("substring : " + (end2 - start2));
    System.out.println("count : " + count);
}
windows(8c16t 32G) idea 
java version "17.0.3.1" 2022-04-22 LTS
Java(TM) SE Runtime Environment (build 17.0.3.1+2-LTS-6)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.3.1+2-LTS-6, mixed mode, sharing)

result:
replaceFirst : 2383
substring : 69
count : 10000000

###############################################################

centos7 vm(2c 2G)  command line 
java version "17.0.6" 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

result:
replaceFirst : 2856
substring : 107
count : 10000000

}
}
return authorities;
Expand Down