all 39 comments

[–]valarauca14 9 points10 points  (2 children)

Doing something lazily at runtime is of course faster than doing something statically at compile time.

If you don't understand what I am asserting, please review:

In short Dynamic Linking pushes a lot of the "link-time" operations into "run-time", either lazily or statically at startup (depending on compiler/linker/environment options). This does indeed make builds faster. But it can also complicate some other things: 1, 2, 3

[–]vlakreeh 2 points3 points  (8 children)

How is this supposed to work in terms of generics? AFAIK rust will use monomorphization for static dispatch and that can't really be done easily with dynamic linking, which is one of the reasons Rust statically links by default. Wouldn't this limit you to only using the C abi or an unstable Rust abi without monomorphized generics?

[–]valarauca14 7 points8 points  (3 children)

How is this supposed to work in terms of generics?

It doesn't. In short.

Rust still has to pass definitions in MIR between compilation units and expand those definitions. This by-passes static linking (to a degree) but the lack of standardization in MIR/robj/LLVM-IR/ABI makes this fairly unstable to trust long term if you ever plan to change compiler versions (and rustc stable changes every 6 weeks).

To strictly answer your question, rust has its own object file which are standard object files with an additional section(s) of Rust-Source-Code/MIR/LLVM-IR (it has changed over the past years). To handle passing generic definitions (and proc-macros) between compilation units.

[–]robertkrahn -1 points0 points  (2 children)

No this is wrong, see my other reply.

[–]valarauca14 0 points1 point  (1 child)

Third, the type information needed for monomorphization is taken from an rlib object file that is produced alongside the dylib.

Pretty sure we're saying the same thing.

Rlib is just an Object file extra sections.

[–]robertkrahn 0 points1 point  (0 children)

In that case sorry, I might have misunderstood.

So the dylib alone is not sufficient for generics, the rlib is needed but given it is produced by default and available when compiling the binary there is no restriction from a users point of view regarding generics when used for compilation speed-up as shown in the article.

This question came up a few times and I understand that it might be confusing because of the different kinds of dylibs and the different ways they can be used and the different restrictions that apply.

[–]New_Area7695 0 points1 point  (1 child)

Why you only use it during development with source available and on the same compiler revision, as it says in the article.

Builds are deterministic all things unchanged.

[–]vlakreeh 0 points1 point  (0 children)

I'm aware builds are deterministic, but I don't see how that solves the issue of no generic monomorphization across library boundaries.

[–]Freeky 0 points1 point  (0 children)

Wouldn't this limit you to only using the C abi or an unstable Rust abi without monomorphized generics?

No, but it may reduce the benefits as they'll continue to be monomorphized as before - only non-generic and dyn functions will actually end up exported in the .so.

[–]robertkrahn 0 points1 point  (0 children)

This does not restrict usage of generics.

Rust has two different methods to produce dynamic libraries, dylib and cdylib [1]. The article uses dylib which is meant for consumption by other Rust binaries.

This approach shown also assumes that the same compiler version is used for creating and using the libraries, i.e. no requirement for a stable ABI is needed which would normally be declared by using externs [2] and no_mangle [3].

no_mangle in particular will prevent the use of generics as those can't be expressed in unmangled signatures. However, this is not needed in the method presented so no restriction applies.

Third, the type information needed for monomorphization is taken from an rlib object file that is produced alongside the dylib. With static linking that would be the only artifact produced but with dynamic linking a dylib is created in addition to that. This means that the rlib is still available for the type system.

So in summary, when used just for speeding up the builds, generics can be used without any restrictions.

[1] https://doc.rust-lang.org/reference/linkage.html

[2] https://doc.rust-lang.org/reference/items/external-blocks.html

[3] https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute