{"action":"closed","issue":{"url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/116","labels_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/116/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/116/comments","events_url":"https://api.github.com/repos/Microsoft/visualfsharp/issues/116/events","html_url":"https://github.com/Microsoft/visualfsharp/issues/116","id":55545912,"number":116,"title":"F# compiler generating unnecessary/incorrect closure for inlined function","user":{"login":"KevinRansom","id":5175830,"avatar_url":"https://avatars.githubusercontent.com/u/5175830?v=3","gravatar_id":"","url":"https://api.github.com/users/KevinRansom","html_url":"https://github.com/KevinRansom","followers_url":"https://api.github.com/users/KevinRansom/followers","following_url":"https://api.github.com/users/KevinRansom/following{/other_user}","gists_url":"https://api.github.com/users/KevinRansom/gists{/gist_id}","starred_url":"https://api.github.com/users/KevinRansom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KevinRansom/subscriptions","organizations_url":"https://api.github.com/users/KevinRansom/orgs","repos_url":"https://api.github.com/users/KevinRansom/repos","events_url":"https://api.github.com/users/KevinRansom/events{/privacy}","received_events_url":"https://api.github.com/users/KevinRansom/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/visualfsharp/labels/suggestion","name":"suggestion","color":"207de5"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-26T22:18:59Z","updated_at":"2015-02-13T11:05:58Z","closed_at":"2015-02-13T11:05:58Z","body":"Originally opened at [CodePlex](https://visualfsharp.codeplex.com/workitem/98) by [jackpappas](https://visualfsharp.codeplex.com/workitem/98)\r\n\r\nI was looking at the \"Language Benchmarks Game\" this afternoon to see if any of the F# implementations could be improved. Long story short, I compiled the F# implementation of the 'pidigits' benchmark using an F# compiler I built from source yesterday, and it looks like the compiler is allocating an unnecessary (and unwanted) closure within what is designed to be an optimized loop. \r\n\r\nI've attached a slightly modified / cleaned-up version of the benchmark implementation code so you can reproduce this. \r\n\r\nComparing the F# code and the IL produced by the compiler, the compiler appears to be creating a closure for the 'prdigit' function instead of inlining it as expected. The function has the type signature \"int -> bool\", and the single time it's called, the result is assigned to the value \"more\" (a module-scoped value). The closure generated by the compiler captures the \"int\" argument, but returns a \"unit -> unit\" function, which is then immediately called. The other code in the if/else block where 'prdigit' is called -- where the value of 'false' is assigned to \"more\" if 'prdigit' returned 'true', otherwise the 'compose_r' function is called -- has also been inlined/captured into the closure, which is how the code still manages to produce the correct result.\r\n\r\nfile attachments\r\n \r\n* [Program.fs](https://www.codeplex.com/Download/AttachmentDownload.ashx?ProjectName=visualfsharp&WorkItemId=98&FileAttachmentId=879115)\r\n\r\n>comments\r\n>TIHan wrote Oct 8, 2014 at 1:23 AM [x] \r\n>If you take this piece:\r\n>```fsharp\r\n    // Generate successive digits of PI. \r\n    let mutable k = 1\r\n    i <- 0\r\n    set_si (&q, 1)\r\n    set_si (&r, 0)\r\n    set_si (&s, 0)\r\n    set_si (&t, 1)\r\n    let mutable more = true\r\n    while more do\r\n        let y = extract 3\r\n        if y = extract 4 then\r\n            if prdigit y then\r\n                more <- false\r\n            else\r\n                compose_r (10, -10 * y, 0, 1)\r\n        else\r\n            compose_l (k, 4 * k + 2, 0, 2 * k + 1)\r\n            k <- k + 1\r\n```\r\n>And wrap it in a function:\r\n>```fsharp\r\nlet compute () =\r\n    // Generate successive digits of PI. \r\n    let mutable k = 1\r\n    i <- 0\r\n    set_si (&q, 1)\r\n    set_si (&r, 0)\r\n    set_si (&s, 0)\r\n    set_si (&t, 1)\r\n    let mutable more = true\r\n    while more do\r\n        let y = extract 3\r\n        if y = extract 4 then\r\n            if prdigit y then\r\n                more <- false\r\n            else\r\n                compose_r (10, -10 * y, 0, 1)\r\n        else\r\n            compose_l (k, 4 * k + 2, 0, 2 * k + 1)\r\n            k <- k + 1\r\n    0\r\n```\r\nprdigit will inline properly."}}