| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,7 +57,6 @@ const { | |||
| 57 | 57 | byteLengthUtf8, | |
| 58 | 58 | compare: _compare, | |
| 59 | 59 | compareOffset, | |
| 60 | - createFromString, | ||
| 61 | 60 | fill: bindingFill, | |
| 62 | 61 | isAscii: bindingIsAscii, | |
| 63 | 62 | isUtf8: bindingIsUtf8, | |
@@ -148,11 +147,12 @@ const constants = ObjectDefineProperties({}, { | |||
| 148 | 147 | }); | |
| 149 | 148 | ||
| 150 | 149 | Buffer.poolSize = 8 * 1024; | |
| 151 | - let poolSize, poolOffset, allocPool; | ||
| 150 | + let poolSize, poolOffset, allocPool, allocBuffer; | ||
| 152 | 151 | ||
| 153 | 152 | function createPool() { | |
| 154 | 153 | poolSize = Buffer.poolSize; | |
| 155 | - allocPool = createUnsafeBuffer(poolSize).buffer; | ||
| 154 | + allocBuffer = createUnsafeBuffer(poolSize); | ||
| 155 | + allocPool = allocBuffer.buffer; | ||
| 156 | 156 | markAsUntransferable(allocPool); | |
| 157 | 157 | poolOffset = 0; | |
| 158 | 158 | } | |
@@ -440,38 +440,49 @@ function allocate(size) { | |||
| 440 | 440 | } | |
| 441 | 441 | ||
| 442 | 442 | function fromStringFast(string, ops) { | |
| 443 | - const length = ops.byteLength(string); | ||
| 443 | + const maxLength = Buffer.poolSize >>> 1; | ||
| 444 | 444 | ||
| 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); | ||
| 447 | 457 | ||
| 448 | 458 | if (length > (poolSize - poolOffset)) | |
| 449 | 459 | 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 | + | ||
| 456 | 464 | poolOffset += actual; | |
| 457 | 465 | alignPool(); | |
| 458 | 466 | return b; | |
| 459 | 467 | } | |
| 460 | 468 | ||
| 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 | + | ||
| 461 | 475 | function fromString(string, encoding) { | |
| 462 | 476 | let ops; | |
| 463 | - if (typeof encoding !== 'string' || encoding.length === 0) { | ||
| 464 | - if (string.length === 0) | ||
| 465 | - return new FastBuffer(); | ||
| 477 | + if (!encoding || encoding === 'utf8') { | ||
| 466 | 478 | ops = encodingOps.utf8; | |
| 467 | 479 | } else { | |
| 468 | 480 | ops = getEncodingOps(encoding); | |
| 469 | 481 | if (ops === undefined) | |
| 470 | 482 | throw new ERR_UNKNOWN_ENCODING(encoding); | |
| 471 | - if (string.length === 0) | ||
| 472 | - return new FastBuffer(); | ||
| 473 | 483 | } | |
| 474 | - return fromStringFast(string, ops); | ||
| 484 | + | ||
| 485 | + return string.length === 0 ? new FastBuffer() : fromStringFast(string, ops); | ||
| 475 | 486 | } | |
| 476 | 487 | ||
| 477 | 488 | function fromArrayBuffer(obj, byteOffset, length) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -529,17 +529,6 @@ MaybeLocal<Object> New(Environment* env, | |||
| 529 | 529 | ||
| 530 | 530 | namespace { | |
| 531 | 531 | ||
| 532 | - void CreateFromString(const FunctionCallbackInfo<Value>& args) { | ||
| 533 | - CHECK(args[0]->IsString()); | ||
| 534 | - CHECK(args[1]->IsInt32()); | ||
| 535 | - | ||
| 536 | - enum encoding enc = static_cast<enum encoding>(args[1].As<Int32>()->Value()); | ||
| 537 | - Local<Object> buf; | ||
| 538 | - if (New(args.GetIsolate(), args[0].As<String>(), enc).ToLocal(&buf)) | ||
| 539 | - args.GetReturnValue().Set(buf); | ||
| 540 | - } | ||
| 541 | - | ||
| 542 | - | ||
| 543 | 532 | template <encoding encoding> | |
| 544 | 533 | void StringSlice(const FunctionCallbackInfo<Value>& args) { | |
| 545 | 534 | Environment* env = Environment::GetCurrent(args); | |
@@ -1422,7 +1411,6 @@ void Initialize(Local<Object> target, | |||
| 1422 | 1411 | SetMethodNoSideEffect(context, target, "btoa", Btoa); | |
| 1423 | 1412 | ||
| 1424 | 1413 | SetMethod(context, target, "setBufferPrototype", SetBufferPrototype); | |
| 1425 | - SetMethodNoSideEffect(context, target, "createFromString", CreateFromString); | ||
| 1426 | 1414 | ||
| 1427 | 1415 | SetFastMethodNoSideEffect(context, | |
| 1428 | 1416 | target, | |
@@ -1487,7 +1475,6 @@ void Initialize(Local<Object> target, | |||
| 1487 | 1475 | ||
| 1488 | 1476 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
| 1489 | 1477 | registry->Register(SetBufferPrototype); | |
| 1490 | - registry->Register(CreateFromString); | ||
| 1491 | 1478 | ||
| 1492 | 1479 | registry->Register(SlowByteLengthUtf8); | |
| 1493 | 1480 | registry->Register(fast_byte_length_utf8.GetTypeInfo()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments