GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — zlib: avoid flattening strings in crc32 fast path · dasunNimantha/node@3f17c06

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • zlib.js
  • src
    • node_zlib.cc
  • typings/internalBinding
    • zlib.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/zlib.js‎

Copy file name to clipboardExpand all lines: lib/zlib.js
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ const {
6161
isUint8Array,
6262
} = require('internal/util/types');
6363
const binding = internalBinding('zlib');
64-
const { crc32: crc32Native, crc32c: crc32cNative } = binding;
64+
const {
65+
crc32: crc32String,
66+
crc32c: crc32cString,
67+
crc32Buffer,
68+
crc32cBuffer,
69+
} = binding;
6570
const assert = require('internal/assert');
6671
const {
6772
Buffer,
@@ -1276,7 +1281,10 @@ function crc32(data, value = 0) {
12761281
validateUint32(value, 'value');
12771282
// Coerce -0 to +0.
12781283
value += 0;
1279-
return crc32Native(data, value);
1284+
// Strings use a regular binding: reading them may flatten (allocate),
1285+
// which must not happen inside a value-returning fast API call.
1286+
return typeof data === 'string' ?
1287+
crc32String(data, value) : crc32Buffer(data, value);
12801288
}
12811289

12821290
function crc32c(data, value = 0) {
@@ -1286,7 +1294,8 @@ function crc32c(data, value = 0) {
12861294
validateUint32(value, 'value');
12871295
// Coerce -0 to +0.
12881296
value += 0;
1289-
return crc32cNative(data, value);
1297+
return typeof data === 'string' ?
1298+
crc32cString(data, value) : crc32cBuffer(data, value);
12901299
}
12911300

12921301
// Legacy alias on the C++ wrapper object. This is not public API, so we may
Collapse file

‎src/node_zlib.cc‎

Copy file name to clipboardExpand all lines: src/node_zlib.cc
+25-4Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,14 +2137,22 @@ static void CRC32(const FunctionCallbackInfo<Value>& args) {
21372137
args.GetReturnValue().Set(CRC32Impl(args.GetIsolate(), args[0], value));
21382138
}
21392139

2140+
// Strings use CRC32 above: the fast API path must not flatten them.
2141+
static void CRC32Buffer(const FunctionCallbackInfo<Value>& args) {
2142+
CHECK(args[0]->IsArrayBufferView());
2143+
CHECK(args[1]->IsUint32());
2144+
uint32_t value = args[1].As<v8::Uint32>()->Value();
2145+
args.GetReturnValue().Set(CRC32Impl(args.GetIsolate(), args[0], value));
2146+
}
2147+
21402148
static uint32_t FastCRC32(v8::Local<v8::Value> receiver,
21412149
v8::Local<v8::Value> data,
21422150
uint32_t value,
21432151
// NOLINTNEXTLINE(runtime/references)
21442152
v8::FastApiCallbackOptions& options) {
21452153
TRACK_V8_FAST_API_CALL("zlib.crc32");
21462154
v8::HandleScope handle_scope(options.isolate);
2147-
CHECK(data->IsArrayBufferView() || data->IsString());
2155+
CHECK(data->IsArrayBufferView());
21482156
return CRC32Impl(options.isolate, data, value);
21492157
}
21502158

@@ -2167,14 +2175,21 @@ static void CRC32C(const FunctionCallbackInfo<Value>& args) {
21672175
args.GetReturnValue().Set(CRC32CImpl(args.GetIsolate(), args[0], value));
21682176
}
21692177

2178+
static void CRC32CBuffer(const FunctionCallbackInfo<Value>& args) {
2179+
CHECK(args[0]->IsArrayBufferView());
2180+
CHECK(args[1]->IsUint32());
2181+
uint32_t value = args[1].As<v8::Uint32>()->Value();
2182+
args.GetReturnValue().Set(CRC32CImpl(args.GetIsolate(), args[0], value));
2183+
}
2184+
21702185
static uint32_t FastCRC32C(v8::Local<v8::Value> receiver,
21712186
v8::Local<v8::Value> data,
21722187
uint32_t value,
21732188
// NOLINTNEXTLINE(runtime/references)
21742189
v8::FastApiCallbackOptions& options) {
21752190
TRACK_V8_FAST_API_CALL("zlib.crc32c");
21762191
v8::HandleScope handle_scope(options.isolate);
2177-
CHECK(data->IsArrayBufferView() || data->IsString());
2192+
CHECK(data->IsArrayBufferView());
21782193
return CRC32CImpl(options.isolate, data, value);
21792194
}
21802195

@@ -2192,8 +2207,12 @@ void Initialize(Local<Object> target,
21922207
MakeClass<ZstdCompressStream>::Make(env, target, "ZstdCompress");
21932208
MakeClass<ZstdDecompressStream>::Make(env, target, "ZstdDecompress");
21942209

2195-
SetFastMethodNoSideEffect(context, target, "crc32", CRC32, &fast_crc32_);
2196-
SetFastMethodNoSideEffect(context, target, "crc32c", CRC32C, &fast_crc32c_);
2210+
SetMethodNoSideEffect(context, target, "crc32", CRC32);
2211+
SetMethodNoSideEffect(context, target, "crc32c", CRC32C);
2212+
SetFastMethodNoSideEffect(
2213+
context, target, "crc32Buffer", CRC32Buffer, &fast_crc32_);
2214+
SetFastMethodNoSideEffect(
2215+
context, target, "crc32cBuffer", CRC32CBuffer, &fast_crc32c_);
21972216
target->Set(env->context(),
21982217
FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
21992218
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)).Check();
@@ -2206,8 +2225,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
22062225
MakeClass<ZstdCompressStream>::Make(registry);
22072226
MakeClass<ZstdDecompressStream>::Make(registry);
22082227
registry->Register(CRC32);
2228+
registry->Register(CRC32Buffer);
22092229
registry->Register(fast_crc32_);
22102230
registry->Register(CRC32C);
2231+
registry->Register(CRC32CBuffer);
22112232
registry->Register(fast_crc32c_);
22122233
}
22132234

Collapse file

‎typings/internalBinding/zlib.d.ts‎

Copy file name to clipboardExpand all lines: typings/internalBinding/zlib.d.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ export interface ZlibBinding {
5050
ZstdDecompress: typeof InternalZlibBinding.ZstdDecompress;
5151
crc32(data: string | ArrayBufferView, value: number): number;
5252
crc32c(data: string | ArrayBufferView, value: number): number;
53+
crc32Buffer(data: ArrayBufferView, value: number): number;
54+
crc32cBuffer(data: ArrayBufferView, value: number): number;
5355
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.