Skip to content

Add memory methods to read into a buffer#1276

Closed
A248 wants to merge 2 commits into
dylibso:mainfrom
A248:memory-read-into-buffer
Closed

Add memory methods to read into a buffer#1276
A248 wants to merge 2 commits into
dylibso:mainfrom
A248:memory-read-into-buffer

Conversation

@A248
Copy link
Copy Markdown
Contributor

@A248 A248 commented Apr 22, 2026

Plus some simple documentation for Memory itself.

@andreaTP
Copy link
Copy Markdown
Collaborator

andreaTP commented Apr 22, 2026

Would you mind to elaborate on the need of expanding the public API with those methods?

Happy to hear @andreas-karlsson POV when available

@A248
Copy link
Copy Markdown
Contributor Author

A248 commented Apr 23, 2026

Exposing APIs that accept caller-provided buffers is a very common Java practice. In my case, I was implementing an InputStream that reads directly from WASM memory given a pointer + length. Without these APIs, I have to perform arraycopy's on intermediate byte arrays, because the InputStream model relies predominantly on caller-provided buffers.

Buffering a big memory region using ByteArrayInputStream is inefficient, and it is especially wasteful if the whole region is not consumed. Rather than reading chunks as multiple new byte[] objects, it would benefit host-guest memory transfer to utilize the methods introduced by this API, whether for re-usable byte buffers or for APIs like InputStream that need it.

@andreaTP
Copy link
Copy Markdown
Collaborator

Do you mind expanding the current JMH tests to prove the efficiency improvement given by those changes?

@A248
Copy link
Copy Markdown
Contributor Author

A248 commented Apr 24, 2026

Why is that a concern?

If the caller has an existing byte[4096], it doesn't make sense to create an intermediate array. This is an extremely common pattern in Java APIs and it seems ridiculous to demand a benchmark. The InputStream API is already designed this way.

@andreaTP
Copy link
Copy Markdown
Collaborator

No specific concerns, additional API increase maintenance, either there is a compelling reason or there is no need to grow the exposed surface.

You are free to implement the Memory interface in your codebase and add anything useful to your project there.

@A248
Copy link
Copy Markdown
Contributor Author

A248 commented Apr 25, 2026

Integrating with the Java ecosystem isn't a compelling reason?

This PR has an incredibly small change surface, re-uses default implementations, and is fully documented. I don't want to fork this repository but it seems you are being pedantic.

Copy link
Copy Markdown
Collaborator

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make an effort to remain professional despite the attitude:

The changes in this PR are usless unless proven, I have done dozens of integrations and never felt the need.

I acknowledge there might be a theoretical minimal benefit, but I'm not up accepting a PR increasing the public API of Memory without proving evidence, being it:

  • performance
  • a real world need

We have plans for consolidating and shrinking it instead.

Feel free to close the PR and fork if you are not comfortable with the answer.

@A248
Copy link
Copy Markdown
Contributor Author

A248 commented Apr 26, 2026

I'm sorry, but I am not the one with an attitude here. You have spoken condescendingly and been intentionally obtuse and hypocritical even as I elaborated patiently.

I am not going to waste time on a benchmark for such an elementary thing and will close this PR.

@A248 A248 closed this Apr 26, 2026
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

Successfully merging this pull request may close these issues.

2 participants