AbsolutePath.ReadKind allocates a new 16 KiB buffer per directory on Windows: the pooled buffer is not returned on failure
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
TruePath/DiskUtils.csから始め、特にArrayPool<byte>.Sharedによるバッファの借用とDeviceIoControl失敗時の返却に関わるTryGetReparseDataBufferを確認します。次に、WindowsでAbsolutePath.ReadKind()がこれを呼び出す流れを追います。提示されたループで報告された割り当てを再現し、Windowsのパス種別に関する既存のテストを確認します。通常のディレクトリで借用したバッファを再利用するか、借用を避けることで、再解析ポイントの結果を変えずに割り当てが減れば完了です。
索引モデルが issue の本文から書いたものです。
説明
AbsolutePath.ReadKind() on Windows ends up in DiskUtils.TryGetReparseDataBuffer for every directory. The method rents a 16 KiB buffer from ArrayPool<byte>.Shared, but returns it only on the success path. For anything that isn't a reparse point (i.e. almost every directory), DeviceIoControl(FSCTL_GET_REPARSE_POINT) fails, the method does an early return null, and the rented buffer is dropped. Every call then allocates a fresh 16 KiB array instead of reusing the pooled one.
Measured on ReadKind() (allocated bytes per call):
- a file: 41 B
- a directory: 16,480 B
Repro:
var dir = new AbsolutePath(Path.GetTempPath());
for (int i = 0; i < 100; i++) dir.ReadKind(); // warmup
var before = GC.GetAllocatedBytesForCurrentThread();
for (int i = 0; i < 10_000; i++) dir.ReadKind();
Console.WriteLine((GC.GetAllocatedBytesForCurrentThread() - before) / 10_000); // ~16480
This is not a correctness failure, but it defeats the pool and produces a lot of garbage in directory-walking code.
Recommendations
- Return the buffer on the
DeviceIoControlfailure path too.DeviceIoControlhas finished writing by then, so this is safe (the "abandon rather than return" advice only applies to buffers that might still be in use). - Alternatively, rent the buffer only when it is actually needed:
ReadKind()already reads the attributes, and a junction always hasFileAttributes.ReparsePointset, so the reparse query can be skipped for everything else. (A 16 KiBstackallocis not a good replacement for the pool: it is well above the ~1 KB guideline for stack buffers.)
- 主要言語
- C#
- スター
- 107
- フォーク
- 22
- 平均マージ
- 3日 2時間
- マージ済み PR(30日)
- 13
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ForNeVeR/TruePath のほかの issue
-
Wrong values of Kernel32.FileShare.Delete and Kernel32.FileAttributes.PosixSemantics対応中かも @ElectLive が今日担当しました。 オープンbug good first issue help wanted
難易度 1/5 1時間未満 初心者へのやさしさ 83/100
ForNeVeR/TruePath#241 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ForNeVeR/TruePath#165 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
documentation help wanted
難易度 3/5 1〜2日 初心者へのやさしさ 57/100
メンテナーはふだん 1 日以内に返信
-
bug good first issue help wanted
難易度 3/5 半日 初心者へのやさしさ 57/100
メンテナーはふだん 1 日以内に返信
-
enhancement help wanted
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
メンテナーはふだん 1 日以内に返信
ForNeVeR/TruePath の issue をすべて見る
似ている issue
-
Backend enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
dimitar-radenkov/Pointframe#271 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
qew21/Genshin-Subtitles#45 ·
-
bug priority: P2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
space-wizards/RobustToolbox#7158 ·
メンテナーはふだん 1 日以内に返信