E5: il conto «Copia anche gli N» non è più quadratico nei file uguali

Ogni file si contava una volta con un NOT EXISTS per file, che per i
file con lo stesso contenuto rileggeva tutti quelli prima: 5-6 s sulla
dimostrazione, 11-14 s con le statistiche. Ora solo gli SHA-256 che più
file hanno passano da un GROUP BY, gli altri si contano direttamente:
0,3 s su 30.000 file di 60 contenuti, dove prima servivano 54 s. Con
«Una copia sola» il numero resta quello della cartella sola, e ora la
documentazione lo dice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-04 20:07:37 +01:00
1 parent 11e9dd276c
commit 3c48582067
3 files changed
+83 -12

No files matched your search

@@ -101,6 +101,68 @@ public sealed class LibraryScaleTests
await MeasureAll(db, accountIds, copies, still, "con ANALYZE", ct);
}
/// <summary>
/// The count of «Copia anche gli N» on a library where many files have the same content (reposts, shared stories; the
/// demonstration: about 30,000 files of 131 contents): 30,000 files of 60 contents over 37 accounts, each account's
/// files together, as an account is saved at once. The count of each file once per account, and once in all with
/// «Una copia sola», stays within the bound of the automatic export, without and with ANALYZE statistics; a NOT
/// EXISTS per file took about 5 s here (quadratic in the files of the same content).
/// </summary>
[TestMethod]
[Timeout(600_000, CooperativeCancellation = true)] // only a hang guard
public async Task TheBacklogCountStaysFastWithManyFilesOfTheSameContent()
{
const int Files = 30_000;
const int Contents = 60;
const int Owners = 37;
var ct = TestContext.CancellationToken;
using var temp = new TempFolder();
await using var db = await TestDatabase.OpenAsync(temp);
var owners = new List<long>();
for (var a = 0; a < Owners; a++)
{
owners.Add((await db.Accounts.AddAsync(Make.Account($"stesso{a:00}.demo", $"{5000 + a}"), ct)).Id);
}
var start = new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero);
for (var batch = 0; batch < Files / 5000; batch++)
{
var items = new List<NewEntry>(5000);
for (var i = 0; i < 5000; i++)
{
var n = (batch * 5000) + i;
var owner = owners[Math.Min(Owners - 1, n / (Files / Owners))];
items.Add(new NewEntry(Make.Entry(owner, $"s{n}", start.AddMinutes(n)), [Make.Stored(Make.Photo(), Make.Sha((n % Contents) + 1), $"s/{n}.jpg")]));
}
await db.Entries.AddManyAsync(items, ct);
}
var general = await db.Exports.AddDestinationAsync(
new ExportDestination { Scope = ExportScope.General, RootPath = temp.Combine("usb"), ContentTypes = ContentTypes.All, CreatedAt = Make.T0 }, ct);
var query = new ExportBacklogQuery(ExportScope.General, null, ContentTypes.All, general.Id, null);
// Each (content, account) pair once; once per content with «Una copia sola».
var pairs = Enumerable.Range(0, Files).Select(n => (n % Contents, Math.Min(Owners - 1, n / (Files / Owners)))).Distinct().Count();
foreach (var label in new[] { "senza statistiche", "con ANALYZE" })
{
if (label == "con ANALYZE")
{
await db.WriteAsync(w => w.Command("ANALYZE").ExecuteNonQuery(), ct);
}
await (ExportSettings.Default with { SamePost = SamePostCopies.OnePerAccount }).SaveAsync(db.Settings, ct);
var perAccount = await TestWait.BestTimeAsync(() => db.Exports.CountAutomaticBacklogAsync(query, ct), AutomaticBoundMs, ct: ct);
Assert.AreEqual(new ExportBacklog(pairs, pairs * 1000L), perAccount.Result, label);
await (ExportSettings.Default with { SamePost = SamePostCopies.OneOnly }).SaveAsync(db.Settings, ct);
var oneOnly = await TestWait.BestTimeAsync(() => db.Exports.CountAutomaticBacklogAsync(query, ct), AutomaticBoundMs, ct: ct);
Assert.AreEqual(new ExportBacklog(Contents, Contents * 1000L), oneOnly.Result, label);
TestContext.WriteLine($"{label}: per account {perAccount.BestMs:F0} ms, una copia sola {oneOnly.BestMs:F0} ms (limite {AutomaticBoundMs:F0} ms)");
Assert.IsLessThanOrEqualTo(AutomaticBoundMs, perAccount.BestMs, $"{label}: per account");
Assert.IsLessThanOrEqualTo(AutomaticBoundMs, oneOnly.BestMs, $"{label}: una copia sola");
}
}
/// <summary>
/// The export side of the library: the general folder for every account, an own folder for account 4 (post photos
/// only, so its videos are not due), account 7 switched off. The copies are written in bulk on the due files only,