Skip to content

Commit 922cbaa

Browse files
committed
Improve explanation of the derivation of BestCaseInertProbabilityThreshold().
1 parent 159dc90 commit 922cbaa

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

ift/encoder/merger.cc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,29 @@ StatusOr<std::optional<GlyphSet>> Merger::MergeSegmentWithCosts(
398398
double Merger::BestCaseInertProbabilityThreshold(
399399
uint32_t base_patch_size, double base_probability, double lowest_cost_delta
400400
) const {
401-
// For a merge of an inert base patch with any other inert segment, this
402-
// computes the minimum probability the other segment must have for
403-
// it to be possible to produce a delta lower than lowest_cost_delta.
401+
// The following assumptions are made:
402+
// - P(base) >= P(other)
403+
// - the best case merged size is max(base_size, other_size) + k
404404
//
405-
// This formula is derived by making the following assumptions:
406-
// - The probability of the merged segment = base_probability.
407-
// - The size of the merged patch is (base_patch_size + BEST_CASE_MERGE_SIZE_DELTA)
405+
// Then if we start with the formula for the cost delta of an inert merge:
408406
//
409-
// These two assumptions give the best possible cost delta for the merge.
407+
// P(merged) * merged_size - P(base) * base_size - P(other) * other_size
408+
//
409+
// (here all sizes include the network overhead delta).
410+
//
411+
// And consider what valid values of P(merged), and other_size will produce the
412+
// lowest total delta we find that this happens when:
413+
// - P(merged) = P(base)
414+
// - other_size = base_size
415+
// - merged_size = base_size + k
416+
//
417+
// From that we find that the smallest possible delta is:
418+
//
419+
// min(cost delta) = P(base) * k - P(other) * base_size
420+
//
421+
// From which we find that:
422+
//
423+
// P(other) > (k * P(base) - lowest_cost_delta) / base_size
410424
base_patch_size += Strategy().NetworkOverheadCost();
411425
return std::min(1.0, std::max(0.0,
412426
(((double) BEST_CASE_MERGE_SIZE_DELTA) * base_probability - lowest_cost_delta) / ((double) base_patch_size)));

ift/encoder/merger.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ class Merger {
143143
absl::Status ApplyInitFontMove(const common::GlyphSet& glyphs_to_move,
144144
double delta);
145145

146-
// Computes the minimum probability an inert segment must have for it to be possible
147-
// to have a lower cost delta than lowest_cost_delta when merged with the inert base patch.
146+
// For a merge of an inert base patch with any other possible inert segment, this
147+
// computes the minimum probability the other segment must have for
148+
// it to be possible to produce a delta lower than lowest_cost_delta
149+
// (regardless of it's probability or size).
150+
//
151+
// This assumes that P(base) >= P(other)
148152
double BestCaseInertProbabilityThreshold(
149153
uint32_t base_patch_size, double base_probability, double lowest_cost_delta
150154
) const;

0 commit comments

Comments
 (0)