{"action":"created","issue":{"url":"https://api.github.com/repos/dropbox/djinni/issues/54","labels_url":"https://api.github.com/repos/dropbox/djinni/issues/54/labels{/name}","comments_url":"https://api.github.com/repos/dropbox/djinni/issues/54/comments","events_url":"https://api.github.com/repos/dropbox/djinni/issues/54/events","html_url":"https://github.com/dropbox/djinni/issues/54","id":56620207,"number":54,"title":"A buffer data type","user":{"login":"mknejp","id":5156768,"avatar_url":"https://avatars.githubusercontent.com/u/5156768?v=3","gravatar_id":"","url":"https://api.github.com/users/mknejp","html_url":"https://github.com/mknejp","followers_url":"https://api.github.com/users/mknejp/followers","following_url":"https://api.github.com/users/mknejp/following{/other_user}","gists_url":"https://api.github.com/users/mknejp/gists{/gist_id}","starred_url":"https://api.github.com/users/mknejp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mknejp/subscriptions","organizations_url":"https://api.github.com/users/mknejp/orgs","repos_url":"https://api.github.com/users/mknejp/repos","events_url":"https://api.github.com/users/mknejp/events{/privacy}","received_events_url":"https://api.github.com/users/mknejp/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-02-05T02:46:49Z","updated_at":"2015-08-22T09:37:30Z","closed_at":null,"body":"The current way the \"data\" type works is only marginally useful for data streaming between languages due to all the copying involved.\r\n\r\nI was thinking about introducing a \"buffer\" data type that represents memory shared between both sides of the fence without allocating and copying stuff around in every call. Both Objective-C and Java have facilities to access \"unmanaged\" regions of memory.\r\n\r\n- From C++ to\r\n  - Java: a direct `java.nio.ByteBuffer` which can be created in JNI with `NewDirectByteBuffer()` and does not copy the content.\r\n  - Objective-C: `NSData dataWithBytesNoCopy:length:freeWhenDone:` same as above (**)\r\n- From Java to C++: Pass in a `java.nio.ByteBuffer` and use `GetDirectBufferAddress()` and `GetDirectBufferCapacity()` to transform into something like `std::experimental::array_view<uint8_t>`.\r\n- From Objective-C to Java: Pass in a `NSMutableData` and use `.length` and `.mutableBytes` to construct the array view.\r\n\r\nThe whole point of this exercise is to avoid copying the buffer content and is intended for long-lived buffers that are shared and written to/read from on both sides to exchange bulk data. There of course must be some sort of agreement in the interface protocol about who creates the data and make sure it is not modified in a way that invalidates the memory region.\r\n\r\nOn a related note, maybe the \"data\" datatype should also switch to something like `std::experimental::array_view<const uint8_t>` to avoid the copy at least in one direction where possible.\r\n\r\n** *The only drawback here is that `NSData` is read-only. If mutable access is necessary the buffer has to be created on the Objective-C side with `NSMutableData`.*"},"comment":{"url":"https://api.github.com/repos/dropbox/djinni/issues/comments/133671638","html_url":"https://github.com/dropbox/djinni/issues/54#issuecomment-133671638","issue_url":"https://api.github.com/repos/dropbox/djinni/issues/54","id":133671638,"user":{"login":"pwais","id":218145,"avatar_url":"https://avatars.githubusercontent.com/u/218145?v=3","gravatar_id":"","url":"https://api.github.com/users/pwais","html_url":"https://github.com/pwais","followers_url":"https://api.github.com/users/pwais/followers","following_url":"https://api.github.com/users/pwais/following{/other_user}","gists_url":"https://api.github.com/users/pwais/gists{/gist_id}","starred_url":"https://api.github.com/users/pwais/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pwais/subscriptions","organizations_url":"https://api.github.com/users/pwais/orgs","repos_url":"https://api.github.com/users/pwais/repos","events_url":"https://api.github.com/users/pwais/events{/privacy}","received_events_url":"https://api.github.com/users/pwais/received_events","type":"User","site_admin":false},"created_at":"2015-08-22T09:37:30Z","updated_at":"2015-08-22T09:37:30Z","body":"My vote would be to have #95 fulfill the ByteBuffer issue; at least my intention is to use that mechanism.  `java.nio.ByteBuffer` isn't necessarily the best solution-- `ByteBuffer`s still have a garbage collected component.  A user could realistically want to leverage `sun.misc.Unsafe` instead to minimize GC pauses.\r\n"}}