tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.92k stars 504 forks source link

prost-build: optimise derived prost::Name #956

Closed mina86 closed 8 months ago

mina86 commented 11 months ago

When deriving prost::Name, prost-bulid generates the following code for full_name and type_url methods:

fn full_name() -> ::prost::alloc::string::String {
    ::prost::alloc::format!(
        "some.package.{0}", Self::NAME
    )
}
fn type_url() -> ::prost::alloc::string::String {
    ::prost::alloc::format!(
        "example.com/{0}", Self::full_name()
    )
}

With those definitions, type_url:

Most of those operations can be done at build time with only a single string allocation being necessary in type_url method.

Change the generated code such that no formatting happens at run time and no temporary strings are allocated in type_url, specifically:

fn full_name() -> ::prost::alloc::string::String {
    "some.package.MessageName".into()
}
fn type_url() -> ::prost::alloc::string::String {
    "example.com/some.package.MessageName"
}

Furthermore, unconditionally derive type_url even if no domain is specified such that default definition (which uses temporary strings and formatting) isn’t used.

mina86 commented 8 months ago

error: package half v2.3.1 cannot be built because it requires rustc 1.70 or newer, while the currently active rustc version is 1.65.0

What about this failure then?

caspermeijn commented 8 months ago

error: package half v2.3.1 cannot be built because it requires rustc 1.70 or newer, while the currently active rustc version is 1.65.0

What about this failure then?

It seems the MSRV is no longer accurate. This PR is trying to fix that: https://github.com/tokio-rs/prost/pull/975

caspermeijn commented 8 months ago

Please rebase to the latest master branch to fix CI