Skip to content

TEST1 Checksum Calculator#6

Open
khusainovilas wants to merge 3 commits intomainfrom
test1-checksum-calculator
Open

TEST1 Checksum Calculator#6
khusainovilas wants to merge 3 commits intomainfrom
test1-checksum-calculator

Conversation

@khusainovilas
Copy link
Owner

No description provided.

Buffer.BlockCopy(childHash, 0, combinedBuffer, currentOffset, childHash.Length);
currentOffset += childHash.Length;
}

Choose a reason for hiding this comment

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

Стоило бы поправить предупреждения, а то по делу ведь. И по правилам собираться должно без предупреждений.

Comment on lines +15 to +16
private static readonly int MaxDegreeOfParallelism = Environment.ProcessorCount;
private static readonly SemaphoreSlim FileReadSemaphore = new(MaxDegreeOfParallelism, MaxDegreeOfParallelism);

Choose a reason for hiding this comment

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

Я бы такими низкоуровневыми вещами не занимался, а оставил бы это штатному пулу потоков. Это не ошибка, но в целом пустая трата усилий.

private static async ValueTask SemaphoreSlimWaitAsync(CancellationToken cancellationToken)
{
await FileReadSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
}

Choose a reason for hiding this comment

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

Используйте =>

Comment on lines +97 to +98
using var md5 = MD5.Create();
return md5.ComputeHash(combinedBuffer);

Choose a reason for hiding this comment

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

Есть у MD5 статический метод, который делает то же самое (правда, называется как-то по-другому). Объект MD5 тут низачем.

try
{
await using var fileStream = File.OpenRead(filePath);
using var memoryStream = new MemoryStream(nameBytes.Length + (int)fileStream.Length);

Choose a reason for hiding this comment

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

Так Вы весть файл в память зачитаете, что не очень, если это какая-нибудь метагеномная сборка в пару терабайт размером.


return await this.md5.ComputeHashAsync(memoryStream, cancellationToken);
}
} No newline at end of file

Choose a reason for hiding this comment

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

Тут довольно много чего можно унифицировать с параллельной версией (в т.ч. по смыслу — хеш от одного файла всё равно считается последовательно всегда), так что можно было применить немного архитектурной магии (паттерны "Стратегия", "Шаблонный метод" и т.п.) и сократить размер кода в полтора раза. Архитектурную магию мы ещё не проходили, так что это не ошибка, но можно было хоть попытаться :)

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.

2 participants