generic-array icon indicating copy to clipboard operation
generic-array copied to clipboard

add GenericArray::remove, remove_unchecked

Open Easyoakland opened this issue 1 year ago • 1 comments

Add new methods for removing an element from an arbitrary index. I'm surprised this wasn't already implemented, so maybe I missed it.

Here's what the assembly looks like for GenericArray::<u8,4>.remove_unchecked

Ignoring the removed element:

	sub rsp, 40
	mov r8, rdx

	mov dword ptr [rsp + 36], ecx

	lea rcx, [rsp + rdx]

	add rcx, 36

	add rdx, rsp

	add rdx, 37

	xor r8, 3

	call memmove

	mov eax, dword ptr [rsp + 36]

	add rsp, 40
	ret

Ignoring the remaining array:

	push rax

	mov dword ptr [rsp + 4], ecx

	movzx eax, byte ptr [rsp + rdx + 4]

	pop rcx

	ret

Using both the remaining array and element:

	push rsi
	sub rsp, 48
	mov r8, rdx

	mov dword ptr [rsp + 44], ecx

	lea rcx, [rsp + rdx]

	add rcx, 44

	add rdx, rsp

	add rdx, 45

	movzx esi, byte ptr [rsp + r8 + 44]

	xor r8, 3

	call memmove

	shl esi, 24
	mov eax, 16777215
	and eax, dword ptr [rsp + 44]
	or eax, esi

	add rsp, 48
	pop rsi
	ret

Easyoakland avatar Mar 14 '24 05:03 Easyoakland

Also, the added tests pass miri so I'm relatively confident the implementation is correct.

Easyoakland avatar Mar 14 '24 05:03 Easyoakland

I've implemented this for 1.1.0 along with a swap_remove in a new Remove trait built on GenericSequence. Codegen in my version is essentially identical.

novacrazy avatar Jul 05 '24 05:07 novacrazy