Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 11, 2024
1 parent 5408663 commit de22cf8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions benchmark/buffers/buffer-write-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const common = require('../common.js');
const bench = common.createBenchmark(main, {
len: [1,8,16,32],
n: [1e6],
n: [1],
});

function main({ len, n }) {
Expand All @@ -12,7 +12,7 @@ function main({ len, n }) {

bench.start();
for (let i = 0; i < n; ++i) {
buf.asciiWrite(string);
buf.write(string, 0, len, 'ascii');
}
bench.end(n);
}
3 changes: 2 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const {
} = primordials;

const {
stringWrite,
byteLengthUtf8,
compare: _compare,
compareOffset,
Expand Down Expand Up @@ -657,7 +658,7 @@ const encodingOps = {
encoding: 'ascii',
encodingVal: encodingsMap.ascii,
byteLength: (string) => string.length,
write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len),
write: (buf, string, offset, len) => stringWrite(buf, string, offset, len),
slice: (buf, start, end) => buf.asciiSlice(start, end),
indexOf: (buf, val, byteOffset, dir) =>
indexOfBuffer(buf,
Expand Down
52 changes: 47 additions & 5 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <iostream>
#include "node_buffer.h"
#include "node.h"
#include "node_blob.h"
Expand Down Expand Up @@ -761,6 +762,41 @@ static v8::CFunction fast_byte_length_utf8(
v8::CFunction::Make(FastByteLengthUtf8));


template <encoding encoding>
void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
SPREAD_BUFFER_ARG(args[0], ts_obj);

THROW_AND_RETURN_IF_NOT_STRING(env, args[1], "argument");

Local<String> str = args[1]->ToString(env->context()).ToLocalChecked();

std::cerr << "SlowWriteString " << str->IsOneByte() << std::endl;

size_t offset = 0;
size_t max_length = 0;

THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], 0, &offset));
if (offset > ts_obj_length) {
return node::THROW_ERR_BUFFER_OUT_OF_BOUNDS(
env, "\"offset\" is outside of buffer bounds");
}

THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[3], ts_obj_length - offset,
&max_length));

max_length = std::min(ts_obj_length - offset, max_length);

if (max_length == 0)
return args.GetReturnValue().Set(0);

uint32_t written = StringBytes::Write(
env->isolate(), ts_obj_data + offset, max_length, str, encoding);
args.GetReturnValue().Set(written);
}

uint32_t FastWriteString(Local<Value> receiver,
const v8::FastApiTypedArray<uint8_t>& dst,
const v8::FastOneByteString& src,
Expand All @@ -769,6 +805,10 @@ uint32_t FastWriteString(Local<Value> receiver,
uint8_t* dst_data;
CHECK(dst.getStorageIfAligned(&dst_data));

// XXX: Throw "\"offset\" is outside of buffer bound

std::cerr << "FastWriteString" << std::endl;

memcpy(dst_data, src.data, max_length);

return max_length;
Expand Down Expand Up @@ -1331,18 +1371,20 @@ void Initialize(Local<Object> target,
SetMethodNoSideEffect(context, target, "ucs2Slice", StringSlice<UCS2>);
SetMethodNoSideEffect(context, target, "utf8Slice", StringSlice<UTF8>);

SetFastMethodNoSideEffect(context,
target,
"asciiWrite",
StringWrite<ASCII>,
&fast_write_string);
SetMethod(context, target, "asciiWrite", StringWrite<ASCII>);
SetMethod(context, target, "base64Write", StringWrite<BASE64>);
SetMethod(context, target, "base64urlWrite", StringWrite<BASE64URL>);
SetMethod(context, target, "latin1Write", StringWrite<LATIN1>);
SetMethod(context, target, "hexWrite", StringWrite<HEX>);
SetMethod(context, target, "ucs2Write", StringWrite<UCS2>);
SetMethod(context, target, "utf8Write", StringWrite<UTF8>);

SetFastMethodNoSideEffect(context,
target,
"stringWrite",
SlowWriteString<ASCII>,
&fast_write_string);

SetMethod(context, target, "getZeroFillToggle", GetZeroFillToggle);
}

Expand Down

0 comments on commit de22cf8

Please sign in to comment.