Skip to content

Use file allocation size where it's beneficial #51116

Closed

Description

@adamsitnik
  1. Identify places, where we know the file size up-front. Example: File.WriteAll*. File.Copy etc.
  2. Ensure that the given source code has benchmarks coverage in the performance repo (contribute new benchmarks).
  3. Change the source code to use the new FileStream ctor that allows for specifying allocationSize and specify the exact value (or estimation as OS is smart enough to shrink the file (when it's closed) if it was too much).
  4. Compare before vs after. If there is a visible performance or reliability gain, contribute the change to dotnet/runtime.

Since #51111 has not been merged yet, please use it for measurements.

Activity

  1. added this to the 6.0.0 milestone on Apr 12, 2021
  2. ghost added
    untriaged
    on Apr 12, 2021
  3. removed
    untriaged
    on Apr 12, 2021
  4. jozkee commented on Apr 13, 2021

    @jozkee
    Member

    For places where we receive text instead of bytes, e.g: File.WriteAllText, should we try to determine the size in bytes up-front?

  5. sakno commented on May 3, 2021

    @sakno
    Contributor

    Do we need support this for Memory-Mapped Files? Dup of this comment.

  6. dmex commented on May 11, 2021

    @dmex

    @jozkee

    Can you please try benchmarking #51298 again with the preallocation changes but adding FILE_FLAG_SEQUENTIAL_SCAN to kernel32.createfile and FILE_SEQUENTIAL_ONLY to ntdll.createfile?

    For *Async methods please try using FILE_FLAG_RANDOM_ACCESS to kernel32.createfile or FILE_RANDOM_ACCESS to ntdll.createfile.

    WriteAllBytes is a sequential operation but dotnet doesn't use correct flags in these cases... The cache manager is likely trying to prefetch data from the file and potentially degrading the performance when preallocation was used which is not what you want when using functions like WriteAllBytes which are sequential.

    See also:
    https://devblogs.microsoft.com/oldnewthing/20120120-00/?p=8493

  7. GSPP commented on May 19, 2021

    @GSPP

    Why should async IO use RANDOM_ACCESS specifically? Just curious for the reasoning.

  8. dmex commented on May 20, 2021

    @dmex

    @GSPP

    Why should async IO use RANDOM_ACCESS specifically? Just curious for the reasoning.

    Just to compare the benchmark results with the other benchmarks.

    @jozkee reported a "perf regression of 10%" when using preallocation which was unexpected... I'm thinking some of the changes related to 40359 and 51111 might be missing a call somewhere after the handle was opened and potentially trashing/polluting the cache manager when running the benchmarks and by comparing the results of different flags we could probably determine the prediction model that was being used by the cache manager at the time.

  9. modified the milestones: 6.0.0, 7.0.0 on Jul 22, 2021
  10. adamsitnik commented on Oct 12, 2021

    @adamsitnik
    MemberAuthor

    I am closing the issue as it's pointless with the regression that has been recently introduced and backported to 6.0 (#59705)

  11. ghost locked as resolved and limited conversation to collaborators on Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.IOtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions