Skip to content

Commit c36f071

Browse files
committed
I, me, myself. was the one out of the line on this: using FileShare.Read **makes things faster** on C#, I don't have the slighest idea why. But it does, point.
So I'm putting it back, with a nice reminder to my future self about "measure, don't guess - or assume".
1 parent ed68f4d commit c36f071

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Source/ModuleManager/Utils/FileUtils.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,27 @@ public static string FileSHA(string filename)
4444
* there will be just not a single logic reason to keep them opened in memory - unless KSP is not calling the Dispose
4545
* or not using the `using` construction and, so, we have a file handlers leaking on this damned thing.
4646
*
47-
* In a way or another, the proposed solution on pull/request 180 to the upstream is, IMHO, completely out of the line.
47+
* In a way or another, the proposed solution on pull/request 180 to the upstream is, IMHO, less than ideal.
4848
* **WE JUST DON'T** open executable files with Writing privileges, **POINT**. At very least, this will prevent anti-virus
4949
* software from being triggered on us, avoiding slowing down KSP's file accesses.
5050
*
5151
* So I will not use `FileShare.ReadWrite` no matter what. I terminantly refuse to do so.
52+
*
53+
* But I had to reconsider and use `FileShare.Read` after doing some benchmarks, as by some reason it's the fastest
54+
* way to open a file under C#, even on a UNIX.
5255
*
5356
* See:
5457
* + https://github.com/sarbian/ModuleManager/pull/180
5558
* + https://forum.kerbalspaceprogram.com/index.php?/topic/50533-18x-112x-module-manager-422-june-18th-2022-the-heatwave-edition/page/302/#comment-4283448
59+
* + https://forum.kerbalspaceprogram.com/index.php?/topic/50533-18x-112x-module-manager-422-june-18th-2022-the-heatwave-edition/page/303/#comment-4284427
60+
* + https://github.com/net-lisias-ksp/KSPe/commit/4fcced165ce72edcf5db2c95311ebafc02d6a921
5661
*/
5762
Exception ex = null;
5863
int i = 8; // Max wait: 1 second
5964
while (i-- > 0) try
6065
{
6166
using (SHA256 sha = SHA256.Create())
62-
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
67+
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
6368
data = sha.ComputeHash(fs);
6469
break;
6570
}

0 commit comments

Comments
 (0)