-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand FileStreamOptions.PreallocationSize description #7289
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
Conversation
|
Tagging subscribers to this area: @carlossanlop Issue Detailsnull
|
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
| }; | ||
| using var destination = new FileStream(DestinationPath, createForWriting); | ||
|
|
||
| source.CopyTo(destination); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good for showing PreallocationSize usage, but there are better ways to copy a file.
If you'd want to use a more 'realistic' use-case, you could consider something like:
- write a large file with random data.
- download a large file using
HttpClient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to keep the main code example as simple as possible. Mixing HttpClient here would be distracting.
I'm not opposed to adding a separate code example showing what you described though. We can add as many code examples as we want. But the main one should be simple.
Co-authored-by: Günther Foidl <gue@korporal.at>
This comment has been minimized.
This comment has been minimized.
xml/System.IO/FileStreamOptions.xml
Outdated
| The file length is dependent on how many bytes were actually written to the file, not on how many were requested to be preallocated. | ||
| For example: if the user preallocates a 2 GB file, but writes only 1 GB to it, the final file length will be 1 GB. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamsitnik here's your suggestion, but changed size for length:
| For example: if the user preallocates a 2 GB file, but writes only 1 GB to it, the final file length will be 1 GB. | |
| For example, if the user preallocates a 2 GB file, but writes only 1 GB to it, the allocated size will be 2 GB until the file gets closed, and after that, the length will be reported as 1 GB. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but changed size for length
I think that this is wrong. The size will be reported as 2 GB until the fd is closed.
I am not 100% sure so most probably you should write some small console app and verify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean changing size to length is incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The length will be 2 GB until the file gets closed, and after that, the length will be reported as 1 GB.
+ The size will be 2 GB until the file gets closed, and after that, the size will be reported as 1 GB.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the word length as it describes the position of the EOF.
size is more ambiguous, it can either be length, or the amount of space allocated (cfr preallocationSize).
When you close the file on Linux, the length will be 1 GB, and the allocated size for it will still be 2 GB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so I think my suggestion above is the correct one. LTM if you feel like I should change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you close the file on Linux, the length will be 1 GB, and the allocated size for it will still be 2 GB.
Isn't that a bug? Also, is there a way to revert the size to match the length? I wonder if using preallocation size you may end up with a full disk sooner than not using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is there a way to revert the size to match the length?
Not using available .NET APIs.
I wonder if using preallocation size you may end up with a full disk sooner than not using it.
If you use this to specify the length of the file you're about to write, the difference should not be an issue.
If you specify a large value and only write a small file, you'll be wasting disk space if you don't intent to write to it further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not using available .NET APIs.
What about with unix syscalls? Furthermore, I wonder if it may be a good idea to free the unused blocks when the FileStream gets Dispose'd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Linux, fallocate can also be used to deallocate.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@gewarren would you mind taking a look once again? I made several modifications after your sign-off. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| <summary>The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created, overwritten, or replaced. | ||
| Negative values are not allowed. | ||
| In other cases (including the default 0 value), it's ignored.</summary> | ||
| <summary>The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created or overwritten (<see cref="F:System.IO.FileMode.Create"/> or <see cref="F:System.IO.FileMode.CreateNew"/>). Negative values are not allowed. In other cases (including the default 0 value), it's ignored. This value is a hint and is not a strong guarantee. It is not supported on Web Assembly (WASM) and FreeBSD (the value is ignored). For Windows, Linux and macOS we will try to preallocate the disk space to fill the requested allocation size. If that turns out to be impossible, the operation is going to throw an exception. The final file length (EOF) will be determined by the number of bytes written to the file.</summary> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems too long for a summary, and some of it is repeated in the remarks. I'd suggest something like:
| <summary>The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created or overwritten (<see cref="F:System.IO.FileMode.Create"/> or <see cref="F:System.IO.FileMode.CreateNew"/>). Negative values are not allowed. In other cases (including the default 0 value), it's ignored. This value is a hint and is not a strong guarantee. It is not supported on Web Assembly (WASM) and FreeBSD (the value is ignored). For Windows, Linux and macOS we will try to preallocate the disk space to fill the requested allocation size. If that turns out to be impossible, the operation is going to throw an exception. The final file length (EOF) will be determined by the number of bytes written to the file.</summary> | |
| <summary>The initial allocation size in bytes for the file. For important details, see the Remarks section of the docs.</summary> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gewarren It was done on purpose. We want the PreallocationSize summary to contain all the info in IntelliSense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| <summary>The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created or overwritten (<see cref="F:System.IO.FileMode.Create"/> or <see cref="F:System.IO.FileMode.CreateNew"/>). Negative values are not allowed. In other cases (including the default 0 value), it's ignored. This value is a hint and is not a strong guarantee. It is not supported on Web Assembly (WASM) and FreeBSD (the value is ignored). For Windows, Linux and macOS we will try to preallocate the disk space to fill the requested allocation size. If that turns out to be impossible, the operation is going to throw an exception. The final file length (EOF) will be determined by the number of bytes written to the file.</summary> | |
| <summary>The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created or overwritten (<see cref="F:System.IO.FileMode.Create"/> or <see cref="F:System.IO.FileMode.CreateNew"/>). Negative values are not allowed. In other cases (including the default 0 value), it's ignored. This value is a hint and is not a strong guarantee. It is not supported on Web Assembly (WASM) and FreeBSD (the value is ignored). For Windows, Linux, and macOS, .NET attempts to preallocate the disk space to fill the requested allocation size. If that is unsuccessful, the operation throws an exception. The final file length (EOF) is determined by the number of bytes written to the file.</summary> |
adamsitnik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, big thanks for writing it down @carlossanlop !
BTW I love all these OS differences. They make our job so easy...
samples/snippets/csharp/System.IO/FileStreamOptions/PreallocationSizeExample.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
This comment has been minimized.
This comment has been minimized.
|
Docs Build status updates of commit 0154397: ✅ Validation status: passed
For more details, please refer to the build report. Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report. For any questions, please:
|
As discussed in dotnet/runtime#59705 , we want to make sure the documentation is clear and detailed on how to use
System.IO.FileStreamOptions.PreallocationSize.