Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why users has to go through 2 rounds of copying? #58

Open
SmartLayer opened this issue Dec 30, 2020 · 2 comments
Open

why users has to go through 2 rounds of copying? #58

SmartLayer opened this issue Dec 30, 2020 · 2 comments

Comments

@SmartLayer
Copy link

SmartLayer commented Dec 30, 2020

Observe the test case:

function getCorrectBuffer(content)
{
const arrayBuffer = new ArrayBuffer(content.length);
const uint8Array = new Uint8Array(arrayBuffer);
for(let i = 0; i < content.length; i++)
uint8Array[i] = content[i];
return arrayBuffer.slice(0);
}

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?

@YuryStrozhevsky
Copy link
Contributor

@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.

@microshine
Copy link
Contributor

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants