Skip to content

Specialized slice::fill implementations are not inlined #83235

@steffahn

Description

@steffahn

Specialized slice::fill implementations are not inlined even though they would compile down to pretty much just a single memset-call.

The bar function below represents manual inlining of the implementation of fill.

use core::ptr::write_bytes;

pub fn foo(slice: &mut [u8]) {
    slice.fill(42);
}

pub fn bar(slice: &mut [u8]) {
    unsafe {
        let ptr = slice.as_mut_ptr();
        let len = slice.len();
        write_bytes(ptr, 0_u8, len);
    }
}

(Playground)

    Finished release [optimized] target(s) in 0.83s
   
---------- Result ----------

playground::foo: # @playground::foo
# %bb.0:
	movl	$42, %edx
	jmpq	*<[u8] as core::slice::specialize::SpecFill<u8>>::spec_fill@GOTPCREL(%rip) # TAILCALL
                                        # -- End function

playground::bar: # @playground::bar
# %bb.0:
	movq	%rsi, %rdx
	xorl	%esi, %esi
	jmpq	*memset@GOTPCREL(%rip)          # TAILCALL
                                        # -- End function

I haven’t tested it in any way, but I suspect that this can be fixed by marking the fill specialized implementations for u8, i8, and bool as #[inline].

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.I-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library 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