简体   繁体   English

Swift程序调用Rust库编译为dylib的性能影响?

[英]Performance implications of a Swift program calling a Rust library compiled as a dylib?

I am writing a OS X application in Swift and considering implementing a portion of my model code (MVC pov) in Rust in order to gain portability of usage on a web server (since Swift doesn't extend into the web server space). 我正在用Swift编写OS X应用程序,并考虑在Rust中实现我的模型代码(MVC pov)的一部分,以便获得在Web服务器上使用的可移植性(因为Swift不会扩展到Web服务器空间中)。

If I compile my Rust code as a dynamic library (.dylib) and follow certain guidelines, I understand that it will produce an external interface that will look like a typical "C" interface that Swift can then consume. 如果我将Rust代码编译为动态库(.dylib)并遵循某些准则,则我知道它将生成一个外部接口,该接口看起来像Swift可以使用的典型“ C”接口。

What would the performance implications be of Swift communicating with the dylib in this instance? 在这种情况下,Swift与dylib通信会对性能产生什么影响? Would they be any different to communicating with something written in C? 它们与用C语言编写的内容进行通信有什么不同吗? The model component in the dylib can be assumed to be central to the application and hence the communication is anticipated to be "chatty". 可以将dylib中的模型组件假定为应用程序的核心,因此可以预期通信是“聊天”的。

[If I were to code the whole thing in Swift, the model portion would have been contained in a separate framework/library which is very similar to a dylib in any event.] [如果我要用Swift编写整个代码,则模型部分将包含在单独的框架/库中,无论如何,该框架/库与dylib非常相似。]

When using a Foreign Function Interface, I can see two major cost centers (performance-wise): 使用外部功能接口时,我可以看到两个主要的成本中心(从性能角度来看):

  1. Conversion: if you need to convert the data back and forth (either on one side or even worse on both sides) 转换:如果您需要来回转换数据(一方面或什至更糟)
  2. Lost optimization opportunity: no inlining, and thus lots of missed opportunities 失去优化机会:没有内联,因此错过了很多机会

The latter can be recovered with a "better" toolchain, especially since in this case both languages compile down to LLVM IR, and I know it is an ambition for at least some Rust developers/users to manage this for Rust/C interaction, but I have never seen it yet. 后者可以用“更好”的工具链来恢复,特别是因为在这种情况下,两种语言都可以编译为LLVM IR,我知道至少一些Rust开发人员/用户要实现Rust / C交互来管理它是一个雄心壮志,我还没看过

The former requires a careful design and even then it can be inevitable that some conversions will occur if you wish to use native compound types on either side ( String being a prime candidate). 前者需要仔细的设计,即使这样,如果您希望在任一侧使用本机复合类型( String是主要的候选对象),那么不可避免地会发生一些转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM