#[derive(partialeq,eq)是否会增加代码大小?

发布于 2025-02-07 02:24:08 字数 410 浏览 2 评论 0原文

我提交了一个 /code> 因为我需要能够测试生成的故障。此补丁启用eqpartialeq,因此我可以使用assert_eq!()测试故障。

问题是,

我的理解是,生成不必要的性状实现可以增加代码大小。

我的理解是,未使用的实现不会生成更多代码?这两个是正确的?

I submitted a patch to derive-builder because I needed the ability to test failures generated. This patch enabled Eq and PartialEq so I could I test for failures using assert_eq!().

The question asked was,

My understanding is that generating unnecessary trait implementations can increase code size.

It is my understanding that implementations that are not used do not generate more code? Which of these two is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2025-02-14 02:24:08

这是我为测试该理论所做的,我生成了一个简单的二进制文件,

#[derive(Debug)]
struct Foo {
  id: i64
}

fn main() {
  let a = Foo { id: 42 }; => Foo
  println!("Hello, world! [{} {:?}]", a.id, a);
}

然后我进行了同一代一代,但是使用#[derive(debug,partialeq,eq)]。在这方面,我发现两者都产生了相同的哈希。他们是相同的。不具体,我还尝试创建库并使用- Release编译。同样的事情。这次仅使用struct foo(否main)。在这种情况下,我确实观察到了差异,这是细微差别:

  • 在两个运行rlib(Rust库)文件之间的大小不同。
  • rlibar chive。对我来说,它有三个文件,其中一个以cgu.0.rcgu.o.o结尾是lib.rmeta
  • 在存档中的文件中,*。O文件完全相同(哈希)。
  • lib.rmeta文件在库中较大,该文件还衍生eqpartialeq

现在,至于 rmeta文档说这

RMETA文件是自定义二进制格式,其中包含板条箱的元数据。该文件可以通过跳过所有代码生成(与货物检查一样),收集足够的信息以进行文档(与货物文档一样)或用于管道来快速进行项目的快速“检查”。如果使用-emit =元数据CLI选项,则创建此文件。
rmeta文件不支持链接,因为它们不包含编译的对象文件。

,似乎某些东西 变得更大,但是某物是只有用于工具目的。

我尝试了上面的测试,其中有和没有pub。我希望如果生成一个未使用的函数,它至少会导致一个.o文件更大。不过,我无法观察到这一点。

Here is what I did to test this theory, I generated a simple binary,

#[derive(Debug)]
struct Foo {
  id: i64
}

fn main() {
  let a = Foo { id: 42 }; => Foo
  println!("Hello, world! [{} {:?}]", a.id, a);
}

I then did the same generation but with #[derive(Debug, PartialEq, Eq)]. In this, I found both generated the same hash; they were identical. Not content, I also tried creating a library and compiling with --release. Same thing. This time with just struct Foo (no main). In this case, I did observe a difference, here is the nuance:

  • Between the two runs the rlib (rust library) file had a different size.
  • An rlib is an archive. For me it had three files, one of them ended in cgu.0.rcgu.o the other in cgu.1.rcgu.o, and there was one file that was lib.rmeta.
  • Of the files in the archive, the *.o files were exactly the same (hashed).
  • The lib.rmeta file was larger in the library that also derived Eq, and PartialEq.

Now as to the merit of rmeta, the Rust documentation says this,

An rmeta file is custom binary format that contains the metadata for the crate. This file can be used for fast "checks" of a project by skipping all code generation (as is done with cargo check), collecting enough information for documentation (as is done with cargo doc), or for pipelining. This file is created if the --emit=metadata CLI option is used.
rmeta files do not support linking, since they do not contain compiled object files.

So it seems something gets bigger, but that something is ONLY used for tooling purposes.

I tried the above test with the library with and without pub. I would expect if a function was generated that wasn't used it would have at the least resulted in one .o file being larger. I was not able to observe this though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文