Skip to content

Use const-initialization in thread-local storage #478

@mahkoh

Description

@mahkoh

GetThreadId uses the following thread-local storage:

        thread_local!(static KEY: u8 = 0);

This can be optimized as follows:

        thread_local!(static KEY: u8 = const { 0 });

which compiles to more optimal assembly:

#[no_mangle]
pub fn a() -> *const u8 {
    thread_local!(static A: u8 = 0);
    A.with(|a| ptr::from_ref(a))
}

#[no_mangle]
pub fn b() -> *const u8 {
    thread_local!(static B: u8 = const { 0 });
    B.with(|b| ptr::from_ref(b))
}
a:
	cmpb	$0, %fs:A@TPOFF
	je	.LBB9_1
	movq	%fs:0, %rax
	leaq	A@TPOFF+1(%rax), %rax
	retq
.LBB9_1:
	movw	$1, %fs:A@TPOFF
	movq	%fs:0, %rax
	leaq	A@TPOFF+1(%rax), %rax
	retq

b:
	movq	%fs:0, %rax
	leaq	B@TPOFF(%rax), %rax
	retq

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions