当前位置:网站首页>Static distribution and dynamic distribution in trust
Static distribution and dynamic distribution in trust
2022-07-23 17:47: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)
边栏推荐
- Summary of stock historical data download interface (dynamic update)
- Solutions to sap Hana database backup failure
- rust猜数字游戏
- @Will multiple bean instances be created by multiple method calls of bean annotations
- tp&smarty使用日记
- [flask advanced] deeply understand the endpoint of flask routing from the source code
- Tampermonkey究竟有什么用?
- nVisual综合布线管理软件与网管软件的区别
- The larger the convolution kernel, the stronger the performance? An interpretation of replknet model
- Aike AI frontier promotion (7.23)
猜你喜欢

leetcode刷题:动态规划04(不同路径)

rust统计文件中单词出现的次数

基于C语言开发的控制台计算器

sns_sensor_instance_api

基于scrapy的电商平台数据爬取与展示

Differences between nvisual generic cabling management software and network management software

Log slimming operation: from 5g to 1g!
不掌握这些坑,你敢用BigDecimal吗?

File management system based on OpenPGP

ContextLoaderListener vs DispatcherServlet
随机推荐
JS tool CECP
深入理解机械系统的模态与振动
Virtual machine network connection mode
LeetCode_455_分发饼干
phpstrom快捷键
Deeply understand the mode and vibration of mechanical system
59. General knowledge of lightning safety
一台Linux机器上启动多个redis实例
Debug:形参带有Const修饰符号需要注意的地方
topology.yaml中指定端口号
工业物联网中的时序数据
[flask advanced] deeply understand the endpoint of flask routing from the source code
New opportunities for cultural tourism in the era of digital intelligence? China Mobile Migu creates "the first island in the yuan universe"
为啥一问 JVM 就 懵B ???
网页返回更新
SAP HANA数据库备份失败解决办法
[ pytorch ] 基本使用丨7. GPU分配丨
Use Preparedstatement to select and display recorded JDBC programs
Encapsulate the general connection and query of the project with pymysql
Create a flow using flow builder in kotlin