From 5fc9fc66188e4f1216cf9635651280efd5bc4d8b Mon Sep 17 00:00:00 2001 From: Chris Richards Date: Mon, 23 Nov 2026 14:50:46 +0000 Subject: [PATCH] Resolve the table path once and suppress the finalizer on Close Opening a table by relative path resolved the current directory six times (File.Exists, the FileStream open and four memo-file checks). Each lookup costs about 10 us on macOS, which made opening a small table by relative path far slower than by absolute path. The path is now resolved once and reused. DbfTable.Close called Dispose(true) directly, so the finalizer was never suppressed and every closed table was promoted to the next GC generation. Close now goes through Dispose(). Co-Authored-By: Claude Fable 5.2 --- src/DbfDataReader/DbfTable.cs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/DbfDataReader/DbfTable.cs b/src/DbfDataReader/DbfTable.cs index 7a7e97f..74d7eba 100644 --- a/src/DbfDataReader/DbfTable.cs +++ b/src/DbfDataReader/DbfTable.cs @@ -20,18 +20,22 @@ public class DbfTable : Disposable public DbfTable(string path, Encoding encoding = null, StringTrimmingOption stringTrimming = StringTrimmingOption.Trim, bool readFloatsAsDecimals = false) { - if (!File.Exists(path)) throw new FileNotFoundException(); + // resolve a relative path once: each File.Exists or FileStream open on a + // relative path looks up the current directory again, which costs far more + // than the check itself + var fullPath = System.IO.Path.GetFullPath(path); + if (!File.Exists(fullPath)) throw new FileNotFoundException(); Path = path; CurrentEncoding = encoding; StringTrimming = stringTrimming; ReadFloatsAsDecimals = readFloatsAsDecimals; - Stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + Stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Init(); - var memoPath = MemoPath(); + var memoPath = FindMemoPath(fullPath); if (!string.IsNullOrEmpty(memoPath)) Memo = CreateMemo(memoPath); } @@ -85,7 +89,9 @@ private void Init() public void Close() { - Dispose(true); + // through Dispose() so that the finalizer is suppressed; otherwise every + // closed table is still finalized and survives into the next GC generation + Dispose(); } protected override void Dispose(bool disposing) @@ -104,13 +110,18 @@ protected override void Dispose(bool disposing) } public string MemoPath() + { + return FindMemoPath(Path); + } + + private static string FindMemoPath(string tablePath) { var paths = new[] { - System.IO.Path.ChangeExtension(Path, "fpt"), - System.IO.Path.ChangeExtension(Path, "FPT"), - System.IO.Path.ChangeExtension(Path, "dbt"), - System.IO.Path.ChangeExtension(Path, "DBT") + System.IO.Path.ChangeExtension(tablePath, "fpt"), + System.IO.Path.ChangeExtension(tablePath, "FPT"), + System.IO.Path.ChangeExtension(tablePath, "dbt"), + System.IO.Path.ChangeExtension(tablePath, "DBT") }; foreach (var path in paths)