@@ -31,15 +31,9 @@ fn sub(a: *const u8, b: *const u8) -> usize {
31
31
32
32
#[ target_feature( enable = "avx512f" , enable = "avx512bw" ) ]
33
33
#[ inline]
34
- pub unsafe fn escape_avx512 < S : AsRef < str > > ( input : S ) -> String {
35
- let s = input. as_ref ( ) ;
36
- let bytes = s. as_bytes ( ) ;
34
+ pub unsafe fn escape_avx512 ( bytes : & [ u8 ] , mut result : Vec < u8 > ) -> String {
37
35
let len = bytes. len ( ) ;
38
36
39
- // Pre-allocate with estimated capacity
40
- let estimated_capacity = len + len / 2 + 2 ;
41
- let mut result = Vec :: with_capacity ( estimated_capacity) ;
42
-
43
37
result. push ( b'"' ) ;
44
38
45
39
let start_ptr = bytes. as_ptr ( ) ;
@@ -237,7 +231,7 @@ pub unsafe fn escape_avx512<S: AsRef<str>>(input: S) -> String {
237
231
}
238
232
} else {
239
233
// Fall back to AVX2 for small strings
240
- return escape_avx2 ( input ) ;
234
+ return escape_avx2 ( bytes , result ) ;
241
235
}
242
236
243
237
// Copy any remaining bytes
@@ -251,15 +245,9 @@ pub unsafe fn escape_avx512<S: AsRef<str>>(input: S) -> String {
251
245
252
246
#[ target_feature( enable = "avx2" ) ]
253
247
#[ inline]
254
- pub unsafe fn escape_avx2 < S : AsRef < str > > ( input : S ) -> String {
255
- let s = input. as_ref ( ) ;
256
- let bytes = s. as_bytes ( ) ;
248
+ pub unsafe fn escape_avx2 ( bytes : & [ u8 ] , mut result : Vec < u8 > ) -> String {
257
249
let len = bytes. len ( ) ;
258
250
259
- // Pre-allocate with estimated capacity
260
- let estimated_capacity = len + len / 2 + 2 ;
261
- let mut result = Vec :: with_capacity ( estimated_capacity) ;
262
-
263
251
result. push ( b'"' ) ;
264
252
265
253
let start_ptr = bytes. as_ptr ( ) ;
@@ -477,7 +465,7 @@ pub unsafe fn escape_avx2<S: AsRef<str>>(input: S) -> String {
477
465
}
478
466
} else {
479
467
// Fall back to SSE2 for small strings
480
- return escape_sse2 ( input ) ;
468
+ return escape_sse2 ( bytes , result ) ;
481
469
}
482
470
483
471
// Copy any remaining bytes
@@ -491,14 +479,9 @@ pub unsafe fn escape_avx2<S: AsRef<str>>(input: S) -> String {
491
479
492
480
#[ target_feature( enable = "sse2" ) ]
493
481
#[ inline]
494
- pub unsafe fn escape_sse2 < S : AsRef < str > > ( input : S ) -> String {
495
- let s = input. as_ref ( ) ;
496
- let bytes = s. as_bytes ( ) ;
482
+ pub unsafe fn escape_sse2 ( bytes : & [ u8 ] , mut result : Vec < u8 > ) -> String {
497
483
let len = bytes. len ( ) ;
498
484
499
- let estimated_capacity = len + len / 2 + 2 ;
500
- let mut result = Vec :: with_capacity ( estimated_capacity) ;
501
-
502
485
result. push ( b'"' ) ;
503
486
504
487
let start_ptr = bytes. as_ptr ( ) ;
0 commit comments