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

Optimize StringSequence.startsWith #21259

Conversation

dreis2211
Copy link
Contributor

Hi,

I noticed that a vanilla Boot app spends some time in StringSequence.startsWith or respectively StringSequence.charAt:
image

This PR optimizes startsWith in cases where the length doesn't differ from the source length, which essentially means that StringSequence is "equal" to the source. In those cases, we can use startsWith on the actual source and by that avoid the repeated boundary checks inside charAt. A little benchmark of old vs. new approach shows the following results.

Benchmark                                Mode  Cnt   Score    Error   Units
MyBenchmark.testNew                      avgt   10   6,270 ±  0,255   ns/op
MyBenchmark.testNew:·gc.alloc.rate       avgt   10  ≈ 10⁻⁴           MB/sec
MyBenchmark.testNew:·gc.alloc.rate.norm  avgt   10  ≈ 10⁻⁶             B/op
MyBenchmark.testNew:·gc.count            avgt   10     ≈ 0           counts
MyBenchmark.testOld                      avgt   10   9,029 ±  0,330   ns/op
MyBenchmark.testOld:·gc.alloc.rate       avgt   10  ≈ 10⁻⁴           MB/sec
MyBenchmark.testOld:·gc.alloc.rate.norm  avgt   10  ≈ 10⁻⁶             B/op
MyBenchmark.testOld:·gc.count            avgt   10     ≈ 0           counts

Let me know what you think.
Cheers,
Christoph

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 29, 2020
@philwebb philwebb added theme: performance Issues related to general performance type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels May 2, 2020
@philwebb philwebb added this to the 2.3.x milestone May 2, 2020
@philwebb
Copy link
Member

philwebb commented May 2, 2020

Nice!

@philwebb philwebb modified the milestones: 2.3.x, 2.3.0 May 6, 2020
philwebb pushed a commit that referenced this pull request May 6, 2020
philwebb added a commit that referenced this pull request May 6, 2020
@philwebb philwebb closed this in fb9bf7a May 6, 2020
@philwebb
Copy link
Member

philwebb commented May 6, 2020

Moving the parameter type to String opens the door for even more optimizations I think. @dreis2211 and @wilkinsona I'd appreciate it if you could look at commit 4a8492d and see if it makes sense.

@philwebb philwebb reopened this May 6, 2020
@philwebb
Copy link
Member

philwebb commented May 6, 2020

Reopening to ensure we get at least one other pair of eyes on this before we release.

@dreis2211
Copy link
Contributor Author

Looks good to me. In reality it won't change much though, because there are actually no other cases in the code where the length differs from the original string (apart from tests). So it's a good polishing commit, but no real performance improvement anymore ;)

@wilkinsona
Copy link
Member

Thanks, @dreis2211. FWIW, it looks good to me too.

@wilkinsona wilkinsona closed this May 6, 2020
@philwebb
Copy link
Member

philwebb commented May 6, 2020

Thanks for the reviews!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: performance Issues related to general performance type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants