Skip to content

Commit 694dd4f

Browse files
authored
Compress SafeSocketHandle.Unix booleans (#112417)
Reduces the logical size of `SafeSocketHandle` by 6-7 bytes on Unix, crossing a treshold that lowers the number of words consumed by the type by 1 on 64bit
1 parent 5bd5ae4 commit 694dd4f

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Unix.cs

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,70 @@ public partial class SafeSocketHandle
1414
{
1515
private int _receiveTimeout = -1;
1616
private int _sendTimeout = -1;
17-
private bool _nonBlocking;
1817
private SocketAsyncContext? _asyncContext;
1918

20-
internal bool LastConnectFailed { get; set; }
21-
internal bool DualMode { get; set; }
22-
internal bool ExposedHandleOrUntrackedConfiguration { get; private set; }
23-
internal bool PreferInlineCompletions { get; set; } = SocketAsyncEngine.InlineSocketCompletionsEnabled;
24-
internal bool IsSocket { get; set; } = true; // (ab)use Socket class for performing async I/O on non-socket fds.
19+
[Flags]
20+
private enum Flags : byte
21+
{
22+
NonBlocking = 1,
23+
LastConnectFailed = 2,
24+
DualMode = 4,
25+
ExposedHandleOrUntrackedConfiguration = 8,
26+
PreferInlineCompletions = 16,
27+
IsSocket = 32,
28+
IsDisconnected = 64,
2529
#if SYSTEM_NET_SOCKETS_APPLE_PLATFROM
26-
internal bool TfoEnabled { get; set; }
30+
TfoEnabled = 128
2731
#endif
32+
}
33+
34+
private Flags _flags = Flags.IsSocket | (SocketAsyncEngine.InlineSocketCompletionsEnabled ? Flags.PreferInlineCompletions : 0);
35+
36+
private void SetFlag(Flags flag, bool value)
37+
{
38+
if (value) _flags |= flag;
39+
else _flags &= ~flag;
40+
}
41+
42+
internal bool LastConnectFailed
43+
{
44+
get => (_flags & Flags.LastConnectFailed) != 0;
45+
set => SetFlag(Flags.LastConnectFailed, value);
46+
}
47+
48+
internal bool DualMode
49+
{
50+
get => (_flags & Flags.DualMode) != 0;
51+
set => SetFlag(Flags.DualMode, value);
52+
}
53+
54+
internal bool ExposedHandleOrUntrackedConfiguration
55+
{
56+
get => (_flags & Flags.ExposedHandleOrUntrackedConfiguration) != 0;
57+
private set => SetFlag(Flags.ExposedHandleOrUntrackedConfiguration, value);
58+
}
59+
60+
internal bool PreferInlineCompletions
61+
{
62+
get => (_flags & Flags.PreferInlineCompletions) != 0;
63+
set => SetFlag(Flags.PreferInlineCompletions, value);
64+
}
65+
66+
// (ab)use Socket class for performing async I/O on non-socket fds.
67+
internal bool IsSocket
68+
{
69+
get => (_flags & Flags.IsSocket) != 0;
70+
set => SetFlag(Flags.IsSocket, value);
71+
}
72+
73+
#if SYSTEM_NET_SOCKETS_APPLE_PLATFROM
74+
internal bool TfoEnabled
75+
{
76+
get => (_flags & Flags.TfoEnabled) != 0;
77+
set => SetFlag(Flags.TfoEnabled, value);
78+
}
79+
#endif
80+
2881
internal void RegisterConnectResult(SocketError error)
2982
{
3083
switch (error)
@@ -67,11 +120,11 @@ internal bool IsNonBlocking
67120
{
68121
get
69122
{
70-
return _nonBlocking;
123+
return (_flags & Flags.NonBlocking) != 0;
71124
}
72125
set
73126
{
74-
_nonBlocking = value;
127+
SetFlag(Flags.NonBlocking, value);
75128

76129
// If transitioning from blocking to non-blocking, we need to set the native socket to non-blocking mode.
77130
// If transitioning from non-blocking to blocking, we keep the native socket in non-blocking mode, and emulate
@@ -112,12 +165,9 @@ internal int SendTimeout
112165
}
113166
}
114167

115-
internal bool IsDisconnected { get; private set; }
168+
internal bool IsDisconnected => (_flags & Flags.IsDisconnected) != 0;
116169

117-
internal void SetToDisconnected()
118-
{
119-
IsDisconnected = true;
120-
}
170+
internal void SetToDisconnected() => _flags |= Flags.IsDisconnected;
121171

122172
/// <returns>Returns whether operations were canceled.</returns>
123173
private bool OnHandleClose()

0 commit comments

Comments
 (0)