Optimizing Io Operations
БесплатноБез исполняемых скриптовНе проверенOptimizes standard I/O and file operations for high-performance data processing in .NET. Use when building high-throughput file processing or competitive…
от christian289★ 42 звезды на GitHubОб этом скилле
.NET High-Performance I/O
A guide for APIs optimizing large-scale data input/output.
Quick Reference: See QUICKREF.md for essential patterns at a glance.
1. Core APIs
| API | Purpose |
|---|---|
Console.OpenStandardInput() |
Buffered stream input |
Console.OpenStandardOutput() |
Buffered stream output |
BufferedStream |
Stream buffering |
FileOptions.Asynchronous |
Async file I/O |
2. High-Speed Standard I/O
2.1 Basic Pattern
// Use buffer stream directly for large I/O
using var inputStream = Console.OpenStandardInput();
using var outputStream = Console.OpenStandardOutput();
using var reader = new StreamReader(inputStream, bufferSize: 65536);
using var writer = new StreamWriter(outputStream, bufferSize: 65536);
// Disable buffer flush for performance improvement
writer.AutoFlush = false;
string? line;
while ((line = reader.ReadLine()) is not null)
{
writer.WriteLine(ProcessLine(line));
}
// Manual flush at the end
writer.Flush();
2.2 For Algorithm Problem Solving
using System.Text;
// High-speed input
using var reader = new StreamReader(
Console.OpenStandardInput(),
Encoding.ASCII,
bufferSize: 65536);
// High-speed output
using var writer = new StreamWriter(
Console.OpenStandardOutput(),
Encoding.ASCII,
bufferSize: 65536);
var sb = new StringBuilder();
// Collect large output in StringBuilder and write at once
for (int i = 0; i < 100000; i++)
{
sb.AppendLine(i.ToString());
}
writer.Write(sb);
writer.Flush();
3. File I/O Optimization
3.1 Buffer Size Optimization
// Use larger buffer than default (4KB)
const int bufferSize = 64 * 1024; // 64KB
using var fileStream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
bufferSize: bufferSize);
3.2 Async File I/O
// Open file with async option
using var fileStream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
bufferSize: 4096,
options: FileOptions.Asynchronous);
var buffer = new byte[4096];
int bytesRead = await fileStream.ReadAsync(buffer);
3.3 SequentialScan Hint
// Provide hint to OS for sequential reading
using var fileStream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
bufferSize: 64 * 1024,
options: FileOptions.SequentialScan);
3.4 RandomAccess (.NET 6+)
// Direct offset access without file position management
using var handle = File.OpenHandle(path, FileMode.Open, FileAccess.Read);
var buffer = new byte[4096];
long offset = 1000;
int bytesRead = RandomAccess.Read(handle, buffer, offset);
// Async version
bytesRead = await RandomAccess.ReadAsync(handle, buffer, offset);
4. Large File Processing
4.1 Chunk-Based Reading
public async IAsyncEnumerable<byte[]> ReadChunksAsync(
string path,
int chunkSize = 64 * 1024,
[EnumeratorCancellation] CancellationToken ct = default)
{
using var stream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
bufferSize: chunkSize,
options: FileOptions.Asynchronous | FileOptions.SequentialScan);
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, ct)) > 0)
{
if (bytesRead == chunkSize)
{
yield return buffer;
buffer = new byte[chunkSize];
}
else
{
yield return buffer[..bytesRead];
}
}
}
4.2 Memory-Mapped Files
using System.IO.MemoryMappedFiles;
// Map large file to memory
using var mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open);
using var accessor = mmf.CreateViewAccessor();
// Direct memory access
byte value = accessor.ReadByte(position);
accessor.Write(position, newValue);
5. Performance Comparison
| Method | Relative Performance | Use Case |
|---|---|---|
| Console.ReadLine() | 1x (baseline) | General |
| StreamReader (default buffer) | 2x | Large data |
| StreamReader (64KB buffer) | 3-5x | Large data |
| MemoryMappedFile | 5-10x | Very large data |
6. Important Notes
Buffer Size
- Too small increases system calls
- Too large wastes memory
- Recommended: 4KB ~ 64KB
Encoding Specification
// Read UTF-8 without BOM
using var reader = new StreamReader(
stream,
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
Flush Timing
// Improve performance with AutoFlush = false
writer.AutoFlush = false;
// Manual flush after important data
writer.Flush();
7. References
Установить Optimizing Io Operations в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл Optimizing Io Operations?
Optimizes standard I/O and file operations for high-performance data processing in .NET. Use when building high-throughput file processing or competitive programming solutions.
Как установить скилл Optimizing Io Operations?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл Optimizing Io Operations запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
от Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
от Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
от Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
от Anthropic