@@ -57,7 +57,6 @@ const {
5757 byteLengthUtf8,
5858 compare : _compare ,
5959 compareOffset,
60- createFromString,
6160 fill : bindingFill ,
6261 isAscii : bindingIsAscii ,
6362 isUtf8 : bindingIsUtf8 ,
@@ -148,11 +147,12 @@ const constants = ObjectDefineProperties({}, {
148147} ) ;
149148
150149Buffer . poolSize = 8 * 1024 ;
151- let poolSize , poolOffset , allocPool ;
150+ let poolSize , poolOffset , allocPool , allocBuffer ;
152151
153152function createPool ( ) {
154153 poolSize = Buffer . poolSize ;
155- allocPool = createUnsafeBuffer ( poolSize ) . buffer ;
154+ allocBuffer = createUnsafeBuffer ( poolSize ) ;
155+ allocPool = allocBuffer . buffer ;
156156 markAsUntransferable ( allocPool ) ;
157157 poolOffset = 0 ;
158158}
@@ -440,38 +440,49 @@ function allocate(size) {
440440}
441441
442442function fromStringFast ( string , ops ) {
443- const length = ops . byteLength ( string ) ;
443+ const maxLength = Buffer . poolSize >>> 1 ;
444444
445- if ( length >= ( Buffer . poolSize >>> 1 ) )
446- return createFromString ( string , ops . encodingVal ) ;
445+ let length = string . length ; // Min length
446+
447+ if ( length >= maxLength )
448+ return createFromString ( string , ops ) ;
449+
450+ length *= 4 ; // Max length (4 bytes per character)
451+
452+ if ( length >= maxLength )
453+ length = ops . byteLength ( string ) ; // Actual length
454+
455+ if ( length >= maxLength )
456+ return createFromString ( string , ops , length ) ;
447457
448458 if ( length > ( poolSize - poolOffset ) )
449459 createPool ( ) ;
450- let b = new FastBuffer ( allocPool , poolOffset , length ) ;
451- const actual = ops . write ( b , string , 0 , length ) ;
452- if ( actual !== length ) {
453- // byteLength() may overestimate. That's a rare case, though.
454- b = new FastBuffer ( allocPool , poolOffset , actual ) ;
455- }
460+
461+ const actual = ops . write ( allocBuffer , string , poolOffset , length ) ;
462+ const b = new FastBuffer ( allocPool , poolOffset , actual ) ;
463+
456464 poolOffset += actual ;
457465 alignPool ( ) ;
458466 return b ;
459467}
460468
469+ function createFromString ( string , ops , length = ops . byteLength ( string ) ) {
470+ const buf = Buffer . allocUnsafeSlow ( length ) ;
471+ const actual = ops . write ( buf , string , 0 , length ) ;
472+ return actual < length ? new FastBuffer ( buf . buffer , 0 , actual ) : buf ;
473+ }
474+
461475function fromString ( string , encoding ) {
462476 let ops ;
463- if ( typeof encoding !== 'string' || encoding . length === 0 ) {
464- if ( string . length === 0 )
465- return new FastBuffer ( ) ;
477+ if ( ! encoding || encoding === 'utf8' ) {
466478 ops = encodingOps . utf8 ;
467479 } else {
468480 ops = getEncodingOps ( encoding ) ;
469481 if ( ops === undefined )
470482 throw new ERR_UNKNOWN_ENCODING ( encoding ) ;
471- if ( string . length === 0 )
472- return new FastBuffer ( ) ;
473483 }
474- return fromStringFast ( string , ops ) ;
484+
485+ return string . length === 0 ? new FastBuffer ( ) : fromStringFast ( string , ops ) ;
475486}
476487
477488function fromArrayBuffer ( obj , byteOffset , length ) {
0 commit comments