Skip to content

Instantly share code, notes, and snippets.

@krdln
Last active October 10, 2022 20:46
Show Gist options
  • Save krdln/cefbb4a14d7ddfb5014099d1a0060916 to your computer and use it in GitHub Desktop.
Save krdln/cefbb4a14d7ddfb5014099d1a0060916 to your computer and use it in GitHub Desktop.
Microbenchmarks for niche get discriminant codegen
#![crate_type="lib"]
pub enum Bool {
True,
Other(char), // try reordering variants
False,
}
use Bool::*;
pub fn ab(x: Bool) -> char {
match x {
Other(c) => c,
True => 'a',
False => 'b',
}
}
pub fn xx(x: Bool) -> char {
match x {
Other(c) => c,
True | False => 'x',
}
}
pub fn x(x: Bool) -> char {
match x {
Other(c) => c,
_ => 'x',
}
}
#![crate_type="lib"]
pub enum Foo {
Bar(bool), // try also Bar(char)
Baz,
Quux,
Quux2,
}
pub fn disco(foo: Foo) -> std::mem::Discriminant<Foo> {
std::mem::discriminant(&foo)
}
pub fn bar(foo: Foo) -> bool {
matches!(foo, Foo::Bar(..))
}
pub fn baz(foo: Foo) -> bool {
matches!(foo, Foo::Baz)
}
pub fn bazquuxes(foo: Foo) -> bool {
matches!(foo, Foo::Baz | Foo::Quux | Foo::Quux2)
}
pub fn quuxes(foo: Foo) -> bool {
matches!(foo, Foo::Quux | Foo::Quux2)
}
pub fn bazquux(foo: Foo) -> bool {
matches!(foo, Foo::Baz | Foo::Quux)
}
pub fn barbaz(foo: Foo) -> bool {
matches!(foo, Foo::Bar(..) | Foo::Baz)
}
pub fn bazquuxes2(foo: Foo) -> bool {
match foo {
Foo::Bar(..) => false,
Foo::Baz | Foo::Quux | Foo::Quux2 => true,
// Foo::Uninh(nope) => nope,
}
}
pub enum T {
Pair(Box<T>, [bool; 8]),
I8,
I16,
I32,
I64,
}
impl T {
pub fn sz(&self) -> usize {
match self {
T::Pair(bx, _) => bx.sz(),
T::I8 => 1,
T::I16 => 2,
T::I32 => 4,
T::I64 => 8,
}
}
}
In archive libenum_fair.rlib:
lib.rmeta: file format elf64-x86-64
enum-fair.enum_fair.d9414b89-cgu.0.rcgu.o: file format elf64-x86-64
Disassembly of section .text._ZN4core3mem12discriminant17h6b81bfaa4f5db990E:
0000000000000000 <core::mem::discriminant>:
0: 0f b6 07 movzx eax,BYTE PTR [rdi]
3: 04 fe add al,0xfe
5: 0f b6 c8 movzx ecx,al
8: 48 8d 51 01 lea rdx,[rcx+0x1]
c: 31 c0 xor eax,eax
e: 80 f9 03 cmp cl,0x3
11: 48 0f 42 c2 cmovb rax,rdx
15: c3 ret
enum-fair.enum_fair.d9414b89-cgu.1.rcgu.o: file format elf64-x86-64
Disassembly of section .text._ZN9enum_fair5disco17h834d4a410b4bb359E:
0000000000000000 <enum_fair::disco>:
0: 40 80 c7 fe add dil,0xfe
4: 40 0f b6 cf movzx ecx,dil
8: 48 8d 51 01 lea rdx,[rcx+0x1]
c: 31 c0 xor eax,eax
e: 80 f9 03 cmp cl,0x3
11: 48 0f 42 c2 cmovb rax,rdx
15: c3 ret
Disassembly of section .text._ZN9enum_fair3bar17hb9720b324d89772bE:
0000000000000000 <enum_fair::bar>:
0: 40 80 c7 fb add dil,0xfb
4: 40 80 ff fd cmp dil,0xfd
8: 0f 92 c0 setb al
b: c3 ret
Disassembly of section .text._ZN9enum_fair3baz17hc54e2017edcd11f4E:
0000000000000000 <enum_fair::baz>:
0: 40 80 ff 02 cmp dil,0x2
4: 0f 94 c0 sete al
7: c3 ret
Disassembly of section .text._ZN9enum_fair6quuxes17h0e267005b1656da8E:
0000000000000000 <enum_fair::quuxes>:
0: 40 80 c7 fe add dil,0xfe
4: 40 80 ff 03 cmp dil,0x3
8: 0f 92 c1 setb cl
b: 40 0f b6 c7 movzx eax,dil
f: 48 ff c8 dec rax
12: 48 83 f8 02 cmp rax,0x2
16: 0f 92 c0 setb al
19: 20 c8 and al,cl
1b: c3 ret
Disassembly of section .text._ZN9enum_fair7bazquux17he11602f6be3a6028E:
0000000000000000 <enum_fair::bazquux>:
0: 40 80 e7 fe and dil,0xfe
4: 40 80 ff 02 cmp dil,0x2
8: 0f 94 c0 sete al
b: c3 ret
Disassembly of section .text._ZN9enum_fair6barbaz17hbb197476ea7e5374E:
0000000000000000 <enum_fair::barbaz>:
0: 40 80 c7 fb add dil,0xfb
4: 40 80 ff fe cmp dil,0xfe
8: 0f 92 c0 setb al
b: c3 ret
Disassembly of section .text._ZN9enum_fair10bazquuxes217hda039980e3af6563E:
0000000000000000 <enum_fair::bazquuxes2>:
0: 40 80 c7 fe add dil,0xfe
4: 40 80 ff 03 cmp dil,0x3
8: 0f 92 c0 setb al
b: c3 ret
Disassembly of section .text._ZN9enum_fair1T2sz17h5da803a1d2a78852E:
0000000000000000 <enum_fair::T::sz>:
0: 31 c9 xor ecx,ecx
2: 66 66 66 66 66 2e 0f data16 data16 data16 data16 cs nop WORD PTR [rax+rax*1+0x0]
9: 1f 84 00 00 00 00 00
10: 0f b6 47 08 movzx eax,BYTE PTR [rdi+0x8]
14: 04 fe add al,0xfe
16: 0f b6 d0 movzx edx,al
19: 48 8d 42 01 lea rax,[rdx+0x1]
1d: 80 fa 04 cmp dl,0x4
20: 48 0f 43 c1 cmovae rax,rcx
24: 48 85 c0 test rax,rax
27: 75 05 jne 2e <enum_fair::T::sz+0x2e>
29: 48 8b 3f mov rdi,QWORD PTR [rdi]
2c: eb e2 jmp 10 <enum_fair::T::sz+0x10>
2e: 48 8d 48 ff lea rcx,[rax-0x1]
32: 48 83 f9 02 cmp rcx,0x2
36: 73 01 jae 39 <enum_fair::T::sz+0x39>
38: c3 ret
39: 83 f8 03 cmp eax,0x3
3c: 75 06 jne 44 <enum_fair::T::sz+0x44>
3e: b8 04 00 00 00 mov eax,0x4
43: c3 ret
44: b8 08 00 00 00 mov eax,0x8
49: c3 ret
In archive libenum_fair.rlib:
lib.rmeta: file format elf64-x86-64
enum-fair.enum_fair.34678b7e-cgu.0.rcgu.o: file format elf64-x86-64
Disassembly of section .text._ZN9enum_fair5disco17hd727fe631a0be119E:
0000000000000000 <enum_fair::disco>:
0: 8d 47 ff lea eax,[rdi-0x1]
3: 31 c9 xor ecx,ecx
5: 40 80 ff 02 cmp dil,0x2
9: 0f b6 c0 movzx eax,al
c: 0f 42 c1 cmovb eax,ecx
f: 0f b6 c0 movzx eax,al
12: c3 ret
Disassembly of section .text._ZN9enum_fair3bar17h2d286b251283cf94E:
0000000000000000 <enum_fair::bar>:
0: 40 80 ff 02 cmp dil,0x2
4: 0f 92 c0 setb al
7: c3 ret
Disassembly of section .text._ZN9enum_fair3baz17h98a71e6ec8974923E:
0000000000000000 <enum_fair::baz>:
0: 40 80 ff 02 cmp dil,0x2
4: 0f 94 c0 sete al
7: c3 ret
Disassembly of section .text._ZN9enum_fair9bazquuxes17h3c886005d9b90342E:
0000000000000000 <enum_fair::bazquuxes>:
0: 40 80 c7 fe add dil,0xfe
4: 40 80 ff 03 cmp dil,0x3
8: 0f 92 c0 setb al
b: c3 ret
Disassembly of section .text._ZN9enum_fair6quuxes17h4e45bdb8222eea11E:
0000000000000000 <enum_fair::quuxes>:
0: 40 80 c7 fd add dil,0xfd
4: 40 80 ff 02 cmp dil,0x2
8: 0f 92 c0 setb al
b: c3 ret
Disassembly of section .text._ZN9enum_fair7bazquux17hfd208c30bcc965d6E:
0000000000000000 <enum_fair::bazquux>:
0: 40 80 e7 fe and dil,0xfe
4: 40 80 ff 02 cmp dil,0x2
8: 0f 94 c0 sete al
b: c3 ret
Disassembly of section .text._ZN9enum_fair6barbaz17h34183eccfb9f9750E:
0000000000000000 <enum_fair::barbaz>:
0: 40 80 ff 03 cmp dil,0x3
4: 0f 92 c0 setb al
7: c3 ret
Disassembly of section .text._ZN9enum_fair10bazquuxes217h51131eda90c7a12eE:
0000000000000000 <enum_fair::bazquuxes2>:
0: 40 80 ff 02 cmp dil,0x2
4: 0f 93 c0 setae al
7: c3 ret
Disassembly of section .text._ZN9enum_fair1T2sz17h27123370ab2596f8E:
0000000000000000 <enum_fair::T::sz>:
0: 31 c0 xor eax,eax
2: 66 66 66 66 66 2e 0f data16 data16 data16 data16 cs nop WORD PTR [rax+rax*1+0x0]
9: 1f 84 00 00 00 00 00
10: 0f b6 4f 08 movzx ecx,BYTE PTR [rdi+0x8]
14: 8d 51 ff lea edx,[rcx-0x1]
17: 80 f9 02 cmp cl,0x2
1a: 0f b6 ca movzx ecx,dl
1d: 0f 42 c8 cmovb ecx,eax
20: 84 c9 test cl,cl
22: 75 05 jne 29 <enum_fair::T::sz+0x29>
24: 48 8b 3f mov rdi,QWORD PTR [rdi]
27: eb e7 jmp 10 <enum_fair::T::sz+0x10>
29: 0f b6 c1 movzx eax,cl
2c: ff c8 dec eax
2e: 48 8d 0d 00 00 00 00 lea rcx,[rip+0x0] # 35 <enum_fair::T::sz+0x35>
35: 48 63 04 81 movsxd rax,DWORD PTR [rcx+rax*4]
39: 48 01 c8 add rax,rcx
3c: ff e0 jmp rax
3e: b8 01 00 00 00 mov eax,0x1
43: c3 ret
44: b8 04 00 00 00 mov eax,0x4
49: c3 ret
4a: b8 08 00 00 00 mov eax,0x8
4f: c3 ret
50: b8 02 00 00 00 mov eax,0x2
55: c3 ret
enum-fair.enum_fair.34678b7e-cgu.1.rcgu.o: file format elf64-x86-64
Disassembly of section .text._ZN4core3mem12discriminant17hb75dd2d12ba19f72E:
0000000000000000 <core::mem::discriminant>:
0: 0f b6 07 movzx eax,BYTE PTR [rdi]
3: 8d 48 ff lea ecx,[rax-0x1]
6: 31 d2 xor edx,edx
8: 3c 02 cmp al,0x2
a: 0f b6 c1 movzx eax,cl
d: 0f 42 c2 cmovb eax,edx
10: 0f b6 c0 movzx eax,al
13: c3 ret
In archive libenum_fair.rlib:
lib.rmeta: file format elf64-x86-64
enum-fair.enum_fair.34678b7e-cgu.0.rcgu.o: file format elf64-x86-64
Disassembly of section .text._ZN9enum_fair5disco17hd727fe631a0be119E:
0000000000000000 <enum_fair::disco>:
0: 40 0f b6 cf movzx ecx,dil
4: 48 ff c9 dec rcx
7: 31 c0 xor eax,eax
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c1 cmovae rax,rcx
11: c3 ret
Disassembly of section .text._ZN9enum_fair3bar17h2d286b251283cf94E:
0000000000000000 <enum_fair::bar>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 85 c9 test rcx,rcx
14: 0f 94 c0 sete al
17: c3 ret
Disassembly of section .text._ZN9enum_fair3baz17h98a71e6ec8974923E:
0000000000000000 <enum_fair::baz>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 83 f9 01 cmp rcx,0x1
15: 0f 94 c0 sete al
18: c3 ret
Disassembly of section .text._ZN9enum_fair9bazquuxes17h3c886005d9b90342E:
0000000000000000 <enum_fair::bazquuxes>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 ff c9 dec rcx
14: 48 83 f9 03 cmp rcx,0x3
18: 0f 92 c0 setb al
1b: c3 ret
Disassembly of section .text._ZN9enum_fair6quuxes17h4e45bdb8222eea11E:
0000000000000000 <enum_fair::quuxes>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 83 f9 02 cmp rcx,0x2
15: 0f 93 c0 setae al
18: c3 ret
Disassembly of section .text._ZN9enum_fair7bazquux17hfd208c30bcc965d6E:
0000000000000000 <enum_fair::bazquux>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 ff c9 dec rcx
14: 48 83 f9 02 cmp rcx,0x2
18: 0f 92 c0 setb al
1b: c3 ret
Disassembly of section .text._ZN9enum_fair6barbaz17h34183eccfb9f9750E:
0000000000000000 <enum_fair::barbaz>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 83 f9 02 cmp rcx,0x2
15: 0f 92 c0 setb al
18: c3 ret
Disassembly of section .text._ZN9enum_fair10bazquuxes217h51131eda90c7a12eE:
0000000000000000 <enum_fair::bazquuxes2>:
0: 40 0f b6 c7 movzx eax,dil
4: 48 ff c8 dec rax
7: 31 c9 xor ecx,ecx
9: 40 80 ff 02 cmp dil,0x2
d: 48 0f 43 c8 cmovae rcx,rax
11: 48 85 c9 test rcx,rcx
14: 0f 95 c0 setne al
17: c3 ret
Disassembly of section .text._ZN9enum_fair1T2sz17h27123370ab2596f8E:
0000000000000000 <enum_fair::T::sz>:
0: 31 c9 xor ecx,ecx
2: 66 66 66 66 66 2e 0f data16 data16 data16 data16 cs nop WORD PTR [rax+rax*1+0x0]
9: 1f 84 00 00 00 00 00
10: 0f b6 57 08 movzx edx,BYTE PTR [rdi+0x8]
14: 48 8d 42 ff lea rax,[rdx-0x1]
18: 48 83 fa 02 cmp rdx,0x2
1c: 48 0f 42 c1 cmovb rax,rcx
20: 48 85 c0 test rax,rax
23: 75 05 jne 2a <enum_fair::T::sz+0x2a>
25: 48 8b 3f mov rdi,QWORD PTR [rdi]
28: eb e6 jmp 10 <enum_fair::T::sz+0x10>
2a: 48 8d 48 ff lea rcx,[rax-0x1]
2e: 48 83 f9 02 cmp rcx,0x2
32: 73 01 jae 35 <enum_fair::T::sz+0x35>
34: c3 ret
35: 48 83 f8 03 cmp rax,0x3
39: 75 06 jne 41 <enum_fair::T::sz+0x41>
3b: b8 04 00 00 00 mov eax,0x4
40: c3 ret
41: b8 08 00 00 00 mov eax,0x8
46: c3 ret
enum-fair.enum_fair.34678b7e-cgu.1.rcgu.o: file format elf64-x86-64
Disassembly of section .text._ZN4core3mem12discriminant17hb75dd2d12ba19f72E:
0000000000000000 <core::mem::discriminant>:
0: 0f b6 0f movzx ecx,BYTE PTR [rdi]
3: 48 8d 51 ff lea rdx,[rcx-0x1]
7: 31 c0 xor eax,eax
9: 48 83 f9 02 cmp rcx,0x2
d: 48 0f 43 c2 cmovae rax,rdx
11: c3 ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment