{"action":"opened","issue":{"url":"https://api.github.com/repos/zig-lang/zig/issues/489","repository_url":"https://api.github.com/repos/zig-lang/zig","labels_url":"https://api.github.com/repos/zig-lang/zig/issues/489/labels{/name}","comments_url":"https://api.github.com/repos/zig-lang/zig/issues/489/comments","events_url":"https://api.github.com/repos/zig-lang/zig/issues/489/events","html_url":"https://github.com/zig-lang/zig/issues/489","id":258299029,"number":489,"title":"if+/if-","user":{"login":"PavelVozenilek","id":5585037,"avatar_url":"https://avatars3.githubusercontent.com/u/5585037?v=4","gravatar_id":"","url":"https://api.github.com/users/PavelVozenilek","html_url":"https://github.com/PavelVozenilek","followers_url":"https://api.github.com/users/PavelVozenilek/followers","following_url":"https://api.github.com/users/PavelVozenilek/following{/other_user}","gists_url":"https://api.github.com/users/PavelVozenilek/gists{/gist_id}","starred_url":"https://api.github.com/users/PavelVozenilek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PavelVozenilek/subscriptions","organizations_url":"https://api.github.com/users/PavelVozenilek/orgs","repos_url":"https://api.github.com/users/PavelVozenilek/repos","events_url":"https://api.github.com/users/PavelVozenilek/events{/privacy}","received_events_url":"https://api.github.com/users/PavelVozenilek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-09-17T12:16:40Z","updated_at":"2017-09-17T12:16:40Z","closed_at":null,"author_association":"NONE","body":"This is proposal for a small feature, local and isolated. Could improve code readability.\r\n\r\n------------\r\nLinux kernel uses macros `likely` and `unlikely` :\r\n\r\n```\r\nif (likely(x > 0)) { ... }\r\n...\r\nif (unlikely(y == NULL)) { ... }\r\n\r\n```\r\n\r\nThese macros may improve performance a little bit and they serve as handy documentation.\r\n\r\nLanguage Nim supports this functionality too, through its standard library ( https://nim-lang.org/docs/system.html#likely.t,bool ).\r\n\r\n----------------------\r\n\r\n**My proposal:**\r\n\r\nAdd `if+` and `if-` into the language. `if+` would signal that the path is rather likely, `if-` will suggest the flow will not go this way.\r\n\r\n```\r\nif+ (x < 0) { \r\n  ...  // likely goes here\r\n}\r\nif- (err) { \r\n  ... // unlikely\r\n}\r\n\r\n```\r\n\r\nWhat is this good for:\r\n1. It gives hint to source code reader, hint which will be almost never found in the documentation. This hint is very short, intuitive, and doesn't require extra pair of parenthesis.\r\n2. It may slightly improve the performance, by rearranging instructions so that the likely path goes w/o jump, or by inserting hint for branch selector. \r\n\r\n\r\nHow it could be implemented:\r\n1. By doing nothing, just using it as documentation only feature.\r\n2. By using IR opcode `llvm.expect`. This is how clang implements `__builtin_expect`, which is called by `likely`/`unlikely `macros. (GCC also provides `__builtin_expect`, MSVC has nothing similar.)\r\n\r\n-------------------\r\n\r\nPerformance effects of `__builtin_expect` are hotly disputed on the internet. Many people claim programmers are invariable bad in such prediction and profile guided optimization will do much, much better. However, they never support their claim by benchmarks.\r\n\r\nEven if performance is unaffected the documentation value remains. When one is stepping the code through debugger and the flow goes against the hint, one may be get more cautious a catch a bug."}}
{"actor":{"display_login":"PavelVozenilek"}}