likebike / fasteval

Fast and safe evaluation of algebraic expressions
https://crates.io/crates/fasteval/
MIT License
267 stars 26 forks source link

Serialize compiled instructions with serde #9

Open adamsky opened 3 years ago

adamsky commented 3 years ago

For some use cases it may be useful to be able to serialize and deserialize compiled Instructions, along with Slabs. Perhaps this could become an optional feature?

adamsky commented 3 years ago

I've actually had a test case where I wanted to send already compiled instructions over the network and ended up implementing this, pulling in serde and placing a few derives here and there. It's not much but worked fine for me.

likebike commented 3 years ago

Hmm, that's really interesting. I guess I assumed that it would be more performant to just save/transfer the original String expression, and then re-parse it on the other side. As you have already figured out, the instructions are tied to the slab, and it takes quite a bit of memory copies and I/O to serialize all those things and save them to a file or send them across a network. Sending the expression string would be way easier, and fasteval parsing is so fast, I had assumed that for most use cases, it would be the right choice.

I'll definitely think about this more, and I'll run some tests. If it's really more performant to serialize compiled expressions, I can definitely support that. Your code will be a great foundation. Thanks!

adamsky commented 3 years ago

From what I've seen in terms of performance, doing some very preliminary benchmarks, deserializing a compiled Instruction + Slab on the receiver side appears to be faster. Of course this largely depends on the format, and so with bincode it's faster, but using something more compact like msgpack ends up being slower. It also varies based on the actual content of the expression, with compiled serialization taking advantage of expression simplification and whatnot. I guess I'll need to investigate this more.

adamsky commented 3 years ago

Of course by saying it's faster I only mean the receiver side, not the combined sender and receiver. Main idea here being that a sender only has to serialize once, which can maybe save some processing time spend by the whole system if there were more than 1 receivers.