From 34daf96c48a004a4450bc42519778f032676f31e Mon Sep 17 00:00:00 2001 From: leemour Date: Wed, 25 Nov 2026 05:24:44 +0100 Subject: [PATCH] perf(mail): build the throwaway-domain set once for every caller isDisposableEmailDomain from disposable-email-domains-js builds its set of about 9,000 domains on every call. scoreGroup called it once per sender, so scoring 20,000 senders spent about 7 s on it. #3347 had already cached the set inside domain-organizations.ts; the cache now lives in mail/score.ts as isDisposableDomain, next to FREE_MAIL, and both callers use it. Synthetic run, 20,000 senders of 3 emails each: 7.0 s -> 0.21 s. Co-Authored-By: Claude Opus 5.5 --- changes/perf-disposable-domain-check.md | 5 +++++ src/mail/score.test.ts | 7 ++++++- src/mail/score.ts | 12 ++++++++++-- src/store/sqlite/domain-organizations.ts | 12 ++---------- 4 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 changes/perf-disposable-domain-check.md diff --git a/changes/perf-disposable-domain-check.md b/changes/perf-disposable-domain-check.md new file mode 100644 index 000000000..eac76bdbd --- /dev/null +++ b/changes/perf-disposable-domain-check.md @@ -0,0 +1,5 @@ +### Fixed + +- **Scoring mail senders no longer rebuilds the throwaway-domain list for each sender.** The spam score + (`scoreGroup`) and the organizations from mail domains now share one set built on first use + (`isDisposableDomain`). Scoring 20,000 senders took about 7 s and now takes about 0.2 s. diff --git a/src/mail/score.test.ts b/src/mail/score.test.ts index 52a00e398..7a2066aaa 100644 --- a/src/mail/score.test.ts +++ b/src/mail/score.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from "vitest" import { headersOf, mailHeadersOf } from "./headers.js" import { domainsToAge, probeMail } from "./probe.js" -import { FREE_MAIL, scoreGroup, spoofedBrand, youngAt } from "./score.js" +import { FREE_MAIL, isDisposableDomain, scoreGroup, spoofedBrand, youngAt } from "./score.js" import type { Listed } from "./source.js" const head = (lines: string[]) => `${lines.join("\r\n")}\r\n` @@ -137,6 +137,11 @@ describe("scoreGroup", () => { expect(scoreGroup(throwaway, { replied: false, bulk: 0, ages }).signals).toEqual(["disposable_domain"]) }) + it("knows a throwaway domain from the shipped list and leaves mailbox providers and companies alone", () => { + expect(["mailinator.com", "guerrillamail.com"].map(isDisposableDomain)).toEqual([true, true]) + expect(["gmail.com", "shop.example"].map(isDisposableDomain)).toEqual([false, false]) + }) + it("adds mail never opened and a DKIM check most of the mail did not pass, and ignores unknowns", () => { const robot = [1, 2].map((n) => email(n, ["From: deals@shop.example", "Authentication-Results: mx.google.com; spf=pass"], { seen: false }), diff --git a/src/mail/score.ts b/src/mail/score.ts index cc55d83e3..6c7735f59 100644 --- a/src/mail/score.ts +++ b/src/mail/score.ts @@ -1,4 +1,4 @@ -import { isDisposableEmailDomain } from "disposable-email-domains-js" +import { disposableEmailBlocklistSet } from "disposable-email-domains-js" import { getDomain } from "tldts" import type { Listed } from "./source.js" @@ -85,6 +85,14 @@ export const FREE_MAIL = new Set([ "zoho.com", ]) +let disposable: Set | undefined +// `isDisposableEmailDomain` builds its set of about 9,000 domains on every call. +/** A throwaway mail domain (CC0 list); `domain` is lower case, as `registrableOf` gives it. */ +export const isDisposableDomain = (domain: string): boolean => { + disposable ??= disposableEmailBlocklistSet() + return disposable.has(domain) +} + /** A brand and the first labels of the domains it really sends from. */ const BRANDS: Record = { paypal: ["paypal"], @@ -192,7 +200,7 @@ export const scoreGroup = ( // Gmail's stamp without a dkim= verdict means the mail carried no signature: that counts as not passed. const stamped = items.filter((item) => item.headers?.auth !== undefined) if (stamped.length && most(stamped, (item) => item.headers?.auth?.dkim !== "pass")) signals.push("dkim_not_passed") - if (domain !== undefined && isDisposableEmailDomain(domain)) signals.push("disposable_domain") + if (domain !== undefined && isDisposableDomain(domain)) signals.push("disposable_domain") if (context.bulk >= 0.8 && !context.replied) signals.push("bulk_never_replied") if (context.replied) signals.push("replied") const kept = diff --git a/src/store/sqlite/domain-organizations.ts b/src/store/sqlite/domain-organizations.ts index 409d71d0f..b6eec7f20 100644 --- a/src/store/sqlite/domain-organizations.ts +++ b/src/store/sqlite/domain-organizations.ts @@ -1,8 +1,7 @@ -import { disposableEmailBlocklistSet } from "disposable-email-domains-js" import { getDomainWithoutSuffix } from "tldts" import { isReadable } from "../../domain/display-names.js" import { isCompanyName } from "../../domain/person-kinds.js" -import { FREE_MAIL, registrableOf } from "../../mail/score.js" +import { FREE_MAIL, isDisposableDomain, registrableOf } from "../../mail/score.js" import type { CacheDatabase } from "../driver.js" import { fold } from "../normalize.js" import { actorOfOrigin } from "./actors.js" @@ -42,18 +41,11 @@ export const BY_DOMAIN = "domain" /** A rejected `proposed_actions` row of this kind keeps a rule from making an organization for its domain again. */ export const REFUSED_DOMAIN = "organization-domain" -let disposable: Set | undefined -// `isDisposableEmailDomain` builds the whole set on every call: about 0.5 ms an address. -const isDisposable = (domain: string): boolean => { - disposable ??= disposableEmailBlocklistSet() - return disposable.has(domain) -} - /** The registrable domain whose organization an address joins; none for free, throwaway and platform mail. */ export const companyDomainOf = (address: string): string | undefined => { if (!address.includes("@")) return undefined const domain = registrableOf(address) - return domain.includes(".") && !FREE_MAIL.has(domain) && !SENDING_PLATFORMS.has(domain) && !isDisposable(domain) + return domain.includes(".") && !FREE_MAIL.has(domain) && !SENDING_PLATFORMS.has(domain) && !isDisposableDomain(domain) ? domain : undefined }