{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/49541","repository_url":"https://api.github.com/repos/rust-lang/rust","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/49541/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/49541/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/49541/events","html_url":"https://github.com/rust-lang/rust/issues/49541","id":310235684,"number":49541,"title":"Suboptimal inlining decisions","user":{"login":"glandium","id":1038527,"avatar_url":"https://avatars3.githubusercontent.com/u/1038527?v=4","gravatar_id":"","url":"https://api.github.com/users/glandium","html_url":"https://github.com/glandium","followers_url":"https://api.github.com/users/glandium/followers","following_url":"https://api.github.com/users/glandium/following{/other_user}","gists_url":"https://api.github.com/users/glandium/gists{/gist_id}","starred_url":"https://api.github.com/users/glandium/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/glandium/subscriptions","organizations_url":"https://api.github.com/users/glandium/orgs","repos_url":"https://api.github.com/users/glandium/repos","events_url":"https://api.github.com/users/glandium/events{/privacy}","received_events_url":"https://api.github.com/users/glandium/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-31T11:59:51Z","updated_at":"2018-03-31T12:55:05Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"I suspect this can happen in more cases, but here is how I observed this:\r\n```rust\r\npub fn foo() -> Box<[u8]> {\r\n    vec![0].into_boxed_slice()\r\n}\r\n```\r\n\r\nThis compiles to:\r\n```asm\r\n  sub rsp, 56\r\n  lea rdx, [rsp + 8]\r\n  mov edi, 1\r\n  mov esi, 1\r\n  call __rust_alloc@PLT\r\n  test rax, rax\r\n  je .LBB2_1\r\n  mov byte ptr [rax], 0\r\n  mov edx, 1\r\n  add rsp, 56\r\n  ret\r\n.LBB2_1:\r\n  (snip oom handling)\r\n```\r\n\r\nWhich is pretty much to the point.\r\n\r\nNow duplicate the function, so that you now have two functions calling `into_boxed_slice()`, and the compiler decides not to inline it at all anymore. Which:\r\n- adds the full blown `Vec::into_boxed_slice` implementation (63 lines of assembly)\r\n- adds `ptr::drop_in_place`\r\n- and changes the function above to:\r\n```asm\r\n  sub rsp, 56\r\n  lea rdx, [rsp + 8]\r\n  mov edi, 1\r\n  mov esi, 1\r\n  call __rust_alloc@PLT\r\n  test rax, rax\r\n  je .LBB4_1\r\n  mov byte ptr [rax], 0\r\n  mov qword ptr [rsp + 8], rax\r\n  mov qword ptr [rsp + 16], 1\r\n  mov qword ptr [rsp + 24], 1\r\n  lea rdi, [rsp + 8]\r\n  call <alloc::vec::Vec<T>>::into_boxed_slice\r\n  add rsp, 56\r\n  ret\r\n.LBB4_1:\r\n  (snip oom handling)\r\n```\r\n\r\nThe threshold to stop inlining seems pretty low for this particular case, and even if it might make sense for some uses across the codebase to not be inlined, when the result of inlining is clearly beneficial, it would be good if we could still inline the calls where it's a win."},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/377691141","html_url":"https://github.com/rust-lang/rust/issues/49541#issuecomment-377691141","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/49541","id":377691141,"user":{"login":"nox","id":123095,"avatar_url":"https://avatars0.githubusercontent.com/u/123095?v=4","gravatar_id":"","url":"https://api.github.com/users/nox","html_url":"https://github.com/nox","followers_url":"https://api.github.com/users/nox/followers","following_url":"https://api.github.com/users/nox/following{/other_user}","gists_url":"https://api.github.com/users/nox/gists{/gist_id}","starred_url":"https://api.github.com/users/nox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nox/subscriptions","organizations_url":"https://api.github.com/users/nox/orgs","repos_url":"https://api.github.com/users/nox/repos","events_url":"https://api.github.com/users/nox/events{/privacy}","received_events_url":"https://api.github.com/users/nox/received_events","type":"User","site_admin":false},"created_at":"2018-03-31T12:55:05Z","updated_at":"2018-03-31T12:55:05Z","author_association":"CONTRIBUTOR","body":"Cc @rust-lang/wg-codegen "}}
{"actor":{"display_login":"nox"}}