Skip to content

Improve codegen for single argument format_args! #75301

@pickfire

Description

@pickfire

I was using write! in a lot of places, some I just do write!(writer, " ") but I noticed that the generated assembly is not as good as writer.write_all(b" "). Since we have minimal const fn now, I wonder if we could improve the generated assembly when a single character or a single argument is passed in.

use std::io::{self, Write};

pub fn write_char(buf: &mut Vec<u8>) -> io::Result<()> {
    write!(buf, " ")
}
use std::io::{self, Write};

pub fn write_char(buf: &mut Vec<u8>) -> io::Result<()> {
    buf.write_all(b" ")
}

https://rust.godbolt.org/z/zzEMjd

I also write!() could accept a single byte or character. Example, write!(buf, ' ').

If anyone can mentor, maybe I can take on this part (hopefully without compiling rust).

Meta

rustc --version --verbose:

nigthly

CC @lzutao

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-fmtArea: `core::fmt`C-bugCategory: This is a bug.I-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.T-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