Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

optimizing-io-operations

/optimizing-io-operations

Optymalizuje operacje wejścia-wyjścia i pliki w .NET do przetwarzania dużych danych i zadań konkursowych.

Licencja
MIT
Ostatnia zmiana repozytorium
21.07.2026

Ocena i bezpieczeństwo

84/ 100Klasa: dobry
Format9 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
2
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
2
Przestarzałość

Brak sygnałów. Repozytorium zmienione 21.07.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Optymalizuje operacje wejścia-wyjścia i pliki w .NET do przetwarzania dużych danych i zadań konkursowych.

Oryginalny opis (EN)

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.

Jak działa

  1. 1. Core APIs
  2. 2. High-Speed Standard I/O
  3. 3. File I/O Optimization
  4. 4. Large File Processing
  5. 5. Performance Comparison
  6. 6. Important Notes
  7. 7. References
Sekcje z nagłówków SKILL.md, w oryginale · 233 linii, 601 słów
Podgląd SKILL.md
---
description: "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."
user-invocable: false
model: sonnet
---

# .NET High-Performance I/O

A guide for APIs optimizing large-scale data input/output.

**Quick Reference:** See [QUICKREF.md](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

```csharp
// 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

```csharp
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

```csharp
// 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

```csharp
// 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

```csharp
// 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+)

```csharp
// 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

```csharp
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

```csharp
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

```csharp
// Read UTF-8 without BOM
using var reader = new StreamReader(
    stream,
    new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
```

### Flush Timing

```csharp
// Improve performance with AutoFlush = false
writer.AutoFlush = false;

// Manual flush after important data
writer.Flush();
```

---

## 7. References

- [File and Stream I/O](https://learn.microsoft.com/en-us/dotnet/standard/io/)
- [Memory-Mapped Files](https://learn.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files)

Pliki w paczce (2)
  • QUICKREF.md 1 KB
  • SKILL.md 5 KB

Wersje

W bazie nie ma innych skilli o nazwie optimizing-io-operations.

Migawka bazy z 13.11.2026 · wersja paczki 17c8b9b1221e (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 90file-processing
    Projektuje strumieniowe przetwarzanie dużych plików i przesyłań oraz poprawia wydajność operacji wejścia wyjścia.
  • 84implementing-io-pipelines
    Wdraża strumieniowe przetwarzanie danych przez System.IO.Pipelines w .NET, przy protokołach sieciowych i dużych strumieniach.
  • 84optimizing-memory-allocation
    Stosuje wzorce zero-allocation w .NET z Span, ArrayPool i ObjectPool, by zmniejszyć presję na GC.
  • 100optimizing-performance
    Analizuje i optymalizuje wydajność aplikacji we frontendzie, backendzie i bazie danych: diagnozuje wolne działanie, skraca czas ładowania, optymalizuje zapytania i zmniejsza paczkę.
  • 100system-io-pipelines
    Prowadzi przez System.IO.Pipelines w .NET: wydajne parsowanie strumieni, zarządzanie buforami bez kopiowania, PipeReader i PipeWriter, parsowanie protokołów sieciowych i ReadOnlySequence.
  • 100file-operations
    Tworzy, czyta, edytuje, wyświetla i usuwa pliki oraz bada strukturę katalogów projektu.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.