Skip to content

Commit 8d5b978

Browse files
committed
Optimize document renaming in PrettifyNames
Conflict checking was O(n), now it is O(1).
1 parent ed941db commit 8d5b978

1 file changed

Lines changed: 16 additions & 29 deletions

File tree

sources/SilkTouch/SilkTouch/Mods/PrettifyNames.cs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,15 @@ await proj.GetCompilationAsync(ct)
195195

196196
// Change the filenames where appropriate.
197197
proj = ctx.SourceProject;
198+
198199
var typeNames = newNames.GetValueOrDefault("", []);
199200
var typeNamesLongestFirst = typeNames.OrderByDescending(x => x.Key.Length).ToArray();
200201

202+
var documentPaths = proj
203+
.Documents.Select(d => d.FilePath)
204+
.Where(d => d != null)
205+
.ToHashSet();
206+
201207
foreach (var docId in proj.DocumentIds)
202208
{
203209
var doc = proj.GetDocument(docId);
@@ -206,6 +212,7 @@ await proj.GetCompilationAsync(ct)
206212
continue;
207213
}
208214

215+
// Find best matching document for renamed types
209216
var firstMatch = typeNamesLongestFirst.FirstOrDefault(x =>
210217
doc.FilePath.Contains(x.Key) || doc.Name.Contains(x.Key)
211218
);
@@ -214,43 +221,23 @@ await proj.GetCompilationAsync(ct)
214221
continue;
215222
}
216223

224+
// Rename doc and update path
217225
var originalName = doc.Name;
226+
var originalPath = doc.FilePath;
218227
doc = doc.ReplaceNameAndPath(oldName, newName);
219228

220-
var found = false;
221-
if (doc.FilePath is not null)
222-
{
223-
foreach (var checkDocId in proj.DocumentIds)
224-
{
225-
if (checkDocId == docId)
226-
{
227-
continue;
228-
}
229-
230-
var checkDoc = proj.GetDocument(checkDocId);
231-
if (checkDoc?.FilePath is null)
232-
{
233-
continue;
234-
}
235-
236-
if (checkDoc.FilePath == doc.FilePath)
237-
{
238-
found = true;
239-
break;
240-
}
241-
}
242-
}
243-
244-
if (found)
229+
// Check for path conflict
230+
documentPaths.Remove(originalPath);
231+
if (!documentPaths.Add(doc.FilePath!))
245232
{
246233
logger.LogError(
247234
$"{originalName} -> {doc.Name} failed to rename file as a file already exists at {doc.FilePath}"
248235
);
236+
237+
continue;
249238
}
250-
else
251-
{
252-
proj = doc.Project;
253-
}
239+
240+
proj = doc.Project;
254241
}
255242

256243
ctx.SourceProject = proj;

0 commit comments

Comments
 (0)