Skip to content

Commit e040ae7

Browse files
authored
Merge pull request #5372 from cloudflare/jasnell/encodeinto-perf-improvement
2 parents d4aba93 + dc5836d commit e040ae7

File tree

9 files changed

+30
-23
lines changed

9 files changed

+30
-23
lines changed

src/workerd/api/encoding.c++

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,13 @@ jsg::BufferSource TextEncoder::encode(jsg::Lock& js, jsg::Optional<jsg::JsString
486486
}
487487

488488
TextEncoder::EncodeIntoResult TextEncoder::encodeInto(
489-
jsg::Lock& js, jsg::JsString input, jsg::BufferSource buffer) {
490-
auto handle = buffer.getHandle(js);
491-
JSG_REQUIRE(handle->IsUint8Array(), TypeError, "buffer must be a Uint8Array");
492-
return encodeIntoImpl(js, input, buffer);
489+
jsg::Lock& js, jsg::JsString input, jsg::JsUint8Array buffer) {
490+
auto result = input.writeInto(
491+
js, buffer.asArrayPtr<char>(), jsg::JsString::WriteFlags::REPLACE_INVALID_UTF8);
492+
return TextEncoder::EncodeIntoResult{
493+
.read = static_cast<int>(result.read),
494+
.written = static_cast<int>(result.written),
495+
};
493496
}
494497

495498
} // namespace workerd::api

src/workerd/api/encoding.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class TextEncoder final: public jsg::Object {
218218

219219
jsg::BufferSource encode(jsg::Lock& js, jsg::Optional<jsg::JsString> input);
220220

221-
EncodeIntoResult encodeInto(jsg::Lock& js, jsg::JsString input, jsg::BufferSource buffer);
221+
EncodeIntoResult encodeInto(jsg::Lock& js, jsg::JsString input, jsg::JsUint8Array buffer);
222222

223223
// UTF-8 is the only encoding type supported by the WHATWG spec.
224224
kj::StringPtr getEncoding() {
@@ -239,6 +239,7 @@ class TextEncoder final: public jsg::Object {
239239
// `Uint8Array`. The spec defines that this function returns a `Uint8Array` too.
240240
JSG_TS_OVERRIDE({
241241
encode(input?: string): Uint8Array;
242+
encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult;
242243
});
243244
}
244245
};

src/workerd/jsg/jsg.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,8 @@ class JsMessage;
22352235
V(Set) \
22362236
V(Promise) \
22372237
V(Proxy) \
2238-
V(Function)
2238+
V(Function) \
2239+
V(Uint8Array)
22392240

22402241
#define V(Name) class Js##Name;
22412242
JS_TYPE_CLASSES(V)

src/workerd/jsg/jsvalue.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ inline void requireOnStack(void* self) {
4444
V(ArrayBuffer) \
4545
V(ArrayBufferView) \
4646
V(TypedArray) \
47-
V(Uint8Array) \
4847
V(Uint8ClampedArray) \
4948
V(Int8Array) \
5049
V(Uint16Array) \
@@ -230,6 +229,20 @@ class JsArray final: public JsBase<v8::Array, JsArray> {
230229
using JsBase<v8::Array, JsArray>::JsBase;
231230
};
232231

232+
class JsUint8Array final: public JsBase<v8::Uint8Array, JsUint8Array> {
233+
public:
234+
template <typename T = kj::byte>
235+
kj::ArrayPtr<T> asArrayPtr() {
236+
v8::Local<v8::Uint8Array> inner = *this;
237+
auto buf = inner->Buffer();
238+
T* data = static_cast<T*>(buf->Data()) + inner->ByteOffset();
239+
size_t length = inner->ByteLength();
240+
return kj::ArrayPtr(data, length);
241+
}
242+
243+
using JsBase<v8::Uint8Array, JsUint8Array>::JsBase;
244+
};
245+
233246
class JsString final: public JsBase<v8::String, JsString> {
234247
public:
235248
int length(Lock& js) const KJ_WARN_UNUSED_RESULT;

src/workerd/jsg/rtti.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ struct BuildRtti<Configuration, v8::Promise> {
468468
};
469469

470470
#define FOR_EACH_BUILTIN_TYPE(F, ...) \
471+
F(jsg::JsUint8Array, BuiltinType::Type::V8_UINT8_ARRAY) \
471472
F(jsg::BufferSource, BuiltinType::Type::JSG_BUFFER_SOURCE) \
472473
F(kj::Date, BuiltinType::Type::KJ_DATE) \
473474
F(v8::ArrayBufferView, BuiltinType::Type::V8_ARRAY_BUFFER_VIEW) \

types/generated-snapshot/experimental/index.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,10 +1587,7 @@ declare class TextEncoder {
15871587
*
15881588
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto)
15891589
*/
1590-
encodeInto(
1591-
input: string,
1592-
buffer: ArrayBuffer | ArrayBufferView,
1593-
): TextEncoderEncodeIntoResult;
1590+
encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult;
15941591
get encoding(): string;
15951592
}
15961593
interface TextDecoderConstructorOptions {

types/generated-snapshot/experimental/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,10 +1592,7 @@ export declare class TextEncoder {
15921592
*
15931593
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto)
15941594
*/
1595-
encodeInto(
1596-
input: string,
1597-
buffer: ArrayBuffer | ArrayBufferView,
1598-
): TextEncoderEncodeIntoResult;
1595+
encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult;
15991596
get encoding(): string;
16001597
}
16011598
export interface TextDecoderConstructorOptions {

types/generated-snapshot/latest/index.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,10 +1542,7 @@ declare class TextEncoder {
15421542
*
15431543
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto)
15441544
*/
1545-
encodeInto(
1546-
input: string,
1547-
buffer: ArrayBuffer | ArrayBufferView,
1548-
): TextEncoderEncodeIntoResult;
1545+
encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult;
15491546
get encoding(): string;
15501547
}
15511548
interface TextDecoderConstructorOptions {

types/generated-snapshot/latest/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,10 +1547,7 @@ export declare class TextEncoder {
15471547
*
15481548
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto)
15491549
*/
1550-
encodeInto(
1551-
input: string,
1552-
buffer: ArrayBuffer | ArrayBufferView,
1553-
): TextEncoderEncodeIntoResult;
1550+
encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult;
15541551
get encoding(): string;
15551552
}
15561553
export interface TextDecoderConstructorOptions {

0 commit comments

Comments
 (0)