| ... | @@ -107,13 +107,90 @@ test "non const ptr to aliased type" { | ... | @@ -107,13 +107,90 @@ test "non const ptr to aliased type" { |
| 107 | try expect(?*int == ?*i32); | 107 | try expect(?*int == ?*i32); |
| 108 | } | 108 | } |
| 109 | | 109 | |
| 110 | test "cold function" { | 110 | test "function branch hints" { |
| 111 | thisIsAColdFn(); | 111 | const S = struct { |
| 112 | comptime thisIsAColdFn(); | 112 | fn none() void { |
| | 113 | @branchHint(.none); |
| | 114 | } |
| | 115 | fn likely() void { |
| | 116 | @branchHint(.likely); |
| | 117 | } |
| | 118 | fn unlikely() void { |
| | 119 | @branchHint(.unlikely); |
| | 120 | } |
| | 121 | fn cold() void { |
| | 122 | @branchHint(.cold); |
| | 123 | } |
| | 124 | fn unpredictable() void { |
| | 125 | @branchHint(.unpredictable); |
| | 126 | } |
| | 127 | }; |
| | 128 | S.none(); |
| | 129 | S.likely(); |
| | 130 | S.unlikely(); |
| | 131 | S.cold(); |
| | 132 | S.unpredictable(); |
| | 133 | comptime S.none(); |
| | 134 | comptime S.likely(); |
| | 135 | comptime S.unlikely(); |
| | 136 | comptime S.cold(); |
| | 137 | comptime S.unpredictable(); |
| | 138 | } |
| | 139 | |
| | 140 | test "if branch hints" { |
| | 141 | var t: bool = undefined; |
| | 142 | t = true; |
| | 143 | if (t) { |
| | 144 | @branchHint(.likely); |
| | 145 | } else { |
| | 146 | @branchHint(.cold); |
| | 147 | } |
| 113 | } | 148 | } |
| 114 | | 149 | |
| 115 | fn thisIsAColdFn() void { | 150 | test "switch branch hints" { |
| 116 | @branchHint(.cold); | 151 | var t: bool = undefined; |
| | 152 | t = true; |
| | 153 | switch (t) { |
| | 154 | true => { |
| | 155 | @branchHint(.likely); |
| | 156 | }, |
| | 157 | false => { |
| | 158 | @branchHint(.cold); |
| | 159 | }, |
| | 160 | } |
| | 161 | } |
| | 162 | |
| | 163 | test "orelse branch hints" { |
| | 164 | var x: ?u32 = undefined; |
| | 165 | x = 123; |
| | 166 | const val = x orelse val: { |
| | 167 | @branchHint(.cold); |
| | 168 | break :val 456; |
| | 169 | }; |
| | 170 | try expect(val == 123); |
| | 171 | } |
| | 172 | |
| | 173 | test "catch branch hints" { |
| | 174 | var x: error{Bad}!u32 = undefined; |
| | 175 | x = 123; |
| | 176 | const val = x catch val: { |
| | 177 | @branchHint(.cold); |
| | 178 | break :val 456; |
| | 179 | }; |
| | 180 | try expect(val == 123); |
| | 181 | } |
| | 182 | |
| | 183 | test "and/or branch hints" { |
| | 184 | var t: bool = undefined; |
| | 185 | t = true; |
| | 186 | try expect(t or b: { |
| | 187 | @branchHint(.unlikely); |
| | 188 | break :b false; |
| | 189 | }); |
| | 190 | try expect(t and b: { |
| | 191 | @branchHint(.likely); |
| | 192 | break :b true; |
| | 193 | }); |
| 117 | } | 194 | } |
| 118 | | 195 | |
| 119 | test "unicode escape in character literal" { | 196 | test "unicode escape in character literal" { |