当前位置:网站首页>Static distribution and dynamic distribution in trust
Static distribution and dynamic distribution in trust
2022-07-22 02:14:00 【pilaf1990】
rust The paradigm in belongs to static polymorphism , It is compile time polymorphism , No run-time performance loss . At compile time , Whether it's paradigm enumeration 、 Canonical functions and canonical structures , Will be singled out (Monomorphization).
Take the following generic function as an example :
fn main() {
let foo = Foo;
static_dispatch(&foo);
dynamic_dispatch(&foo);
}
#[derive(Debug)]
struct Foo;
trait Bar {
fn baz(&self);
}
impl Bar for Foo {
fn baz(&self) {
println!("{:?}", self);
}
}
// Static distribution
fn static_dispatch<T>(t: &T) where T: Bar {
t.baz();
}
// Dynamic distribution
fn dynamic_dispatch(t: &dyn Bar) {
t.baz();
}
static_dispatch The generic function is compiled , Will find Bar trait All implementations of , And generate an implementation for each of them , For example, in the above example , Only Foo A structure realizes Bar trait, In fact, at compile time static_dispatch Will generate
fn static_dispatch(t: &Foo){
t.baz();
}
This method , If there are others Bar trait The implementation will also generate its corresponding concrete methods . That is, when compiling, each Bar trait All implementations of generate a specific method , There is no overhead when running .
Put the above method into https://play.rust-lang.org/ Choose ASM:
You can see that the generated compiled code is indeed singlet :
and dynamic_dispatch The method input parameter of is t:&dyn Bar
, It is a kind of dynamic distribution . Parameters t Type of dimension &Bar yes trait object .trait It is also an abstract rather than a concrete type , But its type size cannot be determined at compile time , therefore trait Objects must use fat pointers . You can use the reference operator & or Box<T> To make a trait object .
trait Object is equivalent to the following structure :
pub struct TraitObject{
pub data: *mut(),
pub vtable: *mut(),
}
TraitObject Include two pointers :data Pointers and vtable The pointer . With impl MyTrait for T
For example ,data Pointer to trait Object saves type data T,vtable Pointer to include T Realized MyTrait Of vtable(virtual table, The name comes from C++, So it can be called virtual table ). The essence of virtual table is a structure , Contains destructors , size , Information such as alignment and method .
At compile time , The compiler only knows TraitObject Contains information about pointers , And the size of the pointer is also determined ( Because it needs to be allocated on the stack , The size must be certain ), But I don't know which method to call . During program operation , When there is trait_object.method() Method is called ,TraitObject Will find the correct function pointer from the virtual table according to the virtual table pointer , Then make dynamic calls (Java The runtime polymorphism of is similar ). It's also going to be trait The reason why objects become dynamic distribution .
Reference material :
1.《Rust The tao of programming 》( Written by Zhang Hantong P61 P71)
边栏推荐
猜你喜欢
随机推荐
3564. Date category
The architecture mode is excerpted from "happy when you smell defects" (this book can be downloaded for free)
ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headlines
电脑是怎样上网的 (一) 消息产生 DNS
【无标题】
H5网站接入微信支付(H5支付+JSAPI支付)
奇葩!一公司面试题竟问如厕习惯、吃饭时长、入睡时间等
AtCoder Beginner Contest 260 - C, D, E
Nacos配置中心中配置文件的创建、微服务读取nacos配置中心
Numpy04_线性代数(未写完)
电脑是怎样上网的 (二) 从网线到网络设备
过时 2 天后,微软放弃“封禁”商业开源!
Hcip day 9 notes
单调栈与单调队列(线性复杂度优化)
3511. Water pouring problem
马斯克把大脑上传云端?周鸿祎:还得要用人脑安全卫士杀个毒
JS object: check whether the attribute exists
电脑是怎样上网的 (三) 报文头封装和接入网与网络运营商
rust中的静态分发和动态分发
开发期质量 节选自《闻缺陷则喜》(此书可免费下载)