Skip to content

Commit 425ae5a

Browse files
committed
Avoid dictionary allocation in observable metric callbacks
Change GetMetrics() to return IEnumerable of tuples via Select instead of allocating a Dictionary via ToDictionary on every collection cycle. Also simplify GetConnectionMax to use Select instead of SelectMany.
1 parent 43d9462 commit 425ae5a

2 files changed

Lines changed: 11 additions & 17 deletions

File tree

src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionPoolManager.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,8 @@ internal void ClearPool(ConnectionString connectionString)
224224
}
225225
}
226226

227-
internal Dictionary<string, (int idleCount, int busyCount, int maxSize)> GetMetrics() =>
228-
_pools.ToDictionary(
229-
kvp => kvp.Key,
230-
kvp => (kvp.Value.AvailableCount, kvp.Value.BusyCount, kvp.Value.MaxSize)
231-
);
227+
internal IEnumerable<(string poolName, int idleCount, int busyCount, int maxSize)> GetMetrics() =>
228+
_pools.Select(kvp => (kvp.Key, kvp.Value.AvailableCount, kvp.Value.BusyCount, kvp.Value.MaxSize));
232229

233230
public void Dispose()
234231
{

src/FirebirdSql.Data.FirebirdClient/Metrics/FbMetricsStore.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,26 @@ internal static void ConnectionOpened(long startedAtTicks, string poolName)
9797

9898
static IEnumerable<Measurement<int>> GetConnectionCount() =>
9999
FbConnectionPoolManager.Instance.GetMetrics()
100-
.SelectMany(kvp => new List<Measurement<int>>
100+
.SelectMany(m => new List<Measurement<int>>
101101
{
102102
new(
103-
kvp.Value.idleCount,
104-
new(ConnectionPoolNameAttributeName, kvp.Key),
103+
m.idleCount,
104+
new(ConnectionPoolNameAttributeName, m.poolName),
105105
new(ConnectionStateAttributeName, ConnectionStateIdleValue)
106106
),
107107

108108
new(
109-
kvp.Value.busyCount,
110-
new(ConnectionPoolNameAttributeName, kvp.Key),
109+
m.busyCount,
110+
new(ConnectionPoolNameAttributeName, m.poolName),
111111
new(ConnectionStateAttributeName, ConnectionStateUsedValue)
112112
),
113113
});
114114

115115
static IEnumerable<Measurement<int>> GetConnectionMax() =>
116116
FbConnectionPoolManager.Instance.GetMetrics()
117-
.SelectMany(kvp => new List<Measurement<int>>
118-
{
119-
new(
120-
kvp.Value.maxSize,
121-
[new(ConnectionPoolNameAttributeName, kvp.Key)]
122-
),
123-
});
117+
.Select(m => new Measurement<int>(
118+
m.maxSize,
119+
[new(ConnectionPoolNameAttributeName, m.poolName)]
120+
));
124121
}
125122
}

0 commit comments

Comments
 (0)