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

[WIP] Fix #2869: Avoid unnecessary updates #2941

Conversation

hensou
Copy link
Contributor

@hensou hensou commented Jun 28, 2022

  • Keep output in a memory stream
  • Only write to file if there is a change or if the file is new

Fix #2869

- Keep output in memory
- Only write to file if there is a change or file is new

Fix fable-compiler#2869
src/Fable.Cli/Main.fs Outdated Show resolved Hide resolved
if count = 0 then return None
else
let res =
if count = buffer.Length then buffer
Copy link
Member

Choose a reason for hiding this comment

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

As we are using a single buffer, if count = 0 we just return a reference to the buffer. But in the lines below:

                    let! nextBytes1 = readBytesAsync memoryStream
                    let! nextBytes2 = readBytesAsync fileStream 

nextBytes1 is going to be same as nextBytes2 (if count is 0 in both cases), because readBytesAsync returns the same array reference in both cases and the second readBytesAsync will overwrite the buffer.

@alfonsogarciacaro
Copy link
Member

This is fantastic @hensou, thanks a lot for this! I've rewritten a bit the IsMemoryStreamEqualToFileAsync function to fix the issue with the shared buffer and simplify a bit the code. I hope that's ok with you.

        let IsMemoryStreamEqualToFileAsync() = async {
            let areStreamsEqualAsync (stream1: IO.Stream) (stream2: IO.Stream) =
                let buffer1 = Array.zeroCreate<byte> 1024
                let buffer2 = Array.zeroCreate<byte> 1024

                let rec areStreamsEqualAsync() = async {
                    let! count1 = stream1.AsyncRead(buffer1, 0, buffer1.Length)
                    let! count2 = stream2.AsyncRead(buffer2, 0, buffer2.Length)
                    match count1, count2 with
                    | 0, 0 -> return true
                    | count1, count2 when count1 = count2 && buffer1 = buffer2 ->
                        return! areStreamsEqualAsync()
                    | _ -> return false
                }

                areStreamsEqualAsync()

            memoryStream.Seek(0, IO.SeekOrigin.Begin) |> ignore
            use fileStream = IO.File.OpenRead(targetPath)

            return! areStreamsEqualAsync memoryStream fileStream
        }

@alfonsogarciacaro alfonsogarciacaro merged commit 8750e1f into fable-compiler:main Jun 29, 2022
@hensou
Copy link
Contributor Author

hensou commented Jun 29, 2022

This is fantastic @hensou, thanks a lot for this! I've rewritten a bit the IsMemoryStreamEqualToFileAsync function to fix the issue with the shared buffer and simplify a bit the code. I hope that's ok with you.

        let IsMemoryStreamEqualToFileAsync() = async {
            let areStreamsEqualAsync (stream1: IO.Stream) (stream2: IO.Stream) =
                let buffer1 = Array.zeroCreate<byte> 1024
                let buffer2 = Array.zeroCreate<byte> 1024

                let rec areStreamsEqualAsync() = async {
                    let! count1 = stream1.AsyncRead(buffer1, 0, buffer1.Length)
                    let! count2 = stream2.AsyncRead(buffer2, 0, buffer2.Length)
                    match count1, count2 with
                    | 0, 0 -> return true
                    | count1, count2 when count1 = count2 && buffer1 = buffer2 ->
                        return! areStreamsEqualAsync()
                    | _ -> return false
                }

                areStreamsEqualAsync()

            memoryStream.Seek(0, IO.SeekOrigin.Begin) |> ignore
            use fileStream = IO.File.OpenRead(targetPath)

            return! areStreamsEqualAsync memoryStream fileStream
        }

Awesome, I'm glad I could help.🙂
The rewritten it's totally fine, the way you did is way more simpler and elegant.
Thanks again.

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.

Avoid unnecessary updates
3 participants