paritytech / jsonrpc

Rust JSON-RPC implementation
MIT License
789 stars 274 forks source link

Request<T> and Response<T> for faster serialization #212

Open faern opened 6 years ago

faern commented 6 years ago

The current Request and Response objects ultimately hold on to serde_json::Value for their actual values. This forces a user of those objects to first serialize/deserialize their types T to/from a serde_json::Value between the wire representation and the final T representation. I have not done any performance benchmarks, but I guess serializing twice does cost quite a lot of extra resources.

If these types had a generic it would be possible to have them deserialize directly from a wire format to their target type T. There would of course be some obstacles to overcome to make this work, but I think it could work. What do you think?

tomusdrw commented 6 years ago

Yup, make perfect sense. Probably can be even done with backward compatibility by doing Request<T: DeserializeOwned = serde_json::Value>

faern commented 6 years ago

A possible set of problems are things like Params::Array(Vec<Value>) where each item in the vector could have a different type. At the moment the way I solve it in jsonrpc-client-core is that I let the T represent the tuple of all types. so If I have a String and u64 as arguments to a method then T is (String, u64).

It basically works because tuples are serialized with [...] so it becomes a list in json. Not completely sure if this can be generalized to the Request object or not. So we would rather need something like Request<T: DeserializeOwned = jsonrpc_client_core::types::Params>