Skip to content

Commit

Permalink
Use QuerySingle* or ReadSingle* where possible to avoid allocating lists
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Jun 21, 2024
1 parent 962db19 commit 9157a1a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Hangfire.SqlServer/SqlServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public override JobData GetJobData(string id)
{
using (var multi = connection.QueryMultiple(sql, new { id = parsedId }, commandTimeout: _storage.CommandTimeout))
{
var jobData = multi.Read().SingleOrDefault();
var jobData = multi.ReadSingleOrDefault();
if (jobData == null) return null;

var parameters = multi.Read<JobParameter>()
Expand Down Expand Up @@ -285,7 +285,7 @@ public override StateData GetStateData(string jobId)

return _storage.UseConnection(_dedicatedConnection, connection =>
{
var sqlState = connection.Query<SqlState>(sql, new { jobId = parsedId }, commandTimeout: _storage.CommandTimeout).SingleOrDefault();
var sqlState = connection.QuerySingleOrDefault<SqlState>(sql, new { jobId = parsedId }, commandTimeout: _storage.CommandTimeout);
if (sqlState == null)
{
return null;
Expand Down
11 changes: 5 additions & 6 deletions src/Hangfire.SqlServer/SqlServerJobQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,11 @@ private SqlServerTimeoutJob FetchJob(string[] queues)
parameters.Add("@timeoutSs", (int)_options.SlidingInvisibilityTimeout.Value.Negate().TotalSeconds);

var fetchedJob = connection
.Query<FetchedJob>(
.QuerySingleOrDefault<FetchedJob>(
GetNonBlockingFetchSql(),
parameters,
commandTimeout: _storage.CommandTimeout)
.SingleOrDefault();

commandTimeout: _storage.CommandTimeout);

return fetchedJob != null
? new SqlServerTimeoutJob(_storage, fetchedJob.Id, fetchedJob.JobId.ToString(CultureInfo.InvariantCulture), fetchedJob.Queue, fetchedJob.FetchedAt.Value)
: null;
Expand Down Expand Up @@ -220,13 +219,13 @@ private SqlServerTransactionJob DequeueUsingTransaction(string[] queues, Cancell
{
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

fetchedJob = connection.Query<FetchedJob>(
fetchedJob = connection.QuerySingleOrDefault<FetchedJob>(
fetchJobSqlTemplate,
#pragma warning disable 618
new { queues = queues, timeout = _options.InvisibilityTimeout.Negate().TotalSeconds },
#pragma warning restore 618
transaction,
commandTimeout: _storage.CommandTimeout).SingleOrDefault();
commandTimeout: _storage.CommandTimeout);

if (fetchedJob != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.SqlServer/SqlServerJobQueueMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ select sum(Enqueued) as EnqueuedCount, sum(Fetched) as FetchedCount

return _storage.UseConnection(null, connection =>
{
var result = connection.Query(sqlQuery, new { queue = queue }).Single();
var result = connection.QuerySingle(sqlQuery, new { queue = queue });

return new EnqueuedAndFetchedCountDto
{
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.SqlServer/SqlServerMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public override JobDetailsDto JobDetails(string jobId)

using (var multi = connection.QueryMultiple(sql, new { id = jobId }, commandTimeout: _storage.CommandTimeout))
{
var job = multi.Read<SqlJob>().SingleOrDefault();
var job = multi.ReadSingleOrDefault<SqlJob>();
if (job == null) return null;

var parameters = multi.Read<JobParameter>()
Expand Down

0 comments on commit 9157a1a

Please sign in to comment.