Skip to content

Commit

Permalink
Omit source code info from well known types descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhickman committed Apr 27, 2023
1 parent 3e840e8 commit ef98c4c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Monomorphised some code in `DescriptorPool::add_*` methods to reduce binary size. Thanks to [@sjrs] for [#40] and [#41].
- Source code info is now omitted from the built-in descriptors for well-known types. This reduces the binary size by around 100KB.

## [0.11.3] - 2023-04-11

Expand Down
2 changes: 2 additions & 0 deletions prost-reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ once_cell = "1.13.0"
proptest = "1.0.0"
prost-build = "0.11.0"
prost-reflect-build = { path = "../prost-reflect-build" }
protox = "0.3.1"
serde_json = "1.0.82"
serde_yaml = "0.9.16"
similar-asserts = { version = "1.4.2", features = ["serde"] }

[package.metadata.release]
tag-name = "{{version}}"
Expand Down
61 changes: 60 additions & 1 deletion prost-reflect/src/reflect/wkt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
use crate::{DescriptorPool, MessageDescriptor, ReflectMessage};

pub(crate) const WELL_KNOWN_TYPES_BYTES: &[u8] = include_bytes!("../well_known_types.bin");
pub(crate) const WELL_KNOWN_TYPES_BYTES: &[u8] = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/well_known_types.bin"
));

#[test]

fn generate_well_known_types_bin() {
use std::fs;

use prost::Message;
use prost_types::FileDescriptorSet;
use protox::{file::GoogleFileResolver, Compiler};

// protox can output a FileDescriptorSet directly, but by going through bytes, this should still work
// when upgrading to a newer prost-types version.
let expected_bytes = Compiler::with_file_resolver(GoogleFileResolver::new())
.include_source_info(false)
.open_files([
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/descriptor.proto",
"google/protobuf/duration.proto",
"google/protobuf/empty.proto",
"google/protobuf/field_mask.proto",
"google/protobuf/source_context.proto",
"google/protobuf/struct.proto",
"google/protobuf/timestamp.proto",
"google/protobuf/type.proto",
"google/protobuf/wrappers.proto",
"google/protobuf/compiler/plugin.proto",
])
.unwrap()
.encode_file_descriptor_set();

let actual = FileDescriptorSet::decode(WELL_KNOWN_TYPES_BYTES).unwrap();
let expected = FileDescriptorSet::decode(expected_bytes.as_ref()).unwrap();

if actual != expected {
fs::write(
concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/well_known_types.expected.bin"
),
expected_bytes,
)
.unwrap();

let actual_str = format!("{:#?}", actual);
let expected_str = format!("{:#?}", expected);

let diff =
similar_asserts::SimpleDiff::from_str(&actual_str, &expected_str, "actual", "expected");

panic!("Found differences in 'well_known_types.bin'.
If this is expected, replace 'well_known_types.bin' with 'well_known_types.actual.bin' to update it
\
differences: {}", diff);
}
}

macro_rules! impl_reflect_message {
($($ty:ty => $name:literal;)*) => {
Expand Down
Binary file modified prost-reflect/src/well_known_types.bin
Binary file not shown.

0 comments on commit ef98c4c

Please sign in to comment.