Skip to content

Introduce MetadataBytes wrapper for compiled-in metadata accessors#4016

Merged
mandlil merged 2 commits into
google:masterfrom
hjanuschka:chromium-metadata-bytes-wrapper
Jun 29, 2026
Merged

Introduce MetadataBytes wrapper for compiled-in metadata accessors#4016
mandlil merged 2 commits into
google:masterfrom
hjanuschka:chromium-metadata-bytes-wrapper

Conversation

@hjanuschka

@hjanuschka hjanuschka commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Introduces a small MetadataBytes RAII wrapper for the compiled-in metadata
buffers returned by the internal metadata.h, short_metadata.h and
alternate_format.h accessors.

The C-style API

int metadata_size();
const void* metadata_get();

is replaced by

MetadataBytes GetMetadata();   // and GetShortMetadata(), GetAlternateFormat()

where MetadataBytes is a move-only handle that holds either:

  1. a non-owning const void* + int size, or
  2. a std::unique_ptr<uint8_t[]> + int size (owning).

The default implementations shipped in this repo continue to wrap the
existing static const unsigned char data[] arrays with a non-owning
instance, so on-disk layout and runtime behaviour are unchanged for
upstream consumers. CppMetadataGenerator (and its tests) are updated to
emit the new API.

Backwards compatibility

These headers are library-internal (not part of the public API).

Related

Chromium CL discussion that motivated this:
https://chromium-review.googlesource.com/c/chromium/src/+/7928952

Replace the C-style `int <type>_size()` / `const void* <type>_get()` pair
in metadata.h, short_metadata.h and alternate_format.h with a single
`MetadataBytes Get<Type>()` accessor that returns a small RAII wrapper.

The default implementations shipped in this repo wrap the existing
static `data[]` arrays in a non-owning MetadataBytes, so the on-disk
layout and runtime behaviour are unchanged for upstream consumers.

The motivating use case is downstream embedders (e.g. Chromium) that
produce the metadata at runtime, for example by decompressing a
brotli-compressed blob on first use to save binary size. With the old
API such embedders had to either leak the decompressed buffer or do
gymnastics to free it after PhoneNumberUtil parsed it. With
MetadataBytes the typical call site

    MetadataBytes bytes = GetMetadata();
    collection->ParseFromArray(bytes.data(), bytes.size());

keeps the buffer alive for exactly as long as ParseFromArray() needs it
and then frees it when the wrapper goes out of scope, simply by having
the downstream-supplied implementation return an owning instance built
from a std::unique_ptr<uint8_t[]>.

CppMetadataGenerator and its tests are updated to emit the new API.
@hjanuschka hjanuschka requested a review from a team as a code owner June 20, 2026 09:34
@tvislavski tvislavski requested a review from katbohm June 25, 2026 09:39

@battre battre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

kkeshava
kkeshava previously approved these changes Jun 26, 2026
@mandlil

mandlil commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Hi @hjanuschka,
It looks like the C++ build is failing with these changes. Could you please take a look and update the PR?

@kkeshava kkeshava self-requested a review June 29, 2026 05:23
The previous commit updated CppMetadataGenerator.java to emit the new
MetadataBytes API but did not rebuild the checked-in
cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar that CMake invokes to
regenerate metadata.{h,cc} at build time. As a result CI regenerated the
headers using the old API and phonenumberutil.cc failed with
'MetadataBytes was not declared in this scope'.

Also reorder the assertions in CppMetadataGeneratorTest.outputHeaderFile
to match the actual emit order (the metadata_bytes.h include is emitted
before the namespace openings, not after).
@hjanuschka

Copy link
Copy Markdown
Contributor Author

@mandlil tests pass now!

@katbohm katbohm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mandlil mandlil merged commit eb796e8 into google:master Jun 29, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants