Skip to content

Small improvement for String.length - #9469

Merged
KevinRansom merged 4 commits into
dotnet:masterfrom
abelbraaksma:String_performance
Jun 17, 2020
Merged

Small improvement for String.length#9469
KevinRansom merged 4 commits into
dotnet:masterfrom
abelbraaksma:String_performance

Conversation

@abelbraaksma

@abelbraaksmaabelbraaksma commented Jun 17, 2020

Copy link
Copy Markdown
Contributor

The PR removes some overhead for String.length, see #9390 (comment).

The disassembly will change from this:

testrax,rax ; rax & rax, in other words: null-check, sets flagsje short M00_L01 ; if zero (null) jump back to loopcmp dword ptr [rax+8],0 ; compare string-length to 0jbe short M00_L01 ; if equal, jump back to loopmovecx,[rax+8] ; if not, get string length fallthrough to loop

to this:

testrax,rax ; rax & rax, for null-check, sets flagsje short M00_L01 ; if zero (null) jump back to loopmovecx,[rax+8] ; get the string-length from the string and fallthrough to loop

Which, in timings, gives roughly a 33% improvement, but since this is hard to test in isolation, it may be more because more chances exist now for optimizing & inlining this by the JIT (which, in this isolated case, indeed happens).

More info and timings in the connected issue comment above.

@abelbraaksma

Copy link
Copy Markdown
ContributorAuthor

Hmm, I've contributed before, though it's a while ago that I made PR's. From time to time you have to sign again?

image

@cartermp

Copy link
Copy Markdown
Contributor

The CLA is a consequence of moving to dotnet/fsharp I believe?

@abelbraaksma

Copy link
Copy Markdown
ContributorAuthor

Possible, anyway, signing took 3 seconds, so it's not a big deal :)

Comment threadsrc/fsharp/FSharp.Core/string.fs Outdated
Comment threadsrc/fsharp/FSharp.Core/string.fs Outdated
Comment threadsrc/fsharp/FSharp.Core/string.fs Outdated
Comment threadsrc/fsharp/FSharp.Core/string.fs Outdated
@KevinRansom

Copy link
Copy Markdown
Contributor

@abelbraaksma It seems as though you combined two changes into one PR and proposed it twice.

/cc @vzarytovskii

@abelbraaksma

Copy link
Copy Markdown
ContributorAuthor

@KevinRansom, yeah, that's why I shouldn't do this long after midnight ;)

I meant to split them (@cartermp and @forkiasked for a PR per function), and thought I only locally had done the accidental merge.

I used VS's github integration, and couldn't switch branches on the PR page, so I couldn't create a new PR on the 2nd branch, and after some frustration clearly did something else wrong ;)

Will fix.

Comment threadsrc/fsharp/FSharp.Core/string.fs Outdated

@cartermpcartermp left a comment

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.

Thanks!

@KevinRansom

Copy link
Copy Markdown
Contributor

@abelbraaksma thanks for taking care of this.

Kevin

@KevinRansom
KevinRansom merged commit 0d1e1d4 into dotnet:masterJun 17, 2020
@abelbraaksma
abelbraaksma deleted the String_performance branch June 17, 2020 20:40
baronfel pushed a commit to baronfel/FSharp.Compiler.Service that referenced this pull request Jun 20, 2020
* Simplify and improve perf of String.length
* Improve performance of String.map
* Revert "Improve performance of String.map"
* Turn String.length into a one-liner, fixesdotnet/fsharp#9469 (comment)
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Feb 23, 2021
* Simplify and improve perf of String.length
* Improve performance of String.map
* Revert "Improve performance of String.map"
* Turn String.length into a one-liner, fixesdotnet#9469 (comment)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@abelbraaksma@cartermp@KevinRansom@forki