Skip to content

Conversation

@GrabYourPitchforks
Copy link
Member

The usage of GetArrayDataReference in HttpHeaders is unnecessary. Since the JIT can prove that accessing the 0th element of the array will not result in an IndexOutOfBoundsException (see the newarr two lines above the changed code), standard array access syntax already avoids the bounds check. There's no need for unsafe code here.

x64 before:

    L00a2: test rdi, rdi // if (store is null)
    L00a5: jne short L00f3
    L00a7: mov r14d, [rbx+0x10]
    L00ab: lea ecx, [r14+1]
    L00af: mov [rbx+0x10], ecx // count++;
    L00b2: mov rcx, 0x7ffe3ea6d520
    L00bc: mov edx, 4
    L00c1: call 0x00007ffe46666610 // newarr
    L00c6: mov rbp, rax
    L00c9: lea rcx, [rbx+8]
    L00cd: mov rdx, rbp
    L00d0: call 0x00007ffde6960010 // stfld (_headerStore)
    L00d5: add rbp, 0x10 // addrof newArr[0]
    L00d9: lea rcx, [rbp+8] // addrof firstEntry.Key
    L00dd: mov rdx, rsi
    L00e0: call 0x00007ffde6960010 // stfld (firstEntry.Key)
    L00e5: mov rax, rbp // return ref firstEntry.Value
    L00e8: add rsp, 0x20
    L00ec: pop rbx
    L00ed: pop rbp
    L00ee: pop rsi
    L00ef: pop rdi
    L00f0: pop r14
    L00f2: ret

x64 after:

    L009f: test rdi, rdi // if (store is null)
    L00a2: jne short L00ed
    L00a4: mov r14d, [rbx+0x10]
    L00a8: lea ecx, [r14+1]
    L00ac: mov [rbx+0x10], ecx // count++;
    L00af: mov rcx, 0x7ffe3eafd510
    L00b9: mov edx, 4
    L00be: call 0x00007ffe46666610 // newarr
    L00c3: mov rbp, rax
    L00c6: lea rcx, [rbx+8]
    L00ca: mov rdx, rbp
    L00cd: call 0x00007ffde6960010 // stfld (_headerStore)
    L00d2: lea rcx, [rbp+0x18] // addrof entries[0].Key
    L00d6: mov rdx, rsi
    L00d9: call 0x00007ffde6960010 // stfld (entries[0].Key)
    L00de: lea rax, [rbp+0x10] // return ref entries[0].Value
    L00e2: add rsp, 0x20
    L00e6: pop rbx
    L00e7: pop rbp
    L00e8: pop rsi
    L00e9: pop rdi
    L00ea: pop r14
    L00ec: ret

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR simplifies HttpHeaders by replacing unnecessary unsafe code with standard array indexing. Since the JIT can eliminate bounds checks when accessing the 0th element immediately after array allocation (via newarr), using MemoryMarshal.GetArrayDataReference provides no performance benefit and adds unnecessary complexity.

Key Changes

  • Replaced MemoryMarshal.GetArrayDataReference(entries) with direct array indexing entries[0]
  • Eliminated unsafe code while maintaining equivalent performance and semantics

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@MihaZupan MihaZupan enabled auto-merge (squash) December 3, 2025 08:49
@MihaZupan MihaZupan merged commit 4d1048d into dotnet:main Dec 3, 2025
94 of 98 checks passed
@MihaZupan MihaZupan added this to the 11.0.0 milestone Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants