Swift泛型是否分别编译? [英] Are Swift generics separately compiled?

查看:118
本文介绍了Swift泛型是否分别编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出 Swift 泛型是如何工作的。具体而言,我无法找到关于编译模型的精确声明。在C ++中,泛型(模板)不是单独编译的,因为它们被实例化(至少在原则上)为每个调用站点。相反,Java泛型是分别编译的。 Swift的情况如何,关于单独编译泛型?解析方案

这是两者的结合,取决于优化器决定会获得更好的表现。他们在 WWDC Session 404:Advanced Swift 结束时讨论它。幻灯片说:
$ b


Swift可以直接运行通用代码



优化器可以生成专用版本的通用代码


  • 单独编写泛型

  • 更快的编译

  • 灵活交易代码大小以提高速度



I'm trying to figure out how Swift generics work. Specifically, I didn't manage to find a precise statement about the compilation model. In C++, generics (templates) are not separately compiled as they are instantiated (at least in principle) for each call site. Java generics, instead, are separately compiled. What is the case with Swift, concerning separate compilation of generics?

解决方案

It's a combination of the two, depending on what the optimizer decides will get better performance. They talk about it near the end of WWDC Session 404: Advanced Swift. The slide says:

Swift can run generic code directly

Optimizer can produce specialized versions of generic code at will

  • Separate compilation of generics
  • Faster compiles
  • Flexibility to trade code size for speed

这篇关于Swift泛型是否分别编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆