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

ByteBuffer POC [DO NOT MERGE] #1526

Closed
wants to merge 4 commits into from
Closed

ByteBuffer POC [DO NOT MERGE] #1526

wants to merge 4 commits into from

Conversation

VysotskiVadim
Copy link
Contributor

It's an example of how mapbox java can deserialise direction response model from buffer allocated in native memory copying data by small chunks, in our case by 1024 bytes which happens inside GSON library.

@Test
public void deserialization_from_byte_buffer() throws Exception {
String json = loadJsonFixture(DIRECTIONS_V5_PRECISION6_FIXTURE);
ByteBuffer buffer = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Copy link
Member

Choose a reason for hiding this comment

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

I just want to point out that you can only pass java.nio.DirectBuffer (created with java.nio.ByteBuffer.allocateDirect, or the JNI NewDirectByteBuffer function) to native using the DataRef type (I know it's unrelated to this test, I just want to make sure there is no surprise). ByteBuffer.wrap doesn't allocate memory on native heap so native can't access the memory region directly. Otherwise, code from DirectionsResponse looks good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the clarification. I updated the test to use direct buffer to avoid confusion.

@@ -146,6 +154,49 @@ public static DirectionsResponse fromJson(@NonNull String json) {
return gson.create().fromJson(json, DirectionsResponse.class).toBuilder().build();
}

public static DirectionsResponse fromJson(@NonNull ByteBuffer json, Charset charsets) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about ways we could access specific parts of the DirectionsResponse, rather than deserializing the entire object. The DirectionsResponse can be large, but if we could stream the components.

For example, could we stream the geometry? #1518

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kmadsen , I'm not sure if I understood this idea correctly. Is the idea about keeping raw json response in memory and deserialise only during first access?

@VysotskiVadim
Copy link
Contributor Author

Closed in favour of #1545

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.

None yet

3 participants