Skip to content

missed optimization: coalesce PathBuf reallocation #127059

@lolbinarycat

Description

@lolbinarycat

compare the following disassemblies:

#![allow(dead_code)] use std::path::{Path, PathBuf}; #[no_mangle] pub fn boxed_path() -> Box<Path> { Path::new("aaa").into() }
boxed_path:  lea rdi, [rip + .L__unnamed_1]  mov esi, 3  jmp qword ptr [rip + std::path::<impl core::convert::From<&std::path::Path> for alloc::boxed::Box<std::path::Path>>::from::hfd2d6fbd02ea1491@GOTPCREL] .L__unnamed_1:  .zero 3,97
#![allow(dead_code)] use std::path::{Path, PathBuf}; #[no_mangle] pub fn boxed_path() -> Box<Path> { PathBuf::from("aaa").into() }
boxed_path:  push rbx  sub rsp, 32  lea rsi, [rip + .L__unnamed_1]  lea rbx, [rsp + 8]  mov edx, 3  mov rdi, rbx  call qword ptr [rip + std::sys::os_str::bytes::Slice::to_owned::hda0e6bb365f52f1f@GOTPCREL]  mov rdi, rbx  call qword ptr [rip + std::ffi::os_str::OsString::into_boxed_os_str::haa190229882bb506@GOTPCREL]  add rsp, 32  pop rbx  ret .L__unnamed_1:  .zero 3,97

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions