mojo decorators详解

@always_inline

You can add the @always_inline decorator on any function to make the Mojo compiler “inline” the body of the function (copy it) directly into the body of the calling function.

This eliminates potential performance costs associated with function calls jumping to a new point in code. Normally, the compiler will do this automatically where it can improve performance, but this decorator forces it to do so. The downside is that it can increase the binary size by duplicating the function at every call site.

For example:

@always_inlinefn add(a: Int, b: Int) -> Int:    return a + bprint(add(1, 2))

Because add() is decorated with @always_inline, Mojo compiles this program without adding the add() function to the call stack, and it instead performs the addition directly at the print() call site, as if it were written like this:

print(1 + 2)

@always_inline("nodebug")

You can also use the decorator with the "nodebug" argument, which has the same effect to inline the function, but without debug information. This means you can’t step into the function when debugging, but it reduces the debug build binary size.

@nonmaterializable

You can add the @nonmaterializable decorator on a struct to declare that the type can exist only in the parameter domain (it can be used for metaprogramming only, and not as a runtime type). And, if an instance of this type does transition into the runtime domain, this decorator declares what type it becomes there.

To use it, declare your type with @nonmaterializable(TargetType), where TargetType is the type that the object should convert to if it becomes a runtime value (you must declare the TargetType). For example, if a struct is marked as @nonmaterializable(Foo), then anywhere that it goes from a parameter value to a runtime value, it automatically converts into the Foo type.

For example, the following NmStruct type can be used in the parameter domain, but the converted_to_has_bool instance of it is converted to HasBool when it’s materialized as a runtime value:

@value@register_passable("trivial")struct HasBool:    var x: Bool    fn __init__(x: Bool) -> Self:        return Self {x: x}    @always_inline("nodebug")    fn __init__(nms: NmStruct) -> Self:        return Self {x: True if (nms.x == 77) else False}@value@nonmaterializable(HasBool)@register_passable("trivial")struct NmStruct:    var x: Int    @always_inline("nodebug")    fn __add__(self: Self, rhs: Self) -> Self:        return NmStruct(self.x + rhs.x)alias still_nm_struct = NmStruct(1) + NmStruct(2)# When materializing to a run-time variable, it is automatically converted,# even without a type annotation.var converted_to_has_bool = still_nm_struct
  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启航学途

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值