-
Notifications
You must be signed in to change notification settings - Fork 36
IPLD with multithreading constraints #283
Description
I would like to start a discussion around the problems I've encountered in an effort of moving JS-IPFS into SharedWorker. I'm posting a summary here, but more details can be found at ipfs/js-ipfs#3022 (comment)
Context
Teams like 3box and Textile are using primarily ipfs.dag.*
subset of JS-IPFS in browsers. They also need custom codecs like dag-jose
and dag-cose
.
Currently ipfs.dag.put
API takes dagNode
in several representations:
- Plain data structures, structure clone-able JS values + options about desired encoding.
- Binary encoded data (
ArrayBuffer
or some typed array / node buffer view). - Some form of
DAGNode
class instance (e.g. one exposed by ipld-dag-pb)
Problem
Last representation is problematic, especially for custom formats, as there is no generic interface for turning it into representation adequate for moving across threads or processes. E.g. ipfs-http-client just assumes that dagNode
is represented in binary encoding if it's not dag-pb
or dag-cbor
:
This implies that passing DAGNode
instance of dag-jose / dag-cose would not work with ipfs-http-client
. This may be ok for js-ipfs-http-client, however it is problematic for SharedWorker based use case because, client on the main thread needs to be able to:
- Differentiate between Representation 1 and 3.
- Have a way to turn it into adequate representation for moving across threads without introducing unnecessary overhead (which may not necessarily be binary representation, more about later).
- Avoid loading codecs (ideally all the encoding / decoding should happen in the worker thread).
I understand that new block API might address some of that, however I would like to:
- Not be blocked, as in have to wait for new API to eventually make it's way into JS-IPFS. Ideally current
DAGNode
interface could be extended so that:
1. It could be distinguished from plain data representation e.g.dagNode instanceof DAGNode
2. Have method to turn into representation that can be send across threads (In some cases it could be binary in others structure clone-able values). - Invite you to incorporate unique constraints introduces by multithreaded setup e.g.
- Not having to load codecs for block assembly and defer that to the worker thread.
- Consider ownership, because
ArrayBuffer
s can be transferred across threads without copying but that causes representation in the former thread to be emptied. That is to say some though needs to be put into API so that intent of copying vs transferring can be inferred. - Consider intermediate representation, that is structure clone-able
Block
representation that would allow transfer of encoded pieces and copy of raw ones.