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

Remove massive string allocations #5940

Merged
merged 5 commits into from
Nov 20, 2018
Merged

Remove massive string allocations #5940

merged 5 commits into from
Nov 20, 2018

Conversation

AviAvni
Copy link
Contributor

@AviAvni AviAvni commented Nov 20, 2018

See #5922

Copy link
Contributor

@forki forki left a comment

Choose a reason for hiding this comment

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

The spurce is not normalized anymore. I'm not sure if that makes any difference, but in any case the property name "NormalizedSource" is now a lie

src/fsharp/NameResolution.fs Outdated Show resolved Hide resolved
@AviAvni
Copy link
Contributor Author

AviAvni commented Nov 20, 2018

@forki Done thanks

Copy link
Contributor

@forki forki left a comment

Choose a reason for hiding this comment

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

If this actually gives a green build then it looks like a massive win for saving allocations

src/fsharp/CheckFormatStrings.fs Show resolved Hide resolved
src/fsharp/NameResolution.fs Outdated Show resolved Hide resolved
@AviAvni
Copy link
Contributor Author

AviAvni commented Nov 20, 2018

@forki thanks you right now the LineEndPositions is according to the source and not the normalized source I think now it's ok

[|
let mutable pos = 0
yield pos
for i in 0..source.Length-1 do
Copy link
Contributor

Choose a reason for hiding this comment

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

i and pos are always in sync right? So you can get rid of that mutable variable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry thanks

@AviAvni
Copy link
Contributor Author

AviAvni commented Nov 20, 2018

ok so now I need to fix the tests

Copy link
Contributor

@TIHan TIHan left a comment

Choose a reason for hiding this comment

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

This looks good on my end, if everything is green I will merge.

@AviAvni
Copy link
Contributor Author

AviAvni commented Nov 20, 2018

It's green

@TIHan TIHan merged commit 87ea864 into dotnet:master Nov 20, 2018
@TIHan
Copy link
Contributor

TIHan commented Nov 20, 2018

Thank you

let c = source.[i]
if c = '\r' && i + 1 < source.Length && source.[i+1] = '\n' then ()
elif c = '\r' then yield i + 1
if c = '\n' then yield i + 1
Copy link

Choose a reason for hiding this comment

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

Should not this be an elif? (It does not change the semantics but improves clarity.) @AviAvni

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine. alter native would be to pattern match on c

yield 0
for i in 0..source.Length-1 do
let c = source.[i]
if c = '\r' && i + 1 < source.Length && source.[i+1] = '\n' then ()
Copy link

Choose a reason for hiding this comment

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

style/readbility comment: looping through 1..source.Length instead of 0..source.Length-1 would make more sense here, since we already processed the 0th byte with the yield 0 above the loop. This would also avoid the i+1 in four places:

 [|
    yield 0
    for i in 1..source.Length do
         let c = source.[i-1]
         if c = '\r' && i < source.Length && source.[i] = '\n' then ()
         elif c = '\r' then yield i
         elif c = '\n' then yield i
    yield source.Length
 |]

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I think we should avoid duplicates as well.

{ NormalizedSource = source
LineEndPositions = positions })
[|
yield 0
Copy link

Choose a reason for hiding this comment

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

Possible bug: So suppose the file content is just the 1 byte character \n. This will give for positions:
[| 0; 1; 1 |] Should not we want just [| 0; 1 |] instead? CC: @forki @AviAvni

|> Seq.toArray
{ NormalizedSource = source
LineEndPositions = positions })
[|
Copy link

Choose a reason for hiding this comment

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

Using Seq.pairwise might be more elegant here, rahter than indexing in arrays. (Though I'm not sure if perf would be better or worse taking into account possible compiler optimization and CPU caching).

@AviAvni
Copy link
Contributor Author

AviAvni commented Nov 20, 2018

@blumu thanks add the fixes #5943

@@ -433,8 +433,7 @@ let _ = printf " %*a" 3 (fun _ _ -> ()) 2
typeCheckResults.GetFormatSpecifierLocationsAndArity()
|> Array.map (fun (range,numArgs) -> range.StartLine, range.StartColumn, range.EndLine, range.EndColumn, numArgs)
|> shouldEqual
[|(2, 45, 2, 47, 1); (3, 23, 3, 25, 1); (4, 38, 4, 40, 1); (5, 27, 5, 29
, 1);
[|(2, 45, 2, 47, 1); (3, 23, 3, 25, 1); (4, 38, 4, 40, 1); (5, 27, 5, 29, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just remove line break

@forki
Copy link
Contributor

forki commented Nov 20, 2018

this is amazeballs

will

KevinRansom pushed a commit that referenced this pull request Nov 30, 2018
* small fixes

* fix test
dsyme pushed a commit that referenced this pull request Dec 5, 2018
* Reduce memory allocations (#5965)

* reduce memory allocations

* remove memory allocations

* Smal fixes for #5940 (#5943)

* small fixes

* fix test
Sign up for free to 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