node-re2 icon indicating copy to clipboard operation
node-re2 copied to clipboard

Add `RE2.test(input, offset, length)`

Open ronag opened this issue 1 year ago • 5 comments

Could avoid the need to create subarrays when matching

i.e.

re2.test(buffer.subarray(1,3)) vs re2.test(buffer, 1, 2)

ronag avatar Sep 12 '24 20:09 ronag

I am not sure we have a use case here: buf.subarray([start[, end]]) "returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indexes". It is my understanding that subarray() creates a view, no byte copying is involved. I assume it is a very fast operation.

Thoughts?

uhop avatar Sep 13 '24 21:09 uhop

subarray is quite slow. I've spent a lot of time removing sub array creation in node core due to this.

ronag avatar Sep 14 '24 07:09 ronag

That is strange. Conceptually it should be a pointer and two numbers like in your proposal. Would it help if I create such object in the library, so it can be used consistently in all methods instead of a string/buffer or do you expect it to be too expensive performance-wise?

uhop avatar Sep 15 '24 15:09 uhop

It super expensive to create typed arrays since it does a lot of checks. Creating a different abstraction is also an option. Internall we use something like this for creating "slices" of buffers:

class Slice {
  buffer: Buffer,
  byteOffset: number,
  byteLength
}

Possibly relevant: https://issues.chromium.org/issues/42210394

ronag avatar Sep 15 '24 15:09 ronag

Your Slice was exactly what I was thinking about.

uhop avatar Sep 15 '24 15:09 uhop