Any plan to support attributes like C++ `[[likely]]`, `[[unlikely]]`, and `[[cold]]`?

Compilers sometimes need some indications to optimize code more efficiently for hot/cold paths. I find [[likely]], [[unlikely]], and [[cold]]in C++ (cold is not standardized in C++ yet) and Rust (currently only support cold in Rust) useful. These attributes sometimes boost the performance more than 30% in my experience.

Swift compiler is powerful enough, but I am wondering if Swift eventually supports these attributes in the future.

Any insight is appreciated. I’d love to know if it’s a good/bad idea to add.

References

Swift already have some methods public func _fastPath(_ x: Bool) -> Bool, public func _slowPath(_ x: Bool) -> Bool, public func _onFastPath(), though they are underlined and not included in the documentation. Perhaps we could propose making them public.

7 Likes

I didn’t know that! That would be awesome if they become public.

I’d appreciate it if you or someone keep us updated when the proposal is created.

Or am I even allowed to make an official proposal? I'm not a Swift contributor or anything though.