2929
3030using System ;
3131using System . Text ;
32+ using System . Buffers ;
3233using System . Collections ;
3334using System . Collections . Generic ;
3435
@@ -96,7 +97,7 @@ public IDataCommand Script(string driver, DataCommandMutability mutability, stri
9697 ArgumentException . ThrowIfNullOrEmpty ( driver ) ;
9798 ArgumentException . ThrowIfNullOrEmpty ( script ) ;
9899
99- var key = $ "# { Convert . ToHexString ( System . Security . Cryptography . SHA1 . HashData ( Encoding . UTF8 . GetBytes ( $ "{ driver . ToUpperInvariant ( ) } :{ script } ") ) ) } " ;
100+ var key = GetKey ( $ "{ driver . ToUpperInvariant ( ) } :{ script } ") ;
100101
101102 return _dictionary . GetOrAdd ( key , ( key , argument ) =>
102103 {
@@ -110,6 +111,38 @@ public IDataCommand Script(string driver, DataCommandMutability mutability, stri
110111
111112 return command ;
112113 } , ( driver , mutability , script , parameters ) ) ;
114+
115+ static string GetKey ( string text )
116+ {
117+ var count = Encoding . UTF8 . GetMaxByteCount ( text . Length ) ;
118+
119+ if ( count <= 1024 )
120+ {
121+ Span < byte > data = stackalloc byte [ count ] ;
122+ var size = Encoding . UTF8 . GetBytes ( text , data ) ;
123+ return Hash ( data [ .. size ] ) ;
124+ }
125+
126+ var bytes = ArrayPool < byte > . Shared . Rent ( count ) ;
127+
128+ try
129+ {
130+ var data = bytes . AsSpan ( 0 , count ) ;
131+ var size = Encoding . UTF8 . GetBytes ( text , data ) ;
132+ return Hash ( data [ .. size ] ) ;
133+ }
134+ finally
135+ {
136+ ArrayPool < byte > . Shared . Return ( bytes ) ;
137+ }
138+
139+ static string Hash ( ReadOnlySpan < byte > data )
140+ {
141+ Span < byte > code = stackalloc byte [ System . Security . Cryptography . SHA1 . HashSizeInBytes ] ;
142+ System . Security . Cryptography . SHA1 . HashData ( data , code ) ;
143+ return $ "#{ Convert . ToHexString ( code ) } ";
144+ }
145+ }
113146 }
114147 #endregion
115148
0 commit comments