Skip to content

Commit 8643d8c

Browse files
committed
Sync code performance restored (#953, https://www.tabsoverspaces.com/id/233866).
1 parent 6c76509 commit 8643d8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+9982
-2267
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/Client/ClientFactory.cs

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
1717

1818
using System;
19+
using System.Threading;
1920
using System.Threading.Tasks;
2021
using FirebirdSql.Data.Client.Managed;
2122
using FirebirdSql.Data.Common;
@@ -26,37 +27,77 @@ namespace FirebirdSql.Data.Client
2627
{
2728
internal static class ClientFactory
2829
{
29-
public static async ValueTask<DatabaseBase> CreateDatabaseAsync(ConnectionString options, AsyncWrappingCommonArgs async)
30+
public static DatabaseBase CreateDatabase(ConnectionString options)
3031
{
3132
return options.ServerType switch
3233
{
33-
FbServerType.Default => await CreateManagedDatabaseAsync(options, async).ConfigureAwait(false),
34+
FbServerType.Default => CreateManagedDatabase(options),
35+
FbServerType.Embedded => new Native.FesDatabase(options.ClientLibrary, Charset.GetCharset(options.Charset)),
36+
_ => throw IncorrectServerTypeException(),
37+
};
38+
}
39+
public static async ValueTask<DatabaseBase> CreateDatabaseAsync(ConnectionString options, CancellationToken cancellationToken = default)
40+
{
41+
return options.ServerType switch
42+
{
43+
FbServerType.Default => await CreateManagedDatabaseAsync(options, cancellationToken).ConfigureAwait(false),
3444
FbServerType.Embedded => new Native.FesDatabase(options.ClientLibrary, Charset.GetCharset(options.Charset)),
3545
_ => throw IncorrectServerTypeException(),
3646
};
3747
}
3848

39-
public static async ValueTask<ServiceManagerBase> CreateServiceManagerAsync(ConnectionString options, AsyncWrappingCommonArgs async)
49+
public static ServiceManagerBase CreateServiceManager(ConnectionString options)
50+
{
51+
return options.ServerType switch
52+
{
53+
FbServerType.Default => CreateManagedServiceManager(options),
54+
FbServerType.Embedded => new Native.FesServiceManager(options.ClientLibrary, Charset.GetCharset(options.Charset)),
55+
_ => throw IncorrectServerTypeException(),
56+
};
57+
}
58+
public static async ValueTask<ServiceManagerBase> CreateServiceManagerAsync(ConnectionString options, CancellationToken cancellationToken = default)
4059
{
4160
return options.ServerType switch
4261
{
43-
FbServerType.Default => await CreateManagedServiceManagerAsync(options, async).ConfigureAwait(false),
62+
FbServerType.Default => await CreateManagedServiceManagerAsync(options, cancellationToken).ConfigureAwait(false),
4463
FbServerType.Embedded => new Native.FesServiceManager(options.ClientLibrary, Charset.GetCharset(options.Charset)),
4564
_ => throw IncorrectServerTypeException(),
4665
};
4766
}
4867

49-
private static async ValueTask<DatabaseBase> CreateManagedDatabaseAsync(ConnectionString options, AsyncWrappingCommonArgs async)
68+
private static DatabaseBase CreateManagedDatabase(ConnectionString options)
5069
{
5170
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, Charset.GetCharset(options.Charset), options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt));
52-
await connection.ConnectAsync(async).ConfigureAwait(false);
71+
connection.Connect();
5372
try
5473
{
55-
await connection.IdentifyAsync(options.Database, async).ConfigureAwait(false);
74+
connection.Identify(options.Database);
5675
}
5776
catch
5877
{
59-
await connection.DisconnectAsync(async).ConfigureAwait(false);
78+
connection.Disconnect();
79+
throw;
80+
}
81+
return connection.ProtocolVersion switch
82+
{
83+
IscCodes.PROTOCOL_VERSION13 => new Managed.Version13.GdsDatabase(connection),
84+
IscCodes.PROTOCOL_VERSION12 => new Managed.Version12.GdsDatabase(connection),
85+
IscCodes.PROTOCOL_VERSION11 => new Managed.Version11.GdsDatabase(connection),
86+
IscCodes.PROTOCOL_VERSION10 => new Managed.Version10.GdsDatabase(connection),
87+
_ => throw UnsupportedProtocolException(),
88+
};
89+
}
90+
private static async ValueTask<DatabaseBase> CreateManagedDatabaseAsync(ConnectionString options, CancellationToken cancellationToken = default)
91+
{
92+
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, Charset.GetCharset(options.Charset), options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt));
93+
await connection.ConnectAsync(cancellationToken).ConfigureAwait(false);
94+
try
95+
{
96+
await connection.IdentifyAsync(options.Database, cancellationToken).ConfigureAwait(false);
97+
}
98+
catch
99+
{
100+
await connection.DisconnectAsync(cancellationToken).ConfigureAwait(false);
60101
throw;
61102
}
62103
return connection.ProtocolVersion switch
@@ -69,17 +110,17 @@ private static async ValueTask<DatabaseBase> CreateManagedDatabaseAsync(Connecti
69110
};
70111
}
71112

72-
private static async ValueTask<ServiceManagerBase> CreateManagedServiceManagerAsync(ConnectionString options, AsyncWrappingCommonArgs async)
113+
private static ServiceManagerBase CreateManagedServiceManager(ConnectionString options)
73114
{
74115
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, Charset.GetCharset(options.Charset), options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt));
75-
await connection.ConnectAsync(async).ConfigureAwait(false);
116+
connection.Connect();
76117
try
77118
{
78-
await connection.IdentifyAsync(!string.IsNullOrEmpty(options.Database) ? options.Database : string.Empty, async).ConfigureAwait(false);
119+
connection.Identify(!string.IsNullOrEmpty(options.Database) ? options.Database : string.Empty);
79120
}
80121
catch
81122
{
82-
await connection.DisconnectAsync(async).ConfigureAwait(false);
123+
connection.Disconnect();
83124
throw;
84125
}
85126
return connection.ProtocolVersion switch
@@ -91,8 +132,30 @@ private static async ValueTask<ServiceManagerBase> CreateManagedServiceManagerAs
91132
_ => throw UnsupportedProtocolException(),
92133
};
93134
}
94-
95-
private static NotSupportedException UnsupportedProtocolException()
135+
private static async ValueTask<ServiceManagerBase> CreateManagedServiceManagerAsync(ConnectionString options, CancellationToken cancellationToken = default)
136+
{
137+
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, Charset.GetCharset(options.Charset), options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt));
138+
await connection.ConnectAsync(cancellationToken).ConfigureAwait(false);
139+
try
140+
{
141+
await connection.IdentifyAsync(!string.IsNullOrEmpty(options.Database) ? options.Database : string.Empty, cancellationToken).ConfigureAwait(false);
142+
}
143+
catch
144+
{
145+
await connection.DisconnectAsync(cancellationToken).ConfigureAwait(false);
146+
throw;
147+
}
148+
return connection.ProtocolVersion switch
149+
{
150+
IscCodes.PROTOCOL_VERSION13 => new Managed.Version13.GdsServiceManager(connection),
151+
IscCodes.PROTOCOL_VERSION12 => new Managed.Version12.GdsServiceManager(connection),
152+
IscCodes.PROTOCOL_VERSION11 => new Managed.Version11.GdsServiceManager(connection),
153+
IscCodes.PROTOCOL_VERSION10 => new Managed.Version10.GdsServiceManager(connection),
154+
_ => throw UnsupportedProtocolException(),
155+
};
156+
}
157+
158+
private static Exception UnsupportedProtocolException()
96159
{
97160
return new NotSupportedException("Protocol not supported.");
98161
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/AuthBlock.cs

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.IO;
2020
using System.Net;
2121
using System.Text;
22+
using System.Threading;
2223
using System.Threading.Tasks;
2324
using FirebirdSql.Data.Client.Managed.Version13;
2425
using FirebirdSql.Data.Common;
@@ -123,21 +124,53 @@ public byte[] UserIdentificationData()
123124
}
124125
}
125126

126-
public async ValueTask SendContAuthToBufferAsync(IXdrWriter xdr, AsyncWrappingCommonArgs async)
127+
public void SendContAuthToBuffer(IXdrWriter xdr)
127128
{
128-
await xdr.WriteAsync(IscCodes.op_cont_auth, async).ConfigureAwait(false);
129-
await xdr.WriteBufferAsync(ClientData, async).ConfigureAwait(false); // p_data
130-
await xdr.WriteAsync(AcceptPluginName, async).ConfigureAwait(false); // p_name
131-
await xdr.WriteAsync(AcceptPluginName, async).ConfigureAwait(false); // p_list
132-
await xdr.WriteBufferAsync(ServerKeys, async).ConfigureAwait(false); // p_keys
129+
xdr.Write(IscCodes.op_cont_auth);
130+
xdr.WriteBuffer(ClientData); // p_data
131+
xdr.Write(AcceptPluginName); // p_name
132+
xdr.Write(AcceptPluginName); // p_list
133+
xdr.WriteBuffer(ServerKeys); // p_keys
134+
}
135+
public async ValueTask SendContAuthToBufferAsync(IXdrWriter xdr, CancellationToken cancellationToken = default)
136+
{
137+
await xdr.WriteAsync(IscCodes.op_cont_auth, cancellationToken).ConfigureAwait(false);
138+
await xdr.WriteBufferAsync(ClientData, cancellationToken).ConfigureAwait(false); // p_data
139+
await xdr.WriteAsync(AcceptPluginName, cancellationToken).ConfigureAwait(false); // p_name
140+
await xdr.WriteAsync(AcceptPluginName, cancellationToken).ConfigureAwait(false); // p_list
141+
await xdr.WriteBufferAsync(ServerKeys, cancellationToken).ConfigureAwait(false); // p_keys
133142
}
134143

135144
// TODO: maybe more logic can be pulled up here
136-
public async ValueTask<IResponse> ProcessContAuthResponseAsync(IXdrReader xdr, AsyncWrappingCommonArgs async)
145+
public IResponse ProcessContAuthResponse(IXdrReader xdr)
137146
{
138-
var operation = await xdr.ReadOperationAsync(async).ConfigureAwait(false);
139-
var response = await GdsConnection.ProcessOperationAsync(operation, xdr, async).ConfigureAwait(false);
140-
GdsConnection.ProcessResponse(response);
147+
var operation = xdr.ReadOperation();
148+
var response = GdsConnection.ProcessOperation(operation, xdr);
149+
GdsConnection.HandleResponseException(response);
150+
if (response is ContAuthResponse)
151+
{
152+
return response;
153+
}
154+
else if (response is CryptKeyCallbackResponse)
155+
{
156+
return response;
157+
}
158+
else if (response is GenericResponse genericResponse)
159+
{
160+
ServerKeys = genericResponse.Data;
161+
Complete();
162+
}
163+
else
164+
{
165+
throw new InvalidOperationException($"Unexpected response ({operation}).");
166+
}
167+
return response;
168+
}
169+
public async ValueTask<IResponse> ProcessContAuthResponseAsync(IXdrReader xdr, CancellationToken cancellationToken = default)
170+
{
171+
var operation = await xdr.ReadOperationAsync(cancellationToken).ConfigureAwait(false);
172+
var response = await GdsConnection.ProcessOperationAsync(operation, xdr, cancellationToken).ConfigureAwait(false);
173+
GdsConnection.HandleResponseException(response);
141174
if (response is ContAuthResponse)
142175
{
143176
return response;
@@ -158,26 +191,48 @@ public async ValueTask<IResponse> ProcessContAuthResponseAsync(IXdrReader xdr, A
158191
return response;
159192
}
160193

161-
public async ValueTask SendWireCryptToBufferAsync(IXdrWriter xdr, AsyncWrappingCommonArgs async)
194+
public void SendWireCryptToBuffer(IXdrWriter xdr)
162195
{
163196
if (WireCrypt == WireCryptOption.Disabled)
164197
return;
165198

166-
await xdr.WriteAsync(IscCodes.op_crypt, async).ConfigureAwait(false);
167-
await xdr.WriteAsync(FirebirdNetworkHandlingWrapper.EncryptionName, async).ConfigureAwait(false);
168-
await xdr.WriteAsync(SessionKeyName, async).ConfigureAwait(false);
199+
xdr.Write(IscCodes.op_crypt);
200+
xdr.Write(FirebirdNetworkHandlingWrapper.EncryptionName);
201+
xdr.Write(SessionKeyName);
169202
}
203+
public async ValueTask SendWireCryptToBufferAsync(IXdrWriter xdr, CancellationToken cancellationToken = default)
204+
{
205+
if (WireCrypt == WireCryptOption.Disabled)
206+
return;
207+
208+
await xdr.WriteAsync(IscCodes.op_crypt, cancellationToken).ConfigureAwait(false);
209+
await xdr.WriteAsync(FirebirdNetworkHandlingWrapper.EncryptionName, cancellationToken).ConfigureAwait(false);
210+
await xdr.WriteAsync(SessionKeyName, cancellationToken).ConfigureAwait(false);
211+
}
212+
213+
public void ProcessWireCryptResponse(IXdrReader xdr, GdsConnection connection)
214+
{
215+
if (WireCrypt == WireCryptOption.Disabled)
216+
return;
170217

171-
public async ValueTask ProcessWireCryptResponseAsync(IXdrReader xdr, GdsConnection connection, AsyncWrappingCommonArgs async)
218+
// after writing before reading
219+
connection.StartEncryption();
220+
221+
var response = GdsConnection.ProcessOperation(xdr.ReadOperation(), xdr);
222+
GdsConnection.HandleResponseException(response);
223+
224+
WireCryptInitialized = true;
225+
}
226+
public async ValueTask ProcessWireCryptResponseAsync(IXdrReader xdr, GdsConnection connection, CancellationToken cancellationToken = default)
172227
{
173228
if (WireCrypt == WireCryptOption.Disabled)
174229
return;
175230

176231
// after writing before reading
177232
connection.StartEncryption();
178233

179-
var response = await GdsConnection.ProcessOperationAsync(await xdr.ReadOperationAsync(async).ConfigureAwait(false), xdr, async).ConfigureAwait(false);
180-
GdsConnection.ProcessResponse(response);
234+
var response = await GdsConnection.ProcessOperationAsync(await xdr.ReadOperationAsync(cancellationToken).ConfigureAwait(false), xdr, cancellationToken).ConfigureAwait(false);
235+
GdsConnection.HandleResponseException(response);
181236

182237
WireCryptInitialized = true;
183238
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/DataProviderStreamWrapper.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
//$Authors = Jiri Cincura (jiri@cincura.net)
1717

1818
using System.IO;
19+
using System.Runtime.CompilerServices;
20+
using System.Threading;
1921
using System.Threading.Tasks;
20-
using FirebirdSql.Data.Common;
2122

2223
namespace FirebirdSql.Data.Client.Managed
2324
{
@@ -30,10 +31,37 @@ public DataProviderStreamWrapper(Stream stream)
3031
_stream = stream;
3132
}
3233

33-
public async ValueTask<int> ReadAsync(byte[] buffer, int offset, int count, AsyncWrappingCommonArgs async) => await async.AsyncSyncCall(_stream.ReadAsync, _stream.Read, buffer, offset, count).ConfigureAwait(false);
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public int Read(byte[] buffer, int offset, int count)
36+
{
37+
return _stream.Read(buffer, offset, count);
38+
}
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public ValueTask<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
41+
{
42+
return new ValueTask<int>(_stream.ReadAsync(buffer, offset, count));
43+
}
3444

35-
public async ValueTask WriteAsync(byte[] buffer, int offset, int count, AsyncWrappingCommonArgs async) => await async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, buffer, offset, count).ConfigureAwait(false);
45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46+
public void Write(byte[] buffer, int offset, int count)
47+
{
48+
_stream.Write(buffer, offset, count);
49+
}
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51+
public ValueTask WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
52+
{
53+
return new ValueTask(_stream.WriteAsync(buffer, offset, count));
54+
}
3655

37-
public async ValueTask FlushAsync(AsyncWrappingCommonArgs async) => await async.AsyncSyncCall(_stream.FlushAsync, _stream.Flush).ConfigureAwait(false);
56+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
57+
public void Flush()
58+
{
59+
_stream.Flush();
60+
}
61+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
62+
public ValueTask FlushAsync(CancellationToken cancellationToken = default)
63+
{
64+
return new ValueTask(_stream.FlushAsync());
65+
}
3866
}
3967
}

0 commit comments

Comments
 (0)