Skip to content

Avoid unnecessary heap allocation in Display impl for DataTypeNode #260

@nohupped

Description

@nohupped

Can I remove unnecessary heap allocation in the Display trait for DataTypeNode?

Currently, the Display implementation for DataTypeNode calls .to_string() on a known static string ("UInt8", "Int32", etc), which leads to unnecessary heap allocation even with -C opt-level=3. It performs a heap allocation and deallocation every time fmt() is called. A minimal example in Godbolt.

mov     edi, 5                    ; size = 5
call    __rust_alloc              ; heap allocation
mov     byte ptr [rax + 4], 56    ; manual copying
mov     dword ptr [rax], 1953384789
call    __rust_dealloc            ; cleanup

Using f.write_str("UInt8") or write!(f, "UInt8") avoids this heap allocation, and is also idiomatic.

lea     rsi, [rip + .Lanon.d10afb4a363194822fac1f5885ac38a9.0]
.Lanon.d10afb4a363194822fac1f5885ac38a9.0:
        .ascii  "UInt8"

Perhaps this isn't a hot path, but it's a zero risk improvement that makes the code more idiomatic. Happy to implement this if you're interested.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions