ASN1.js icon indicating copy to clipboard operation
ASN1.js copied to clipboard

why users has to go through 2 rounds of copying?

Open SmartLayer opened this issue 3 years ago • 2 comments

Observe the test case:

https://github.com/PeculiarVentures/ASN1.js/blob/5c64632d8d08955f07a9f80b9518a84d76f605cd/test/testSuite.js#L12-L21

The file being read is copied into a new ArrayBuffer (line 17), which is in turn copied to a new one (line 20 with slice() Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer).

Why the same data has to be copied 2 times? If the underlying data is 1GB, that is 2GB additonal memory being used (although the intermediary 1GB will be released once the function ends). Just curious.

P.S. in our applications we tried to feed the Buffer returnd directly from ReadFileSync or the ArrayBuffer of that Buffer, all failed with this error. The only way that works is to honestly copy 2 times just like you did.

		throw new Error("Object's schema was not verified against input data for MyModule");
		      ^

Can you fix this issue by adding a line of comment before the line 12 reasoning the 2× copying?

SmartLayer avatar Dec 30 '20 05:12 SmartLayer

@colourful-land Frankly speaking I do not remember why I made the getCorrectBuffer. Most probably it is an issue in fs.readFileSync. I do not have a time to investigate it again. This is not an issue in ASN1js itself.

YuryStrozhevsky avatar Dec 30 '20 09:12 YuryStrozhevsky

Maybe the problem is in Buffer usage. Its buffer property is greater than init value.

Buffer.from("test").buffer
ArrayBuffer {
  [Uint8Contents]: <2f 0f 14 05 01 00 00 00 2f 00 00 00 00 00 00 00 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff 0a 00 00 00 ff ff ff ff 30 00 00 00 ff ff ff ff 36 00 00 00 80 0f 14 05 01 00 00 00 00 00 00 00 00 00 00 00 38 22 06 06 01 00 00 00 00 00 00 00 ... 8092 more bytes>,
  byteLength: 8192

@colourful-land Could you try updated getCorrectBuffer?

function getCorrectBuffer(content)
{
  return new Uint8Array(content).buffer;
}

It must return the correct buffer. But I'm not sure it doesn't copy data

new Uint8Array(Buffer.from("test")).buffer
ArrayBuffer { [Uint8Contents]: <74 65 73 74>, byteLength: 4 }

microshine avatar Mar 16 '21 23:03 microshine