Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advantage performance of Fetch/FetchAsync #1121

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
Expand All @@ -19,6 +19,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using FirebirdSql.Data.Common;
Expand All @@ -36,7 +37,7 @@ internal class GdsStatement : StatementBase
protected Descriptor _parameters;
protected Descriptor _fields;
protected bool _allRowsFetched;
private Queue<DbValue[]> _rows;
private Queue<object[]> _rows;
private int _fetchSize;

#endregion
Expand Down Expand Up @@ -110,7 +111,8 @@ public GdsStatement(GdsDatabase database, GdsTransaction transaction)
{
_handle = IscCodes.INVALID_OBJECT;
_fetchSize = 200;
_rows = new Queue<DbValue[]>();
//_rows = new Queue<DbValue[]>();
_rows = new Queue<object[]>(); // optimized
OutputParameters = new Queue<DbValue[]>();

_database = database;
Expand Down Expand Up @@ -375,6 +377,26 @@ public override async ValueTask ExecuteAsync(int timeout, IDescriptorFiller desc
}
}

protected DbValue[] _dbValues; // optimized
protected void CreateDbValues()
{
_dbValues = new DbValue[_fields.Count];
for (int i = 0; i < _fields.Count; i++)
{
_dbValues[i] = new DbValue(this, _fields[i], null);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected DbValue[] GetDbValues(object[] values)
{
for (int i=0; i < _fields.Count; ++i)
{
_dbValues[i].SetValue(values[i]);
}
return _dbValues;
}

public override DbValue[] Fetch()
{
EnsureNotDeallocated();
Expand Down Expand Up @@ -407,14 +429,14 @@ public override DbValue[] Fetch()
var operation = _database.ReadOperation();
if (operation == IscCodes.op_fetch_response)
{
CreateDbValues();

var hasOperation = true;
while (!_allRowsFetched)
{
var response = hasOperation
if ((hasOperation
? _database.ReadResponse(operation)
: _database.ReadResponse();
hasOperation = false;
if (response is FetchResponse fetchResponse)
: _database.ReadResponse()) is FetchResponse fetchResponse)
{
if (fetchResponse.Count > 0 && fetchResponse.Status == 0)
{
Expand All @@ -433,6 +455,7 @@ public override DbValue[] Fetch()
{
break;
}
hasOperation = false;
}
}
else
Expand All @@ -448,7 +471,7 @@ public override DbValue[] Fetch()

if (_rows != null && _rows.Count > 0)
{
return _rows.Dequeue();
return GetDbValues(_rows.Dequeue());
}
else
{
Expand Down Expand Up @@ -488,14 +511,13 @@ public override async ValueTask<DbValue[]> FetchAsync(CancellationToken cancella
var operation = await _database.ReadOperationAsync(cancellationToken).ConfigureAwait(false);
if (operation == IscCodes.op_fetch_response)
{
CreateDbValues();
var hasOperation = true;
while (!_allRowsFetched)
{
var response = hasOperation
if ((hasOperation
? await _database.ReadResponseAsync(operation, cancellationToken).ConfigureAwait(false)
: await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
hasOperation = false;
if (response is FetchResponse fetchResponse)
: await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false)) is FetchResponse fetchResponse)
{
if (fetchResponse.Count > 0 && fetchResponse.Status == 0)
{
Expand All @@ -514,6 +536,7 @@ public override async ValueTask<DbValue[]> FetchAsync(CancellationToken cancella
{
break;
}
hasOperation = false;
}
}
else
Expand All @@ -529,7 +552,7 @@ public override async ValueTask<DbValue[]> FetchAsync(CancellationToken cancella

if (_rows != null && _rows.Count > 0)
{
return _rows.Dequeue();
return GetDbValues(_rows.Dequeue());
}
else
{
Expand Down Expand Up @@ -850,7 +873,8 @@ protected void ProcessStoredProcedureExecuteResponse(SqlResponse response)
{
if (response.Count > 0)
{
OutputParameters.Enqueue(ReadRow());
CreateDbValues();
OutputParameters.Enqueue(GetDbValues(ReadRow()));
}
}
catch (IOException ex)
Expand All @@ -864,7 +888,8 @@ protected async ValueTask ProcessStoredProcedureExecuteResponseAsync(SqlResponse
{
if (response.Count > 0)
{
OutputParameters.Enqueue(await ReadRowAsync(cancellationToken).ConfigureAwait(false));
CreateDbValues();
OutputParameters.Enqueue(GetDbValues(await ReadRowAsync(cancellationToken).ConfigureAwait(false)));
}
}
catch (IOException ex)
Expand Down Expand Up @@ -1735,9 +1760,9 @@ protected void ClearAll()
_fields = null;
}

protected virtual DbValue[] ReadRow()
protected virtual object[] ReadRow()
{
var row = new DbValue[_fields.Count];
var row = new object[_fields.Count];
try
{
for (var i = 0; i < _fields.Count; i++)
Expand All @@ -1746,11 +1771,11 @@ protected virtual DbValue[] ReadRow()
var sqlInd = _database.Xdr.ReadInt32();
if (sqlInd == -1)
{
row[i] = new DbValue(this, _fields[i], null);
row[i] = null;
}
else if (sqlInd == 0)
{
row[i] = new DbValue(this, _fields[i], value);
row[i] = value;
}
else
{
Expand All @@ -1764,9 +1789,11 @@ protected virtual DbValue[] ReadRow()
}
return row;
}
protected virtual async ValueTask<DbValue[]> ReadRowAsync(CancellationToken cancellationToken = default)


protected virtual async ValueTask<object[]> ReadRowAsync(CancellationToken cancellationToken = default)
{
var row = new DbValue[_fields.Count];
var row = new object[_fields.Count];
try
{
for (var i = 0; i < _fields.Count; i++)
Expand All @@ -1775,11 +1802,11 @@ protected virtual async ValueTask<DbValue[]> ReadRowAsync(CancellationToken canc
var sqlInd = await _database.Xdr.ReadInt32Async(cancellationToken).ConfigureAwait(false);
if (sqlInd == -1)
{
row[i] = new DbValue(this, _fields[i], null);
row[i] = null;
}
else if (sqlInd == 0)
{
row[i] = new DbValue(this, _fields[i], value);
row[i] = value;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
Expand Down Expand Up @@ -131,9 +131,9 @@ protected override async ValueTask<byte[]> WriteParametersAsync(CancellationToke
}
}

protected override DbValue[] ReadRow()
protected override object[] ReadRow()
{
var row = new DbValue[_fields.Count];
var row = new object[_fields.Count];
try
{
if (_fields.Count > 0)
Expand All @@ -144,12 +144,11 @@ protected override DbValue[] ReadRow()
{
if (nullBits.Get(i))
{
row[i] = new DbValue(this, _fields[i], null);
row[i] = null;
}
else
{
var value = ReadRawValue(_database.Xdr, _fields[i]);
row[i] = new DbValue(this, _fields[i], value);
row[i] = ReadRawValue(_database.Xdr, _fields[i]);
}
}
}
Expand All @@ -160,9 +159,10 @@ protected override DbValue[] ReadRow()
}
return row;
}
protected override async ValueTask<DbValue[]> ReadRowAsync(CancellationToken cancellationToken = default)

protected override async ValueTask<object[]> ReadRowAsync(CancellationToken cancellationToken = default)
{
var row = new DbValue[_fields.Count];
var row = new object[_fields.Count];
try
{
if (_fields.Count > 0)
Expand All @@ -173,12 +173,11 @@ protected override async ValueTask<DbValue[]> ReadRowAsync(CancellationToken can
{
if (nullBits.Get(i))
{
row[i] = new DbValue(this, _fields[i], null);
row[i] = null;
}
else
{
var value = await ReadRawValueAsync(_database.Xdr, _fields[i], cancellationToken).ConfigureAwait(false);
row[i] = new DbValue(this, _fields[i], value);
row[i] = await ReadRawValueAsync(_database.Xdr, _fields[i], cancellationToken).ConfigureAwait(false);
}
}
}
Expand Down