Skip to content

Commit 78e4678

Browse files
feat(blst): use external buffers for blst operations (#358)
Depends on ChainSafe/zapi#30 (we need to release zapi and update the dep) This is one of possible likely causes for increased GC pressure on experiments to swap out blst-ts for lodestar-z/bls, as observed on feat2 and feat3 deployments in [this PR](ChainSafe/lodestar#9342). With external array buffers, V8 is only aware of the pointer to the backing memory, instead of having to track both the pointer and the backing memory. This means that during marking phase the GC does not have to walk the backing memory to mark it as 'live' - the frequency of the GC firing off is still the same, but each cycle does less work. This of course comes with a tradeoff, we need a **finalizer** to let V8 know how much external memory is in native heap so that the GC tells the native impl to free the useless memory. Though, regardless of the effect, we should still probably do this anyway, since [napi-rs does the same](https://github.com/napi-rs/napi-rs/blob/159395b365c583a6642ad481edc5708d9f36a24b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs#L175), and only defaults to V8 managed array buffers if it is disallowed (like in Electron).
1 parent c52af09 commit 78e4678

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

bindings/napi/blst.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ pub const PublicKey = struct {
136136
pub fn toBytes(self: *const PublicKey, compress: ?js.Boolean) !js.Uint8Array {
137137
if (try boolOrDefault(compress, true)) {
138138
const bytes = self.raw.compress();
139-
return js.Uint8Array.from(bytes[0..]);
139+
return js.Uint8Array.fromExternal(bytes[0..]);
140140
}
141141
const bytes = self.raw.serialize();
142-
return js.Uint8Array.from(bytes[0..]);
142+
return js.Uint8Array.fromExternal(bytes[0..]);
143143
}
144144

145145
pub fn toHex(self: *const PublicKey, compress: ?js.Boolean) !js.String {
@@ -214,10 +214,10 @@ pub const Signature = struct {
214214
pub fn toBytes(self: *const Signature, compress: ?js.Boolean) !js.Uint8Array {
215215
if (try boolOrDefault(compress, true)) {
216216
const bytes = self.raw.compress();
217-
return js.Uint8Array.from(bytes[0..]);
217+
return js.Uint8Array.fromExternal(bytes[0..]);
218218
}
219219
const bytes = self.raw.serialize();
220-
return js.Uint8Array.from(bytes[0..]);
220+
return js.Uint8Array.fromExternal(bytes[0..]);
221221
}
222222

223223
pub fn toHex(self: *const Signature, compress: ?js.Boolean) !js.String {
@@ -295,9 +295,9 @@ pub const SecretKey = struct {
295295
}
296296

297297
/// Serializes the SecretKey to bytes (32 bytes).
298-
pub fn toBytes(self: *const SecretKey) js.Uint8Array {
298+
pub fn toBytes(self: *const SecretKey) !js.Uint8Array {
299299
const bytes = self.raw.serialize();
300-
return js.Uint8Array.from(bytes[0..]);
300+
return js.Uint8Array.fromExternal(bytes[0..]);
301301
}
302302

303303
pub fn toHex(self: *const SecretKey) !js.String {

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
.hash = "zig_yaml-0.1.0-C1161kFWAwDxjKAFmklKwWVDvz2mmq0Q__bDhGGjeyd3",
3737
},
3838
.zapi = .{
39-
.url = "git+https://github.com/chainsafe/zapi?ref=zapi-v2.0.0#f9fa8b0237352326e9f970b62588b6af01e3e384",
40-
.hash = "zapi-2.0.0-rIqzUcc3BABKRuJlzpYNtiMVCOFybuVSGOLmk0KOCeel",
39+
.url = "git+https://github.com/chainsafe/zapi?ref=zapi-v2.1.0#c5c877af9742d9d7fd6cab2ce6fec698817d56cd",
40+
.hash = "zapi-2.1.0-rIqzUbxNBADOW16nSYQfUOtib1TgC8PxbR4ggN6ezYfA",
4141
},
4242
.zbench = .{
4343
.url = "git+https://github.com/hendriknielaender/zBench#b2b89c475e3ef1bb2bd71255c80478a82d3e0ca8",

0 commit comments

Comments
 (0)