Last active
April 15, 2022 19:52
-
-
Save airbreather/48fdecdc2dca0b657dc9642cc8458ba0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Concurrent; | |
using System.IO.MemoryMappedFiles; | |
using BlockingCollection<string> coll = new(); | |
var threads = new Thread[100]; | |
foreach (ref var thread in threads.AsSpan()) | |
{ | |
thread = new Thread(RunThread) { IsBackground = true }; | |
thread.Start(coll); | |
} | |
var filesFromInputDirectories = | |
from arg in args | |
where Directory.Exists(arg) | |
let dir = new DirectoryInfo(arg) | |
from file in dir.EnumerateFiles("*", SearchOption.AllDirectories) | |
select file; | |
var inputFiles = | |
from arg in args | |
where File.Exists(arg) | |
select new FileInfo(arg); | |
var files = | |
from file in filesFromInputDirectories.Concat(inputFiles) | |
where file.Length > 0 | |
orderby file.Length | |
select file; | |
foreach (var file in files.Distinct()) | |
{ | |
coll.Add(file.FullName); | |
} | |
coll.CompleteAdding(); | |
foreach (var thread in threads) | |
{ | |
thread.Join(); | |
} | |
static unsafe void RunThread(object? collObj) | |
{ | |
if (collObj is not BlockingCollection<string> coll) | |
{ | |
throw new ArgumentException("Expected a collection.", nameof(collObj)); | |
} | |
foreach (string fl in coll.GetConsumingEnumerable()) | |
{ | |
using var fs = new FileStream(fl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); | |
using var f = MemoryMappedFile.CreateFromFile(fs, null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, true); | |
using var acc = f.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read); | |
long len = acc.Capacity; | |
byte* b = null; | |
try | |
{ | |
acc.SafeMemoryMappedViewHandle.AcquirePointer(ref b); | |
for (long i = 0; i < len; i += 4096) | |
{ | |
_ = b[i]; | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine(ex); | |
} | |
finally | |
{ | |
if (b != null) | |
{ | |
acc.SafeMemoryMappedViewHandle.ReleasePointer(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment