diff --git a/MODULE.bazel b/MODULE.bazel index 2390800f..0120340d 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -15,7 +15,7 @@ bazel_dep(name = "riegeli", version = "0.0.0-20250822-9f2744d") bazel_dep(name = "platforms", version = "1.0.0") bazel_dep(name = "rules_rust", version = "0.68.1") bazel_dep(name = "glib", version = "2.82.2.bcr.7") -bazel_dep(name = "brotli", version = "1.2.0") +bazel_dep(name = "brotli", version = "1.2.1") bazel_dep(name = "bazel_skylib", version = "1.9.0") # Frequency Data diff --git a/brotli/BUILD b/brotli/BUILD index 4147dd40..28235a16 100644 --- a/brotli/BUILD +++ b/brotli/BUILD @@ -40,12 +40,16 @@ cc_library( cc_library( name = "shared_brotli_encoder", hdrs = [ + "brotli_arena.h", "shared_brotli_encoder.h", ], visibility = [ "//visibility:public", ], deps = [ + "@abseil-cpp//absl/log", + "@abseil-cpp//absl/strings", + "@abseil-cpp//absl/types:span", "@brotli//:brotli_inc", "@brotli//:brotlienc", ], diff --git a/brotli/brotli_arena.h b/brotli/brotli_arena.h new file mode 100644 index 00000000..818d7c39 --- /dev/null +++ b/brotli/brotli_arena.h @@ -0,0 +1,98 @@ +#ifndef BROTLI_BROTLI_ARENA_H_ +#define BROTLI_BROTLI_ARENA_H_ + +#include +#include +#include +#include +#include + +#include "absl/log/log.h" + +namespace brotli { + +/* Reusable bump-pointer arena for Brotli encoder and dictionary allocations. */ +class BrotliArena { + public: + static constexpr size_t kAlignment = alignof(std::max_align_t); + static_assert(kAlignment >= sizeof(size_t)); + + explicit BrotliArena(size_t capacity = 256u * 1024u * 1024u) + : capacity_(capacity), offset_(0), buf_(nullptr) {} + + BrotliArena(const BrotliArena&) = delete; + BrotliArena& operator=(const BrotliArena&) = delete; + BrotliArena(BrotliArena&&) = default; + BrotliArena& operator=(BrotliArena&&) = default; + + static void* Alloc(void* opaque, size_t size) { + if (!opaque) { + return nullptr; + } + auto* self = static_cast(opaque); + if (!self->buf_) { + self->buf_.reset(new uint8_t[self->capacity_]); + } + // Round size up to multiple of kAlignment bytes for proper alignment. + size_t payload_size = (size + kAlignment - 1) & ~(kAlignment - 1); + size_t total_size = payload_size + kAlignment; + if (self->offset_ < self->capacity_ && + total_size <= self->capacity_ - self->offset_) { + *reinterpret_cast(self->buf_.get() + self->offset_) = + payload_size; + void* ptr = self->buf_.get() + self->offset_ + kAlignment; + self->offset_ += total_size; + return ptr; + } + // Fall back to heap allocation if arena capacity is exceeded. + LOG(WARNING) << "BrotliArena capacity exceeded (requested " << size + << " bytes at offset " << self->offset_ << " of " + << self->capacity_ << "), falling back to new."; + self->fallback_allocs_.emplace_back(new uint8_t[size]); + return self->fallback_allocs_.back().get(); + } + + static void Free(void* opaque, void* address) { + if (!address || !opaque) { + return; + } + auto* self = static_cast(opaque); + auto addr_val = reinterpret_cast(address); + auto buf_start = reinterpret_cast(self->buf_.get()); + if (self->buf_ && addr_val >= buf_start + kAlignment && + addr_val <= buf_start + self->offset_) { + size_t alloc_offset = addr_val - buf_start; + size_t payload_size = *reinterpret_cast( + self->buf_.get() + (alloc_offset - kAlignment)); + if (alloc_offset + payload_size == self->offset_) { + self->offset_ = alloc_offset - kAlignment; + } + return; + } + for (auto it = self->fallback_allocs_.rbegin(); + it != self->fallback_allocs_.rend(); ++it) { + if (it->get() == address) { + self->fallback_allocs_.erase(std::next(it).base()); + return; + } + } + } + + void Reset() { + offset_ = 0; + fallback_allocs_.clear(); + } + + size_t offset() const { return offset_; } + size_t capacity() const { return capacity_; } + + private: + size_t capacity_; + size_t offset_; + std::unique_ptr buf_; + std::vector> fallback_allocs_; +}; + +} // namespace brotli + +#endif // BROTLI_BROTLI_ARENA_H_ diff --git a/brotli/shared_brotli_encoder.h b/brotli/shared_brotli_encoder.h index 3ea8f85e..e77d48ee 100644 --- a/brotli/shared_brotli_encoder.h +++ b/brotli/shared_brotli_encoder.h @@ -2,10 +2,12 @@ #define BROTLI_SHARED_BROTLI_ENCODER_H_ #include +#include #include "absl/log/log.h" #include "absl/strings/string_view.h" #include "absl/types/span.h" +#include "brotli/brotli_arena.h" #include "brotli/encode.h" namespace brotli { @@ -20,19 +22,25 @@ typedef std::unique_ptr data) { + static DictionaryPointer CreateDictionary(absl::Span data, + BrotliArena* arena = nullptr) { + brotli_alloc_func alloc_func = arena ? &BrotliArena::Alloc : nullptr; + brotli_free_func free_func = arena ? &BrotliArena::Free : nullptr; return DictionaryPointer( BrotliEncoderPrepareDictionary( BROTLI_SHARED_DICTIONARY_RAW, data.size(), data.data(), - BROTLI_MAX_QUALITY, nullptr, nullptr, nullptr), + BROTLI_MAX_QUALITY, alloc_func, free_func, arena), &BrotliEncoderDestroyPreparedDictionary); } static EncoderStatePointer CreateEncoder( unsigned quality, size_t font_size, unsigned stream_offset, - const BrotliEncoderPreparedDictionary* dictionary) { + const BrotliEncoderPreparedDictionary* dictionary, + BrotliArena* arena = nullptr) { + brotli_alloc_func alloc_func = arena ? &BrotliArena::Alloc : nullptr; + brotli_free_func free_func = arena ? &BrotliArena::Free : nullptr; EncoderStatePointer state = EncoderStatePointer( - BrotliEncoderCreateInstance(nullptr, nullptr, nullptr), + BrotliEncoderCreateInstance(alloc_func, free_func, arena), &BrotliEncoderDestroyInstance); if (!BrotliEncoderSetParameter(state.get(), BROTLI_PARAM_QUALITY, diff --git a/ift/common/BUILD b/ift/common/BUILD index 9191cf51..a1846302 100644 --- a/ift/common/BUILD +++ b/ift/common/BUILD @@ -174,10 +174,12 @@ cc_test( ":common", ":mocks", ":test_font_loader", + "//brotli:shared_brotli_encoder", "@abseil-cpp//absl/container:btree", "@abseil-cpp//absl/container:flat_hash_map", "@abseil-cpp//absl/container:flat_hash_set", "@abseil-cpp//absl/hash:hash_testing", + "@abseil-cpp//absl/log", "@abseil-cpp//absl/status", "@abseil-cpp//absl/strings", "@abseil-cpp//absl/types:span", diff --git a/ift/common/brotli_binary_diff.cc b/ift/common/brotli_binary_diff.cc index e1bb1417..2e7c47e1 100644 --- a/ift/common/brotli_binary_diff.cc +++ b/ift/common/brotli_binary_diff.cc @@ -1,5 +1,6 @@ #include "ift/common/brotli_binary_diff.h" +#include #include #include "absl/log/log.h" @@ -39,11 +40,25 @@ Status BrotliBinaryDiff::Diff(const FontData& font_base, Status BrotliBinaryDiff::Diff(const FontData& font_base, string_view data, unsigned stream_offset, bool is_last, std::vector& sink) const { + + total_calls_++; + total_input_bytes_ += data.size(); + total_dict_bytes_ += font_base.size(); + auto start = std::chrono::high_resolution_clock::now(); + + // Reset the arena on scope exit, ensuring any allocated state and dictionary + // are cleaned up first before offset is reset. + struct ArenaResetGuard { + brotli::BrotliArena& arena; + ~ArenaResetGuard() { arena.Reset(); } + } reset_guard{arena_}; + // There's a decent amount of overhead in creating a dictionary, even if it's // completely empty. So don't set a dictionary unless it's non-empty. DictionaryPointer dictionary(nullptr, nullptr); if (font_base.size() > 0) { - dictionary = SharedBrotliEncoder::CreateDictionary(font_base.span()); + dictionary = + SharedBrotliEncoder::CreateDictionary(font_base.span(), &arena_); if (!dictionary) { return absl::InternalError("Failed to create the shared dictionary."); } @@ -52,7 +67,7 @@ Status BrotliBinaryDiff::Diff(const FontData& font_base, string_view data, // Don't give the encoder an estimated size if this is not all the data. unsigned data_size = !stream_offset && is_last ? data.size() : 0; EncoderStatePointer state = SharedBrotliEncoder::CreateEncoder( - quality_, data_size, stream_offset, dictionary.get()); + quality_, data_size, stream_offset, dictionary.get(), &arena_); if (!state) { return absl::InternalError("Failed to create the encoder."); } @@ -63,6 +78,17 @@ Status BrotliBinaryDiff::Diff(const FontData& font_base, string_view data, return absl::InternalError("Failed to encode brotli binary patch."); } + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = end - start; + total_time_ += elapsed.count(); + + if (total_calls_ % 1000 == 0) { + VLOG(1) << "BrotliBinaryDiff(q" << quality_ << "): " << total_calls_ << " ops, " + << (total_input_bytes_ / total_calls_) << " bytes/call, " + << (total_dict_bytes_ / total_calls_) << " dict bytes/call, " + << (total_time_ / (double) total_calls_) << " ms/call."; + } + return absl::OkStatus(); } diff --git a/ift/common/brotli_binary_diff.h b/ift/common/brotli_binary_diff.h index ad990e68..f8dd0ed9 100644 --- a/ift/common/brotli_binary_diff.h +++ b/ift/common/brotli_binary_diff.h @@ -4,6 +4,7 @@ #include #include "absl/status/status.h" +#include "brotli/brotli_arena.h" #include "ift/common/binary_diff.h" #include "ift/common/font_data.h" @@ -24,8 +25,15 @@ class BrotliBinaryDiff : public BinaryDiff { unsigned stream_offset, bool is_last, std::vector& sink) const; + const brotli::BrotliArena& arena() const { return arena_; } + private: - unsigned quality_; + const unsigned quality_; + mutable brotli::BrotliArena arena_; + mutable uint64_t total_input_bytes_ = 0; + mutable uint64_t total_dict_bytes_ = 0; + mutable uint64_t total_calls_ = 0; + mutable double total_time_ = 0; }; } // namespace ift::common diff --git a/ift/common/brotli_patching_test.cc b/ift/common/brotli_patching_test.cc index e785a38a..2f00f899 100644 --- a/ift/common/brotli_patching_test.cc +++ b/ift/common/brotli_patching_test.cc @@ -1,5 +1,6 @@ #include "absl/status/status.h" #include "absl/types/span.h" +#include "brotli/brotli_arena.h" #include "gtest/gtest.h" #include "ift/common/brotli_binary_diff.h" #include "ift/common/brotli_binary_patch.h" @@ -124,4 +125,90 @@ TEST_F(BrotliPatchingTest, DiffAndPatchWithSmallerDerived) { EXPECT_EQ(Span(patched), Span(subset_a_)); } +TEST(BrotliArenaTest, BumpAllocationAndAlignment) { + brotli::BrotliArena arena(1024); + EXPECT_EQ(arena.offset(), 0); + EXPECT_EQ(arena.capacity(), 1024); + + constexpr size_t kAlign = brotli::BrotliArena::kAlignment; + + void* p1 = brotli::BrotliArena::Alloc(&arena, 10); + ASSERT_NE(p1, nullptr); + EXPECT_EQ(reinterpret_cast(p1) % kAlign, 0); + // Header + aligned payload + size_t expected_p1_offset = kAlign + ((10 + kAlign - 1) & ~(kAlign - 1)); + EXPECT_EQ(arena.offset(), expected_p1_offset); + + void* p2 = brotli::BrotliArena::Alloc(&arena, 24); + ASSERT_NE(p2, nullptr); + EXPECT_EQ(reinterpret_cast(p2) % kAlign, 0); + size_t expected_p2_offset = + expected_p1_offset + kAlign + ((24 + kAlign - 1) & ~(kAlign - 1)); + EXPECT_EQ(arena.offset(), expected_p2_offset); + + // LIFO free p2 then p1 + brotli::BrotliArena::Free(&arena, p2); + EXPECT_EQ(arena.offset(), expected_p1_offset); + + brotli::BrotliArena::Free(&arena, p1); + EXPECT_EQ(arena.offset(), 0); +} + +TEST(BrotliArenaTest, ResetAndReallocation) { + brotli::BrotliArena arena(1024); + void* p1 = brotli::BrotliArena::Alloc(&arena, 64); + EXPECT_GT(arena.offset(), 0); + + arena.Reset(); + EXPECT_EQ(arena.offset(), 0); + + void* p2 = brotli::BrotliArena::Alloc(&arena, 64); + EXPECT_EQ(p1, p2); // Reuses the exact same backing memory address + EXPECT_GT(arena.offset(), 0); + + arena.Reset(); + EXPECT_EQ(arena.offset(), 0); +} + +TEST(BrotliArenaTest, OversizedAllocationFallsBackToMalloc) { + brotli::BrotliArena arena(256); + // Allocate more than capacity + void* p = brotli::BrotliArena::Alloc(&arena, 1024); + ASSERT_NE(p, nullptr); + EXPECT_EQ(arena.offset(), 0); // Arena was bypassed + + // Freeing fallback allocation does not corrupt arena + brotli::BrotliArena::Free(&arena, p); + EXPECT_EQ(arena.offset(), 0); +} + +TEST_F(BrotliPatchingTest, DiffReusesArenaAcrossMultipleCalls) { + EXPECT_EQ(diff_->arena().offset(), 0); + + FontData patch1; + EXPECT_EQ(diff_->Diff(subset_a_, subset_b_, &patch1), absl::OkStatus()); + EXPECT_EQ(diff_->arena().offset(), 0); // Cleaned up after call + + FontData patch2; + EXPECT_EQ(diff_->Diff(subset_a_, subset_b_, &patch2), absl::OkStatus()); + EXPECT_EQ(diff_->arena().offset(), 0); // Cleaned up after call + + // Output must be bit-for-bit identical across calls + EXPECT_EQ(Span(patch1), Span(patch2)); + + // Also test with quality 11 (uses H10 binary tree hasher) + BrotliBinaryDiff diff11(11); + EXPECT_EQ(diff11.arena().offset(), 0); + + FontData patch11_1; + EXPECT_EQ(diff11.Diff(subset_a_, subset_b_, &patch11_1), absl::OkStatus()); + EXPECT_EQ(diff11.arena().offset(), 0); + + FontData patch11_2; + EXPECT_EQ(diff11.Diff(subset_a_, subset_b_, &patch11_2), absl::OkStatus()); + EXPECT_EQ(diff11.arena().offset(), 0); + + EXPECT_EQ(Span(patch11_1), Span(patch11_2)); +} + } // namespace ift::common diff --git a/ift/encoder/candidate_merge.cc b/ift/encoder/candidate_merge.cc index 24d2b09e..70118b61 100644 --- a/ift/encoder/candidate_merge.cc +++ b/ift/encoder/candidate_merge.cc @@ -35,7 +35,6 @@ using absl::flat_hash_map; using absl::flat_hash_set; using absl::Status; using absl::StatusOr; -using ift::GlyphKeyedDiff; using ift::common::CodepointSet; using ift::common::CompatId; using ift::common::FontData; diff --git a/ift/encoder/closure_glyph_segmenter.cc b/ift/encoder/closure_glyph_segmenter.cc index e574143d..41fde2b3 100644 --- a/ift/encoder/closure_glyph_segmenter.cc +++ b/ift/encoder/closure_glyph_segmenter.cc @@ -55,7 +55,6 @@ using absl::Span; using absl::Status; using absl::StatusOr; using absl::StrCat; -using ift::GlyphKeyedDiff; using ift::common::CodepointSet; using ift::common::CompatId; using ift::common::FontData; diff --git a/ift/encoder/compiler.cc b/ift/encoder/compiler.cc index f7cf3c89..a9e82a01 100644 --- a/ift/encoder/compiler.cc +++ b/ift/encoder/compiler.cc @@ -715,9 +715,11 @@ TableKeyedDiff* Compiler::GetTableKeyedDifferFor(ProcessingContext& context, // If only table keyed patches are used we can diff the whole font. if (exclude_ift) { return new TableKeyedDiff(compat_id, {"IFT ", "IFTX"}, - &context.table_diff_cache_); + &context.table_diff_cache_, + &context.binary_diff_); } else { - return new TableKeyedDiff(compat_id, &context.table_diff_cache_); + return new TableKeyedDiff(compat_id, &context.table_diff_cache_, + &context.binary_diff_); } } @@ -726,13 +728,14 @@ TableKeyedDiff* Compiler::GetTableKeyedDifferFor(ProcessingContext& context, // tables with variations will need to be replaced (gvar/CFF2), all other // glyph data tables are excluded. if (exclude_ift) { - return new TableKeyedDiff(compat_id, - {"IFT ", "IFTX", "glyf", "loca", "CFF "}, - {"gvar", "CFF2"}, &context.table_diff_cache_); + return new TableKeyedDiff( + compat_id, {"IFT ", "IFTX", "glyf", "loca", "CFF "}, {"gvar", "CFF2"}, + &context.table_diff_cache_, &context.binary_diff_); } else { return new TableKeyedDiff(compat_id, {"glyf", "loca", "CFF "}, {"IFTX", "gvar", "CFF2"}, - &context.table_diff_cache_); + &context.table_diff_cache_, + &context.binary_diff_); } } @@ -741,11 +744,11 @@ TableKeyedDiff* Compiler::GetTableKeyedDifferFor(ProcessingContext& context, if (exclude_ift) { return new TableKeyedDiff( compat_id, {"IFT ", "IFTX", "glyf", "loca", "gvar", "CFF ", "CFF2"}, {}, - &context.table_diff_cache_); + &context.table_diff_cache_, &context.binary_diff_); } else { - return new TableKeyedDiff(compat_id, - {"IFTX", "glyf", "loca", "gvar", "CFF ", "CFF2"}, - {}, &context.table_diff_cache_); + return new TableKeyedDiff( + compat_id, {"IFTX", "glyf", "loca", "gvar", "CFF ", "CFF2"}, {}, + &context.table_diff_cache_, &context.binary_diff_); } } diff --git a/ift/encoder/compiler.h b/ift/encoder/compiler.h index 2a7e9257..47c1513f 100644 --- a/ift/encoder/compiler.h +++ b/ift/encoder/compiler.h @@ -11,6 +11,7 @@ #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "hb-subset.h" +#include "ift/common/brotli_binary_diff.h" #include "ift/common/compat_id.h" #include "ift/common/font_data.h" #include "ift/common/int_set.h" @@ -446,6 +447,7 @@ class Compiler { absl::flat_hash_map estimated_patch_sizes_; ift::common::IntSet built_table_keyed_patches_; ift::TableDiffCache table_diff_cache_; + ift::common::BrotliBinaryDiff binary_diff_{11}; SubsetDefinition init_subset_; diff --git a/ift/table_keyed_diff.cc b/ift/table_keyed_diff.cc index dd4fd39f..21e8b480 100644 --- a/ift/table_keyed_diff.cc +++ b/ift/table_keyed_diff.cc @@ -59,15 +59,17 @@ Status TableKeyedDiff::Diff(const FontData& font_base, } FontData table_patch; + const auto& differ = + binary_diff_ != nullptr ? *binary_diff_ : default_binary_diff_; if (cache_ == nullptr) { - TRYV(binary_diff_.Diff(base_table, derived_table, &table_patch)); + TRYV(differ.Diff(base_table, derived_table, &table_patch)); } else { TableDiffKey key(base_table, derived_table); std::optional& cache_value = (*cache_)[key]; if (cache_value.has_value()) { table_patch.shallow_copy(*cache_value); } else { - TRYV(binary_diff_.Diff(base_table, derived_table, &table_patch)); + TRYV(differ.Diff(base_table, derived_table, &table_patch)); FontData cached_patch; cached_patch.shallow_copy(table_patch); cache_value = std::move(cached_patch); diff --git a/ift/table_keyed_diff.h b/ift/table_keyed_diff.h index 407de8a7..d78ce274 100644 --- a/ift/table_keyed_diff.h +++ b/ift/table_keyed_diff.h @@ -57,14 +57,20 @@ using TableDiffCache = /* Creates a per table brotli binary diff of two fonts. */ class TableKeyedDiff : public ift::common::BinaryDiff { public: - explicit TableKeyedDiff(ift::common::CompatId base_compat_id, - TableDiffCache* cache = nullptr) - : binary_diff_(11), base_compat_id_(base_compat_id), cache_(cache) {} + explicit TableKeyedDiff( + ift::common::CompatId base_compat_id, TableDiffCache* cache = nullptr, + const ift::common::BrotliBinaryDiff* binary_diff = nullptr) + : default_binary_diff_(11), + binary_diff_(binary_diff), + base_compat_id_(base_compat_id), + cache_(cache) {} TableKeyedDiff(ift::common::CompatId base_compat_id, std::initializer_list excluded_tags, - TableDiffCache* cache = nullptr) - : binary_diff_(11), + TableDiffCache* cache = nullptr, + const ift::common::BrotliBinaryDiff* binary_diff = nullptr) + : default_binary_diff_(11), + binary_diff_(binary_diff), base_compat_id_(base_compat_id), excluded_tags_(), replaced_tags_(), @@ -76,8 +82,10 @@ class TableKeyedDiff : public ift::common::BinaryDiff { TableKeyedDiff(ift::common::CompatId base_compat_id, absl::btree_set excluded_tags, absl::btree_set replaced_tags, - TableDiffCache* cache = nullptr) - : binary_diff_(11), + TableDiffCache* cache = nullptr, + const ift::common::BrotliBinaryDiff* binary_diff = nullptr) + : default_binary_diff_(11), + binary_diff_(binary_diff), base_compat_id_(base_compat_id), excluded_tags_(std::move(excluded_tags)), replaced_tags_(std::move(replaced_tags)), @@ -97,7 +105,8 @@ class TableKeyedDiff : public ift::common::BinaryDiff { const absl::flat_hash_set& before, const absl::flat_hash_set& after) const; - ift::common::BrotliBinaryDiff binary_diff_; + ift::common::BrotliBinaryDiff default_binary_diff_; + const ift::common::BrotliBinaryDiff* binary_diff_ = nullptr; ift::common::CompatId base_compat_id_; absl::btree_set excluded_tags_; absl::btree_set replaced_tags_; diff --git a/ift/table_keyed_diff_test.cc b/ift/table_keyed_diff_test.cc index 26cb0c2d..4997c1d8 100644 --- a/ift/table_keyed_diff_test.cc +++ b/ift/table_keyed_diff_test.cc @@ -253,6 +253,32 @@ TEST_F(TableKeyedDiffTest, CachedDiff) { ASSERT_EQ(patch1.string(), patch2.string()); } +TEST_F(TableKeyedDiffTest, SharedBrotliBinaryDiff) { + FontData before = FontHelper::BuildFont({ + {tag1, "foo"}, + {tag2, "bar"}, + }); + + FontData after = FontHelper::BuildFont({ + {tag1, "fooo"}, + {tag2, "baar"}, + }); + + BrotliBinaryDiff shared_brotli_diff(11); + TableKeyedDiff differ1(CompatId(1, 2, 3, 4), nullptr, &shared_brotli_diff); + TableKeyedDiff differ2(CompatId(1, 2, 3, 4), nullptr, &shared_brotli_diff); + + FontData patch1; + auto sc1 = differ1.Diff(before, after, &patch1); + ASSERT_TRUE(sc1.ok()) << sc1; + + FontData patch2; + auto sc2 = differ2.Diff(before, after, &patch2); + ASSERT_TRUE(sc2.ok()) << sc2; + + ASSERT_EQ(patch1.string(), patch2.string()); +} + /* TEST_F(TableKeyedDiffTest, FilteredDiff) { FontData before = FontHelper::BuildFont({