-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Remove Populatable and BytesConvertable traits #2019
Conversation
I like the changes so far 👍 |
08594a8
to
2c862af
Compare
}; | ||
let mut res: Self = unsafe { ::std::mem::uninitialized() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the size is checked in advance, how fill with zero is safer than just unintialized (which is no-op)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's strictly safer but in general it's better practice than using uninitialized
. LLVM can optimize out the zero-ing so I doubt we'd see a performance penalty. uninitialized
imo should be used only for FFI and dirty tricks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it's most used operation in ipc serialization
Every hash, every vector element go through it, so we better be sure imo
A couple of examples:
Before: .section ".text._ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h68bda87c1f8302ffE","ax",@progbits
.globl _ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h68bda87c1f8302ffE
.align 16, 0x90
.type _ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h68bda87c1f8302ffE,@function
_ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h68bda87c1f8302ffE:
.Lfunc_begin1463:
.cfi_startproc
pushq %rbp
.Ltmp26724:
.cfi_def_cfa_offset 16
.Ltmp26725:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp26726:
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.Ltmp26727:
.cfi_offset %rbx, -24
movq %rdi, %rbx
leaq 8(%rbx), %rdi
.loc 85 678 0
cmpq $520, %rdx
jne .LBB1463_1
.loc 207 12 0
movl $520, %edx
callq memcpy@PLT
xorl %eax, %eax
jmp .LBB1463_3
.LBB1463_1:
.loc 207 9 0
movl $520, %esi
callq _ZN11ethcore_ipc6binary18BinaryConvertError4size17hea6e073b993df4fbE@PLT
movl $1, %eax
.LBB1463_3:
.loc 207 12 0
movq %rax, (%rbx)
.loc 207 12 0 is_stmt 0
movq %rbx, %rax
addq $8, %rsp
popq %rbx
popq %rbp
retq After: .section ".text._ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h8374b5e05199295bE","ax",@progbits
.globl _ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h8374b5e05199295bE
.align 16, 0x90
.type _ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h8374b5e05199295bE,@function
_ZN85_$LT$ethcore..error..BlockError$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h8374b5e05199295bE:
.Lfunc_begin1465:
.cfi_startproc
pushq %rbp
.Ltmp26802:
.cfi_def_cfa_offset 16
.Ltmp26803:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp26804:
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.Ltmp26805:
.cfi_offset %rbx, -24
movq %rdi, %rbx
leaq 8(%rbx), %rdi
.loc 85 678 0
cmpq $520, %rdx
jne .LBB1465_1
.loc 207 13 0
movl $520, %edx
callq memcpy@PLT
xorl %eax, %eax
jmp .LBB1465_3
.LBB1465_1:
.loc 207 9 0
movl $520, %esi
callq _ZN11ethcore_ipc6binary18BinaryConvertError4size17hea6e073b993df4fbE@PLT
movl $1, %eax
.LBB1465_3:
.loc 207 13 0
movq %rax, (%rbx)
.loc 207 13 0 is_stmt 0
movq %rbx, %rax
addq $8, %rsp
popq %rbx
popq %rbp
retq
Before: .section ".text._ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17ha3b6e8fd82cf9e0dE","ax",@progbits
.globl _ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17ha3b6e8fd82cf9e0dE
.align 16, 0x90
.type _ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17ha3b6e8fd82cf9e0dE,@function
_ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17ha3b6e8fd82cf9e0dE:
.Lfunc_begin75:
.cfi_startproc
pushq %rbp
.Ltmp308:
.cfi_def_cfa_offset 16
.Ltmp309:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp310:
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.Ltmp311:
.cfi_offset %rbx, -24
movq %rdi, %rbx
.loc 10 678 0
cmpq $256, %rdx
jne .LBB75_1
.loc 4 717 0
leaq 1(%rbx), %rdi
movl $256, %edx
callq memcpy@PLT
xorl %eax, %eax
jmp .LBB75_3
.LBB75_1:
.loc 6 69 0
movq $1, 8(%rbx)
xorps %xmm0, %xmm0
.loc 7 297 0
movups %xmm0, 16(%rbx)
.loc 4 50 0
movq $256, 40(%rbx)
.loc 4 51 0
movq %rdx, 48(%rbx)
movb $0, 32(%rbx)
movb $1, %al
.LBB75_3:
.loc 4 717 0
movb %al, (%rbx)
.loc 4 718 0
movq %rbx, %rax
addq $8, %rsp
popq %rbx
popq %rbp
retq After: .section ".text._ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h99ac2fdb585a06a9E","ax",@progbits
.globl _ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h99ac2fdb585a06a9E
.align 16, 0x90
.type _ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h99ac2fdb585a06a9E,@function
_ZN78_$LT$bigint..hash..H2048$u20$as$u20$ethcore_ipc..binary..BinaryConvertable$GT$10from_bytes17h99ac2fdb585a06a9E:
.Lfunc_begin75:
.cfi_startproc
pushq %rbp
.Ltmp308:
.cfi_def_cfa_offset 16
.Ltmp309:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp310:
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.Ltmp311:
.cfi_offset %rbx, -24
movq %rdi, %rbx
.loc 10 678 0
cmpq $256, %rdx
jne .LBB75_1
.loc 4 723 0
leaq 1(%rbx), %rdi
movl $256, %edx
callq memcpy@PLT
xorl %eax, %eax
jmp .LBB75_3
.LBB75_1:
.loc 6 69 0
movq $1, 8(%rbx)
xorps %xmm0, %xmm0
.loc 7 297 0
movups %xmm0, 16(%rbx)
.loc 4 49 0
movq $256, 40(%rbx)
.loc 4 50 0
movq %rdx, 48(%rbx)
movb $0, 32(%rbx)
movb $1, %al
.LBB75_3:
.loc 4 723 0
movb %al, (%rbx)
.loc 4 724 0
movq %rbx, %rax
addq $8, %rsp
popq %rbx
popq %rbp
retq |
@NikVolf As you can see the zeroing has been optimized out and has no effect on the produced code. |
Our safety guarantees around
Populatable
were really weak andBytesConvertable
just duplicatedAsRef<[u8]>
. I also cleaned up thefixed_binary_size
macro to require the type beCopy
, since again that's necessary for strong safety guarantees about not serializing types containing pointers over IPC.Goes after #2018