《2019年Rust跨平臺客戶端開發在字節跳動的實踐.pdf》由會員分享,可在線閱讀,更多相關《2019年Rust跨平臺客戶端開發在字節跳動的實踐.pdf(32頁珍藏版)》請在三個皮匠報告上搜索。
1、C R O S S-P L A T F O R M R U S TO N T H E C L I E N T-S I D EBy wangcong ByteDanceA G E N D AWhy cross-platform and why Rust?Our experience using RustBuilding with RustPerformance requirementsEver-growing requirements withfixed resourcesNeed to achieve more with lessbattery consumptionImplementatio
2、n complexityAlmost monolithic architectureLow-level code comes with lesssafety guaranteed,traditionallySecurity considerationsClient-side programming ishardAnd,each platform isdifferentWe have to repeat our work and make newmistakesGoing cross-platform bringsusOne codebase to design,implement andrev
3、iewOne server-facing clientOne place to implement security policesC O N C E R N SPerformanceThe app should run fastThe app should not be battery-hungryLook and feelWe need platform-native appearance&interactionsWe care about user experiencewhen doing cross-platformOnly for some componentsWorks but n
4、ot enoughAll code,but with tradeoffNo system UI componentsSystem UI components with tediousand high-cost bindingsHow far do we go cross-platform?We dont sacrifice UX,especially not in frequently-used apps like FeishuNon-UI code in cross-platform,systemlanguages.Optimized for performanceUI code uses
5、platform-specific APIsW H YR U S T?We want a systems programminglanguage with safetyguaranteed,best effort isnot enoughC+is great,but your team only livesin peace when everyone is a guruWhile in Rust,the compiler acts as theguru,always watching your backGuaranteed safety,that meansmemory safety and
6、fearlessconcurrencyWe can focus on the business logic,spend less time hunting for bugsWe have the bravery and confidence toachieve moreOther things we love aboutRustMinimal runtime that easily embeds intoother languagesPerformance with zero-overheadabstractionsHigh-level language with modernfeatures
7、Scalable to large codebases,with lowmaintenance if done correctlyO U RE X P E R I E NC E U S I N GR U S TWe introduced Rust intoFeishu at the beginning of2017We knew what to be scared of,especially in the chaoticworld of systemsUnsafe Rust codeC/C+componentsThe rules of Rust kept usfreeFree to aggre
8、sively introduceparallelism with nothing to worry aboutFree to call battle-tested C+code,finding thread-safety issues on the C+side,where mutable shared state wasinvolvedRust ecosystem is still youngand issues do ariseWhen we started,HTTPS requests failedon iOS&Android due to TLS-relatedproblemsAfte
9、r long-term production use,wefound that the compression library weused could not handle NonCompressedDEFLATE messagesBut Rust is growing quickly,and sufficient for generalusageProblems solved quickly,easy tocontribute,and easy to get helpNew libraries&tools are emerging withthe qualities of RustRust
10、 is still in flux andmajor changes still happenWe have to do refactorings likemigrating error handling from error-chain to failure,instead of focusingon our usersWe get to play with all thetreasures of C/C+.OurArchitectureTHREAD INTERACTIONFFIProtocol BuffersWith enough abstraction,only asmall numbe
11、r of FFI functions iskept and seldom changeNot suitable for large data setsor high-frequency callsCapn Proto and FlatBuffers helpreduce overheadRaw data typesWhen its expected or performancedemands itFFI is unsafe by nature andbrings inherent costsMODULE DESIGNEvery crate can be independely built,ru
12、n,tested and depended onManaged by CargoBuild speedCrates at the bottom should notchange oftenExcessively large crates become abottleneck and reduce parallelismBUILDING WITH RUSTStorage-related featuresbuilt with RustMigration supportResource usage and performance monitorEncryption supportAnd more.N
13、etwork-related featuresbuilt with RustHedged requests and route selectionTLS1.3 and QUIC supportPersistent connections with adaptiveheartbeatDNS over HTTPSNetwork diagnostics:ping,traceroute,reachability comparisonsAnd more.Code footprintStatic dispatch of genericsGenerated code sizeRust uses a diff
14、erent LLVM buildthan the one Apple shipsNo direct bitcode compatibilityChallenges we have facedQ U E S T I O NS?Code footprintStatic dispatch of genericsGenerated code sizeRust uses a different LLVM buildthan the one Apple shipsNo direct bitcode compatibilityChallenges we have facedT H A N K SPhotos from