|
15 | 15 | using System.Transactions; |
16 | 16 | using Microsoft.Extensions.Logging; |
17 | 17 | using Npgsql.Internal; |
18 | | -using Npgsql.NameTranslation; |
19 | 18 | using Npgsql.TypeMapping; |
20 | 19 | using Npgsql.Util; |
21 | | -using NpgsqlTypes; |
22 | 20 | using IsolationLevel = System.Data.IsolationLevel; |
23 | 21 |
|
24 | 22 | namespace Npgsql; |
@@ -92,6 +90,8 @@ internal NpgsqlDataSource NpgsqlDataSource |
92 | 90 | public INpgsqlTypeMapper TypeMapper |
93 | 91 | => throw new NotSupportedException(); |
94 | 92 |
|
| 93 | + static ICloningInstantiator? _cloningInstantiator; |
| 94 | + |
95 | 95 | /// <summary> |
96 | 96 | /// The default TCP/IP port for PostgreSQL. |
97 | 97 | /// </summary> |
@@ -219,6 +219,9 @@ void SetupDataSource() |
219 | 219 | dataSourceBuilder.EnableParameterLogging(NpgsqlLoggingConfiguration.GlobalIsParameterLoggingEnabled); |
220 | 220 | var newDataSource = dataSourceBuilder.Build(); |
221 | 221 |
|
| 222 | + // See Clone() on the following line: |
| 223 | + _cloningInstantiator = new CloningInstantiator(); |
| 224 | + |
222 | 225 | _dataSource = PoolManager.Pools.GetOrAdd(canonical, newDataSource); |
223 | 226 | if (_dataSource == newDataSource) |
224 | 227 | { |
@@ -1817,8 +1820,14 @@ object ICloneable.Clone() |
1817 | 1820 | { |
1818 | 1821 | CheckDisposed(); |
1819 | 1822 |
|
| 1823 | + // For NativeAOT code size reduction, we avoid instantiating a connection here directly with |
| 1824 | + // `new NpgsqlConnection(_connectionString)`, since that would bring in the default data source builder, and with it various |
| 1825 | + // features which significantly increase binary size (ranges, System.Text.Json...). Instead, we pass through a "cloning |
| 1826 | + // instantiator" abstraction, where the implementation only ever gets set if SetupDataSource above is called (in which case the |
| 1827 | + // default data source is brought in anyway). |
| 1828 | + Debug.Assert(_dataSource is not null || _cloningInstantiator is not null); |
1820 | 1829 | var conn = _dataSource is null |
1821 | | - ? new NpgsqlConnection(_connectionString) |
| 1830 | + ? _cloningInstantiator!.Instantiate(_connectionString) |
1822 | 1831 | : _dataSource.CreateConnection(); |
1823 | 1832 |
|
1824 | 1833 | conn.ProvideClientCertificatesCallback = ProvideClientCertificatesCallback; |
@@ -1859,6 +1868,17 @@ public NpgsqlConnection CloneWith(string connectionString) |
1859 | 1868 | }; |
1860 | 1869 | } |
1861 | 1870 |
|
| 1871 | + interface ICloningInstantiator |
| 1872 | + { |
| 1873 | + public NpgsqlConnection Instantiate(string connectionString); |
| 1874 | + } |
| 1875 | + |
| 1876 | + sealed class CloningInstantiator : ICloningInstantiator |
| 1877 | + { |
| 1878 | + public NpgsqlConnection Instantiate(string connectionString) |
| 1879 | + => new(connectionString); |
| 1880 | + } |
| 1881 | + |
1862 | 1882 | /// <summary> |
1863 | 1883 | /// This method changes the current database by disconnecting from the actual |
1864 | 1884 | /// database and connecting to the specified. |
|
0 commit comments