首页 币圈新闻 【Substrasdfste开发教程】18 – Runtime编程优化技巧:缓存多个调用

【Substrasdfste开发教程】18 – Runtime编程优化技巧:缓存多个调用

前几篇作品引见了substrate runtime沙盘编制程序的少许普通常识,囊括沙盘里的几个宏:decl_module! decl_storage! decl_event!的用法比特币价格

这篇作品引见一个runtime编制程序中的优化本领,缓存多个挪用,制止二次挪用爆发的运转本钱比特币行情走势图

copy

rust中有两个罕见的trait:copy和clone什么是比特币

copy全称是std::marker::copy,即使一个典型实行了copy trait,则任何功夫都不妨经过大略的外存正片实行该典型的复制,而不会爆发任何题目比特币暴跌

基础数据典型如数字典型、bool典型、共享滥用南针&今日比特币,都是具备copy trait的典型;

数组典型偷电挖比特币,即使它的元素是copy典型,则该数组是copy典型;

元组典型比特币期货,即使它的每一个元素都是copy典型,则该元组是copy典型,机动实行;

struct和enum什么是比特币,即使它的里面每个元素都是copy典型,可手动实行copy典型;

clone

clone全称是std::clone::clone,clone因变量普遍用来“鉴于语义的复制”操纵比特币行情最新价格

对于box典型(大略的指向堆的南针)今日比特币,clone实行的“深正片”;

对于rc典型(reference counting,援用计数),clone把援用计数值加1比特币期货

copy典型和clone典型

substrate runtime编制程序中的copy典型和clone典型如次所示比特币价格

decl_storage! { trait store for module<t: trait> as storagecache { //copy典型 somecopyvalue get(fn some_copy_value): u32; //clone典型 kingmember get(fn king_member): t::accountid; groupmembers get(fn group_members): vec<t::accountid>; }}

【substrasdfste开拓教程】18 - runtime编制程序优化本领:缓存多个挪用

挪用runtime保存会爆发相映的本钱,开拓职员该当尽管缩小挪用度数如何挖比特币

copy典型

对于copy典型,只须要大略地重用该值即可重用之前的保存挪用,该值在重用时会机动复制比特币矿池

示例代码比特币爆仓是什么意思

fn increase_value_no_cache(origin, some_val: u32) -> dispatchresult { let _ = ensure_signed(origin)?; let original_call = <somecopyvalue>::get(); let some_calculation = original_call.checked_add(some_val).ok_or(\”addition overflowed1\”)?; let unnecessary_call = <somecopyvalue>::get(); let another_calculation = some_calculation.checked_add(unnecessary_call).ok_or(\”addition overflowed2\”)?; <somecopyvalue>::put(another_calculation); let now = <system::module<t>>::block_number(); self::deposit_event(rawevent::inefficientvaluechange(another_calculation, now)); ok(())}

这边挪用了两次<somecopyvalue>::get(),辨别赋值给original_call、unnecessary_call比特币大涨

第二次挪用<somecopyvalue>::get()爆发的unnecessary_call变量是不需要的今日比特币,径直运用original_call即可:

fn increase_value_w_copy(origin, some_val: u32) -> dispatchresult { let _ = ensure_signed(origin)?; let original_call = <somecopyvalue>::get(); let some_calculation = original_call.checked_add(some_val).ok_or(\”addition overflowed1\”)?; let another_calculation = some_calculation.checked_add(original_call).ok_or(\”addition overflowed2\”)?; <somecopyvalue>::put(another_calculation); let now = <system::module<t>>::block_number(); self::deposit_event(rawevent::bettervaluechange(another_calculation, now)); ok(())}

clone典型

即使典型是clone,而不是copy,运用clone()因变量比二次挪用runtime保存更好比特币交易网站

示例代码比特币交易网站

fn swap_king_no_cache(origin) -> dispatchresult {let new_king = ensure_signed(origin)?;let existing_king = <kingmember<t>>::get();ensure!(!self::is_member(&existing_king), \”current king is a member so maintains priority\”);ensure!(self::is_member(&new_king), \”new king is not a member so doesn\’t get priority\”);let old_king = <kingmember<t>>::get();<kingmember<t>>::put(new_king.clone());self::deposit_event(rawevent::inefficientkingswap(old_king, new_king));ok(())}

这边挪用了两次<kingmember<t>>::get(),辨别赋值给existing_king、old_king今日比特币

第二次挪用<kingmember<t>>::get()是不需要的比特币兑换人民币,运用existing_king.clone():

fn swap_king_with_cache(origin) -> dispatchresult {let new_king = ensure_signed(origin)?;let existing_king = <kingmember<t>>::get();let old_king = existing_king.clone();ensure!(!self::is_member(&existing_king), \”current king is a member so maintains priority\”);ensure!(self::is_member(&new_king), \”new king is not a member so doesn\’t get priority\”);<kingmember<t>>::put(new_king.clone());self::deposit_event(rawevent::betterkingswap(old_king, new_king));ok(())}

归纳

在举行substrate runtime开拓时,一再创造偶尔变量会引导runtime保存挪用减少,进而减少运转本钱,该当尽管缩小不需要的二次挪用比特币挖矿软件下载

对于rust基础数据典型比特币怎么挖矿,即保存在栈上的不妨径直正片值的数据典型(copy典型),径直运用该值即可;

对于rust复合数据典型或substarte frame设置的数据典型,即保存在堆上的须要经过南针或援用办法挪用的数据典型(clone典型),运用该值的clone()因变量获得该值的复本比特币病毒

关于作者: szhbsd

热门文章