Rustのderiveはしばしばinlineを暗示する
Rust's derive often implies inline (yossarian.net)
要約
Rustの`#[derive(...)]`マクロは、デバッグ、表示、クローンなどのコアトレイトの実装を生成する際に、デフォルトで`#[inline]`属性を付与することが多いです。これは通常、これらの派生実装が小さく、インライン化の恩恵を受けるため望ましいですが、エラー階層のような複雑な構造では、過剰なインライン化がバイナリサイズを大幅に増加させる可能性があります。この問題に対処するために、`#[inline(never)]`を持つカスタムマクロを使用するアプローチが紹介されています。
全文翻訳
Rustのderiveはしばしばinlineを暗示する
Rustにおいて、Debug、Display、Cloneといったコアなトレイトを実装する最も一般的な方法の一つは、それらを#[derive(...)]することです。例:
#[derive(Debug)]
struct Widgets {
foo: u32,
bar: usize,
}
私が最近まで知らなかったのは、Rustが現在、これらの派生実装の一部として#[inline]を発行していることです。これは保証されているわけではないようですが、リファレンスの例で暗示されており、マクロを展開すると確認できます。
上記の例を使用すると、プレイグラウンドで #[derive(Debug)] を展開した結果は以下のようになります。
struct Widgets {
foo: u32,
bar: usize,
}
#[automatically_derived]
impl ::core::fmt::Debug for Widgets {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Widgets",
"foo", &self.foo, "bar", &&self.bar)
}
}
これはほぼ常に望ましいことです。#[inline]は単なるヒントであり、典型的な派生Debug、Cloneなどの実装は、(しばしば些細なものであるため)インライン化されるとメリットがあります。
しかし、常にそうとは限りません!以下のようなエラー階層を想像してみてください[1]。
#[derive(Debug)]
struct ErrorA {
lots: String,
of: String,
chunky: String,
fields: String,
within: String,
this: String,
r#type: String,
}
#[derive(Debug)]
struct ErrorB {
inner: ErrorA,
}
#[derive(Debug)]
struct ErrorC {
inner: ErrorB,
}
#[derive(Debug)]
enum Errors {
A(ErrorA),
B(ErrorB),
C(ErrorC),
}
これは以下のようなコードを生成します。
struct ErrorA {
lots: String,
of: String,
chunky: String,
fields: String,
within: String,
this: String,
r#type: String,
}
#[automatically_derived]
impl ::core::fmt::Debug for ErrorA {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["lots", "of", "chunky", "fields", "within", "this", "type"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.lots, &self.of, &self.chunky, &self.fields, &self.within,
&self.this, &&self.r#type];
::core::fmt::Formatter::debug_struct_fields_finish(f, "ErrorA", names,
values)
}
}
#[automatically_derived]
impl ::core::default::Default for ErrorA {
#[inline]
fn default() -> Self {
Self {
lots: ::core::default::Default::default(),
of: ::core::default::Default::default(),
chunky: ::core::default::Default::default(),
fields: ::core::default::Default::default(),
within: ::core::default::Default::default(),
this: ::core::default::Default::default(),
r#type: ::core::default::Default::default(),
}
}
}
struct ErrorB {
inner: ErrorA,
}
#[automatically_derived]
impl ::core::fmt::Debug for ErrorB {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "ErrorB",
"inner", &&self.inner)
}
}
struct ErrorC {
inner: ErrorB,
}
#[automatically_derived]
impl ::core::fmt::Debug for ErrorC {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "ErrorC",
"inner", &&self.inner)
}
}
enum Errors {
A(ErrorA),
B(ErrorB),
C(ErrorC),
}
#[automatically_derived]
impl ::core::fmt::Debug for Errors {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::A(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "A",
&__self_0),
Self::B(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "B",
&__self_0),
Self::C(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "C",
&__self_0),
}
}
}
これは、ErrorsのDebug実装の各呼び出しでインライン化される可能性のある大量のコードであり、例えばデバッグログやトレースログで繰り返し発生する可能性があります。
実際、Rustバイナリの総サイズのかなりの部分を占めることがあります。uvのバイナリサイズを約160KB削減できたのは、rustcが特定のDebug実装のインライン化を妨げたためです[2]。
これは私にとって二重に驚きでした。サイズコストが急速に積み重なること、そしてrustcがDebug実装のサイズやインライン化される回数に制限を適用していないように見えることです。しかし、多くのプログラムではこれが正しい判断だと推測します!