From 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:33 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- .../places/PlacesFrecencyRecalculator.sys.mjs | 55 ++++++++++++---------- 1 file changed, 30 insertions(+), 25 deletions(-) (limited to 'toolkit/components/places/PlacesFrecencyRecalculator.sys.mjs') diff --git a/toolkit/components/places/PlacesFrecencyRecalculator.sys.mjs b/toolkit/components/places/PlacesFrecencyRecalculator.sys.mjs index 2de558af34..7c52c508ee 100644 --- a/toolkit/components/places/PlacesFrecencyRecalculator.sys.mjs +++ b/toolkit/components/places/PlacesFrecencyRecalculator.sys.mjs @@ -51,6 +51,14 @@ XPCOMUtils.defineLazyPreferenceGetter( 90 ); +// For origins frecency calculation only sample pages visited recently. +XPCOMUtils.defineLazyPreferenceGetter( + lazy, + "originsFrecencyCutOffDays", + "places.frecency.originsCutOffDays", + 90 +); + // Time between deferred task executions. const DEFERRED_TASK_INTERVAL_MS = 2 * 60000; // Maximum time to wait for an idle before the task is executed anyway. @@ -218,16 +226,19 @@ export class PlacesFrecencyRecalculator { let affectedCount = 0; let db = await lazy.PlacesUtils.promiseUnsafeWritableDBConnection(); await db.executeTransaction(async () => { + // NULL frecencies are normalized to 1.0 (to avoid confusion with pages + // 0 frecency special meaning), as the table doesn't support NULL values. let affected = await db.executeCached( ` UPDATE moz_origins - SET frecency = CAST( - (SELECT total(frecency) - FROM moz_places h - WHERE origin_id = moz_origins.id AND frecency > 0) - AS INT - ), - recalc_frecency = 0 + SET frecency = IFNULL(( + SELECT sum(frecency) + FROM moz_places h + WHERE origin_id = moz_origins.id + AND last_visit_date > + strftime('%s','now','localtime','start of day', + '-${lazy.originsFrecencyCutOffDays} day','utc') * 1000000 + ), 1.0), recalc_frecency = 0 WHERE id IN ( SELECT id FROM moz_origins WHERE recalc_frecency = 1 @@ -238,25 +249,19 @@ export class PlacesFrecencyRecalculator { ); affectedCount += affected.length; - // Calculate and store the frecency statistics used to calculate a - // thredhold. Origins above that threshold will be considered meaningful - // and autofilled. + // Calculate and store the frecency threshold. Origins whose frecency is + // above this value will be considered meaningful and autofilled. // While it may be tempting to do this only when some frecency was // updated, that won't catch the edge case of the moz_origins table being // emptied. - let row = ( - await db.executeCached(` - SELECT count(*), total(frecency), total(pow(frecency,2)) - FROM moz_origins - WHERE frecency > 0 - `) - )[0]; - await lazy.PlacesUtils.metadata.setMany( - new Map([ - ["origin_frecency_count", row.getResultByIndex(0)], - ["origin_frecency_sum", row.getResultByIndex(1)], - ["origin_frecency_sum_of_squares", row.getResultByIndex(2)], - ]) + // In case of NULL, the default threshold is 2, that is higher than the + // default frecency set above. + let threshold = ( + await db.executeCached(`SELECT avg(frecency) FROM moz_origins`) + )[0].getResultByIndex(0); + await lazy.PlacesUtils.metadata.set( + "origin_frecency_threshold", + threshold ?? 2 ); }); @@ -343,7 +348,7 @@ export class PlacesFrecencyRecalculator { } } - observe(subject, topic, data) { + observe(subject, topic) { lazy.logger.trace(`Got ${topic} topic`); switch (topic) { case "idle-daily": @@ -524,7 +529,7 @@ class AlternativeFrecencyHelper { return affected; } - async #recalculateSomePagesAlternativeFrecencies({ chunkSize, variables }) { + async #recalculateSomePagesAlternativeFrecencies({ chunkSize }) { lazy.logger.trace( `Recalculate ${chunkSize} alternative pages frecency values` ); -- cgit v1.2.3