On Fri, Feb 8, 2013 at 9:13 AM, Paul Oliver <
puzz...@gmail.com> wrote:
>
> Hey Anton,
>
> What would be required to have the _ext json/xml stuff work in pure Erlang
> as well?
Hi Paul,
Can you tell more about your use case for this?
I can think of 3 practical cases.
1. Optimizing raw performance, i.e. how fast one can convert JSON and
XML bits into Erlang representation.
2. Minimizing IPC latency or eliminating IPC overhead entirely. This
can be important for latency-sensitive applications. Also, it improves
throughput of single-threaded applications.
3. Better portability of Erlang applications that use Piqi for JSON
and XML serialization. We need to be very specific here about
potential deployment platforms. I would say this is more of a
usability problem. In other words, it comes down to the question of
how much effort it takes for a user to setup piqi-erlang on platform
X. Ideally, the effort should be 0 (it should already be this way on
Linux and Mac).
Let's take a closer look at potential optimization gains. I ran a
series of erlang serialization tests with Piqi and mochijson2 on my
dual-core mac and the micro-benchmarks showed that:
- If implemented using mochijson2:decode, JSON deserialization
performance would be comparable with Piqi.
- JSON serialization based on mochijson2:encode would be roughly 2.5
times faster. Not sure where such a big difference come from.
- IPC latency overhead, when serializing small data structures with
Piqi, stays within 40-50 microseconds.
- Native Erlang implementation would easily utilize all available
multi-core CPU resource. IPC, at least the current implementation,
doesn't allow Piqi-based serialization achieve 100% CPU resource
utilization, but it is getting close starting from moderate
concurrency levels (in my case, it was 8 threads on 2 cores).
I haven't tested xmerl, but I expect XML serialization to be slower in
case of a pure Erlang implementation.
At the same time, there are still a lot of optimization opportunities
in the existing implementation based on using the external piqi
program. There are certain limits of course. For example, IPC latency
won't go away completely. Another fundamental limit of this approach
is that it assumes one extra serialization cycle for sending Erlang
data structures to Piqi over pipe and reading them into internal
representation on the other end.
Now, to answer your question of what it would take to add support for
native Erlang JSON serialization. It is totally doable:
- Implementation effort -- varies depending on who will do it.
Comparable with piqic-erlang backend for protocol buffers
serialization + small runtime support library. All can be done in
Erlang. I've written 6 different backends for Piqi -- it is fairly
mechanical work for the most part but it needs to be done accurately.
This assumes using a solid third-party JSON library such as
mochijson2.
- Giving up on some features such as JSON pretty-printing and location
information in parsing errors.
- Risk of introducing subtle differences in behavior between native
Erlang and Piqi-based serialization (alternatively, developing
conformance tests and testing the new implementation against the
existing one).
- Risk of increased maintenance and support overhead.
- To add native XML support on top of that, multiply everything by 2.
Anton