Skip to content

ipv6: rewrite core ipv6 methods to operate on u128s - #187

Merged
achanda merged 3 commits into
achanda:masterfrom
Alextopher:ipv6
May 1, 2024
Merged

ipv6: rewrite core ipv6 methods to operate on u128s #187
achanda merged 3 commits into
achanda:masterfrom
Alextopher:ipv6

Conversation

@Alextopher

Copy link
Copy Markdown
Contributor

I was doing some assembly analysis and saw that the methods on Ipv6Network were not optimizing nearly as well as the equivalent Ipv4Network methods. This PR rewrites the core methods on Ipv6Network to use u128 similarly to how Ipv4Network uses u32.

While preforming this change I addressed some clippy lints and made some more common-sense additions.

Results on .contains()

Before:

.section .text.ipnetwork::ipv6::Ipv6Network::contains,"ax",@progbits	.globl	ipnetwork::ipv6::Ipv6Network::contains	.p2align	4,0x90	.type	ipnetwork::ipv6::Ipv6Network::contains,@functionipnetwork::ipv6::Ipv6Network::contains: // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 226 pub fn contains(&self,ip: Ipv6Addr) -> bool {	.cfi_startprocpushrbp	.cfi_def_cfa_offset 16pushr15	.cfi_def_cfa_offset 24pushr14	.cfi_def_cfa_offset 32pushrbx	.cfi_def_cfa_offset 40	.cfi_offset rbx,-40	.cfi_offset r14,-32	.cfi_offset r15,-24	.cfi_offset rbp,-16 // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/net/ip_addr.rs : 1373 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_,[u16; 8]>(self.octets) };movzxedx, word ptr [rsi]movzxr11d, byte ptr [rdi+16]xoreax,eaxmovr8d,r11dsubr8b,16movr9d,16movr10d,r11d	cmovae r10d,r9dmovecx,-65536 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 206 let mask = !(0xffff >> set_bits) as u16;sarxebx,ecx,r10d // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 208 chunk[1] = mask as u8;movzxebp,bh	vmovd xmm0,ebp	vpinsrb xmm0,xmm0,ebx,1 // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/num/mod.rs : 493 uint_impl! {movzxr8d,r8b	cmovb r8d,eaxcmpr8b,16	cmovae r8d,r9dmovr10d,r11dsubr10b,32movzxr10d,r10b	cmovb r10d,eaxcmpr10b,16	cmovae r10d,r9dmovebx,r11dsubbl,48movzxebx,bl	cmovb ebx,eaxcmpbl,16	cmovae ebx,r9dmovebp,r11dsub bpl,64movzxebp, bpl	cmovb ebp,eaxcmp bpl,16	cmovae ebp,r9dmovr14d,r11dsubr14b,80movzxr14d,r14b	cmovb r14d,eaxcmpr14b,16	cmovae r14d,r9dmovr15d,r11dsubr15b,96movzxr15d,r15b	cmovb r15d,eaxcmpr15b,16	cmovae r15d,r9dsubr11b,112movzxr11d,r11b	cmovb r11d,eaxcmpr11b,16	cmovae r11d,r9d // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/net/ip_addr.rs : 1373 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_,[u16; 8]>(self.octets) };	vmovd r9d,xmm0 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xordx, word ptr [rdi]testdx,r9w // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;jne .LBB39_14sarxeax,ecx,r8dmovedx,eaxshredx,8	vpinsrb xmm0,xmm0,edx,2	vpinsrb xmm0,xmm0,eax,3sarxeax,ecx,r10dmovedx,eaxshredx,8	vpinsrb xmm0,xmm0,edx,4	vpinsrb xmm5,xmm0,eax,5sarxeax,ecx,ebxmovedx,eaxshredx,8	vpinsrb xmm0,xmm5,edx,6	vpinsrb xmm4,xmm0,eax,7sarxeax,ecx,ebpmovedx,eaxshredx,8	vpinsrb xmm0,xmm4,edx,8	vpinsrb xmm3,xmm0,eax,9sarxeax,ecx,r14dmovedx,eaxshredx,8	vpinsrb xmm0,xmm3,edx,10	vpinsrb xmm2,xmm0,eax,11sarxeax,ecx,r15dmovedx,eaxshredx,8	vpinsrb xmm0,xmm2,edx,12	vpinsrb xmm1,xmm0,eax,13sarxeax,ecx,r11dmovecx,eaxshrecx,8	vpinsrb xmm0,xmm1,ecx,14	vpinsrb xmm0,xmm0,eax,15 // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/net/ip_addr.rs : 1373 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_,[u16; 8]>(self.octets) };	vpextrw eax,xmm0,1movzxecx, word ptr [rsi+2] // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorcx, word ptr [rdi+2]testecx,eax // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;jne .LBB39_2movzxeax, word ptr [rdi+4]movzxecx, word ptr [rsi+4]	vpextrw edx,xmm5,2 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorecx,eaxtestcx,dx // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;jne .LBB39_2movzxeax, word ptr [rdi+6]movzxecx, word ptr [rsi+6]	vpextrw edx,xmm4,3 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorecx,eaxtestcx,dx // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;jne .LBB39_2movzxeax, word ptr [rdi+8]movzxecx, word ptr [rsi+8]	vpextrw edx,xmm3,4 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorecx,eaxtestcx,dx // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;jne .LBB39_2movzxeax, word ptr [rdi+10]movzxecx, word ptr [rsi+10]	vpextrw edx,xmm2,5 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorecx,eaxtestcx,dx // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;jne .LBB39_2movzxeax, word ptr [rdi+12]movzxecx, word ptr [rsi+12]	vpextrw edx,xmm1,6 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorecx,eaxtestcx,dx // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/iter/traits/iterator.rs : 2462 accum = f(accum, x)?;je .LBB39_13.LBB39_2:xoreax,eax.LBB39_14: // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 235 }poprbx	.cfi_def_cfa_offset 32popr14	.cfi_def_cfa_offset 24popr15	.cfi_def_cfa_offset 16poprbp	.cfi_def_cfa_offset 8ret.LBB39_13:	.cfi_def_cfa_offset 40movzxeax, word ptr [rdi+14]movzxecx, word ptr [rsi+14]	vpextrw edx,xmm0,7 // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 234 .all(|(mask, (a, b))| a & mask == b & mask)xorecx,eaxtestcx,dx	sete aljmp .LBB39_14

After:

.section .text.ipnetwork::ipv6::Ipv6Network::contains,"ax",@progbits	.globl	ipnetwork::ipv6::Ipv6Network::contains	.p2align	4,0x90	.type	ipnetwork::ipv6::Ipv6Network::contains,@functionipnetwork::ipv6::Ipv6Network::contains: // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 242 pub fn contains(&self,ip: Ipv6Addr) -> bool {	.cfi_startprocmovzxeax, byte ptr [rdi+16]xorecx,ecx // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 190 let mask = u128::MAX << (IPV6_BITS - self.prefix);xoredx,edxsubdl,almovrax,-1shlxr8,rax,rdxtestdl,64	cmove rcx,r8	cmovne rax,r8 // /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/num/mod.rs : 1205 uint_impl! {bswapraxbswaprcx // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 208 let network = u128::from(self.addr) & mask;movrdx, qword ptr [rdi]movrdi, qword ptr [rdi+8] // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 248 (ip & mask) == netxorrdx, qword ptr [rsi]andrdx,raxxorrdi, qword ptr [rsi+8]andrdi,rcxorrdi,rdx	sete al // /home/mahonec/p/github.com/Alextopher/ipnetwork/src/ipv6.rs : 249 }ret

@Alextopher
Alextopherforce-pushed the ipv6 branch 2 times, most recently from 3a8d94e to 6586886CompareApril 29, 2024 18:06
@achanda
achanda merged commit 70d1f74 into achanda:masterMay 1, 2024
@achanda

Copy link
Copy Markdown
Owner

Thanks! This is great.

@github-actionsgithub-actionsBot mentioned this pull request Jan 6, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@Alextopher@achanda