Turn The Unfiltered Class Into A Struct #13

Closed
opened 2024-11-28 10:27:25 +00:00 by owner · 0 comments
Owner

A quick micro benchmark shows that we can completely eliminate allocation (and by extension GC) by splitting the IP string up into 4 ints.

[Benchmark]
public UnfilteredClass TestUnfilteredClass()
{
    UnfilteredClass unfiltered = new UnfilteredClass();
        
    for (int i = 0; i < 10000; i++)
    {
        unfiltered = new()
        {
            Id = 1,
            Filtered = 1,
            Ip = "127.0.0.1",
            Port1 = 80,
            Port2 = 443,
            ResponseCode = 1
        };
    }
        
        return unfiltered;
    }
    
[Benchmark]
public UnfilteredStruct TestUnfilteredStruct()
{
    UnfilteredStruct unfiltered = new UnfilteredStruct();
        
    for (int i = 0; i < 10000; i++)
    {
        unfiltered = new()
        {
            Id = 1,
            Filtered = 1,
            Ip1 = 127,
            Ip2 = 0,
            Ip3 = 0,
            Ip4 = 1,
            Port1 = 80,
            Port2 = 443,
            ResponseCode = 1
        };
    }
        
    return unfiltered;
}
A quick micro benchmark shows that we can completely eliminate allocation (and by extension GC) by splitting the IP string up into 4 ints. ```cs [Benchmark] public UnfilteredClass TestUnfilteredClass() { UnfilteredClass unfiltered = new UnfilteredClass(); for (int i = 0; i < 10000; i++) { unfiltered = new() { Id = 1, Filtered = 1, Ip = "127.0.0.1", Port1 = 80, Port2 = 443, ResponseCode = 1 }; } return unfiltered; } [Benchmark] public UnfilteredStruct TestUnfilteredStruct() { UnfilteredStruct unfiltered = new UnfilteredStruct(); for (int i = 0; i < 10000; i++) { unfiltered = new() { Id = 1, Filtered = 1, Ip1 = 127, Ip2 = 0, Ip3 = 0, Ip4 = 1, Port1 = 80, Port2 = 443, ResponseCode = 1 }; } return unfiltered; } ```
owner added the
enhancement
label 2024-11-28 10:27:25 +00:00
owner self-assigned this 2024-11-28 10:29:31 +00:00
owner added this to the Minimize GC Pressure milestone 2024-11-28 10:32:06 +00:00
owner added reference ReworkUnfilteredObject 2024-11-28 10:36:04 +00:00
owner closed this issue 2024-11-28 13:25:09 +00:00
Sign in to join this conversation.
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: owner/RSE#13
No description provided.