@@ -59,7 +59,6 @@ const {
5959 compare : _compare ,
6060 compareOffset,
6161 copy : _copy ,
62- createFromString,
6362 fill : bindingFill ,
6463 isAscii : bindingIsAscii ,
6564 isUtf8 : bindingIsUtf8 ,
@@ -150,11 +149,12 @@ const constants = ObjectDefineProperties({}, {
150149} ) ;
151150
152151Buffer . poolSize = 8 * 1024 ;
153- let poolSize , poolOffset , allocPool ;
152+ let poolSize , poolOffset , allocPool , allocBuffer ;
154153
155154function createPool ( ) {
156155 poolSize = Buffer . poolSize ;
157- allocPool = createUnsafeBuffer ( poolSize ) . buffer ;
156+ allocBuffer = createUnsafeBuffer ( poolSize ) ;
157+ allocPool = allocBuffer . buffer ;
158158 markAsUntransferable ( allocPool ) ;
159159 poolOffset = 0 ;
160160}
@@ -442,38 +442,49 @@ function allocate(size) {
442442}
443443
444444function fromStringFast ( string , ops ) {
445- const length = ops . byteLength ( string ) ;
445+ const maxLength = Buffer . poolSize >>> 1 ;
446446
447- if ( length >= ( Buffer . poolSize >>> 1 ) )
448- return createFromString ( string , ops . encodingVal ) ;
447+ let length = string . length ; // Min length
448+
449+ if ( length >= maxLength )
450+ return createFromString ( string , ops ) ;
451+
452+ length *= 4 ; // Max length (4 bytes per character)
453+
454+ if ( length >= maxLength )
455+ length = ops . byteLength ( string ) ; // Actual length
456+
457+ if ( length >= maxLength )
458+ return createFromString ( string , ops , length ) ;
449459
450460 if ( length > ( poolSize - poolOffset ) )
451461 createPool ( ) ;
452- let b = new FastBuffer ( allocPool , poolOffset , length ) ;
453- const actual = ops . write ( b , string , 0 , length ) ;
454- if ( actual !== length ) {
455- // byteLength() may overestimate. That's a rare case, though.
456- b = new FastBuffer ( allocPool , poolOffset , actual ) ;
457- }
462+
463+ const actual = ops . write ( allocBuffer , string , poolOffset , length ) ;
464+ const b = new FastBuffer ( allocPool , poolOffset , actual ) ;
465+
458466 poolOffset += actual ;
459467 alignPool ( ) ;
460468 return b ;
461469}
462470
471+ function createFromString ( string , ops , length = ops . byteLength ( string ) ) {
472+ const buf = Buffer . allocUnsafeSlow ( length ) ;
473+ const actual = ops . write ( buf , string , 0 , length ) ;
474+ return actual < length ? new FastBuffer ( buf . buffer , 0 , actual ) : buf ;
475+ }
476+
463477function fromString ( string , encoding ) {
464478 let ops ;
465- if ( typeof encoding !== 'string' || encoding . length === 0 ) {
466- if ( string . length === 0 )
467- return new FastBuffer ( ) ;
479+ if ( ! encoding || encoding === 'utf8' ) {
468480 ops = encodingOps . utf8 ;
469481 } else {
470482 ops = getEncodingOps ( encoding ) ;
471483 if ( ops === undefined )
472484 throw new ERR_UNKNOWN_ENCODING ( encoding ) ;
473- if ( string . length === 0 )
474- return new FastBuffer ( ) ;
475485 }
476- return fromStringFast ( string , ops ) ;
486+
487+ return string . length === 0 ? new FastBuffer ( ) : fromStringFast ( string , ops ) ;
477488}
478489
479490function fromArrayBuffer ( obj , byteOffset , length ) {
0 commit comments