Live data from Hacker News

Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

github.com

21–30 of 36 posts

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#21

More protobuf implementations is certainly good, Google's have been really really lacking for us. It would be really nice if there was a pure Python implementation that didn't use tons of unnecessary metaprogramming.

It's really interesting to me that the top two comments on this story both wish Protocol Buffers were different, but seem to be asking for opposite things.

Your comment wishes that there was a pure-Python implementation that doesn't do metaprogramming. That would imply doing code generation, but having the generated code be more "concrete", so-to-speak (containing all of the actual implementation).

However, a different comment (https://news.ycombinator.com/item?id=10762101) admires the thriftpy project which doesn't have a codegen step at all. This is in some sense the opposite of what you want: everything becomes metaprogramming.

I work on Protocol Buffers at Google, and I frequently observe these kinds of opposing feelings about how protobufs ought to work. One thing I've learned is how incredibly difficult it is to please everybody. Another good example of this is whether to be pure-Python: some people (like you) want that. But it's nearly impossible to make it very fast, and lots of users are sensitive to speed (you can find various comments and blog articles complaining about the speed of Python protobuf).

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#22

Thrift is a very similar system for message serialization to Protobuf. The thriftpy project implements a pure Python parser for thrift files that dynamically generates Python modules without a build/codegen step. Also has Py2, Py3, and PyPy support. Awesome library for this sort of thing. Includes a Cython impl of Thrift parsing/serialization, too: https://github.com/eleme/thriftpy

What is the benefit of using something like Thrift to something like CapNProto or FlatBuffers ?

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#23
post #3
post #2

If your looking for a speedy alternative to protobuf then you owe it to yourself to try flatbuffers. https://google.github.io/flatbuffers/

Don't bother, go directly to cap'n proto: https://capnproto.org

There are a couple of reasons I chose FB over Can'n Proto, Python and full C++ doesn't work on MSVC/Windows (this might have changed recently especially given the Clang compiler integration into MS backend)

Second problem is Capnproto doesn't have structs, ie. you want something like struct Vec3 {x: float, y: float, z: float} in CapnProto you have to either make Vec3 a ref type (+ pointer size and lookup indirection for each instance) or inline it manually (especially annoying given that you must manually number CapnProto fields, maybe this is a benefit in some use cases but it's of no use to me and only creates work).

CapnProto is much bigger as well - it includes it's own RPC/distributed object protocol and library - the serialization part isn't separate they are in one library - that's taking in a lot of useless code that you would have to figure out if you decided to maintain it.

Also worth noting FlatBuffers on Python are actually not that fast because they are pure-python - there are Cython versions in the PR (at least last time I checked) - this doesn't matter in my use case but worth pointing out. And the Python API is very very unpythonic (C style API with even the naming completely off from PEP8) compared to CapnProto which is excellent !

I needed a cross language typed serialization format for binary files and FlatBuffers is better for that than CapnProto IMO.

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#24
post #8
post #5

Earlier quoted context omitted.

Last I looked cap'n proto didn't have any msvc support.

The last release added it (partially). "The core serialization functionality sufficient for 90% of users is available, but reflection and RPC APIs are not."

So Python API still doesn't work on Windows ? (AFAIK it's using reflection bindings from C++ ?)

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#25

More protobuf implementations is certainly good, Google's have been really really lacking for us. It would be really nice if there was a pure Python implementation that didn't use tons of unnecessary metaprogramming.

It's really interesting to me that the top two comments on this story both wish Protocol Buffers were different, but seem to be asking for opposite things. Your comment wishes that there was a pure-Python implementation that doesn't do metaprogramming. That would imply doing code generation, but having the generated code be more "concrete", so-to-speak (containing all of the actual implementation). However, a differe…

Hey Josh! Jorge from gRPC here. What's your take on Pyrobuf?

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#27

Thrift is a very similar system for message serialization to Protobuf. The thriftpy project implements a pure Python parser for thrift files that dynamically generates Python modules without a build/codegen step. Also has Py2, Py3, and PyPy support. Awesome library for this sort of thing. Includes a Cython impl of Thrift parsing/serialization, too: https://github.com/eleme/thriftpy

What is the benefit of using something like Thrift to something like CapNProto or FlatBuffers ?

I suspect it comes down to the details of the serialization format.

I know that Thrift and Protobuf were developed / publicly released around the same time (~2008). They both have serialization and RPC approaches. They both have an IDL format and a compiler for codegen in static languages like C++ and Java.

Thrift was adopted by the Apache O/SS community and so it can be found in some Apache projects, e.g. Thrift is used as an interop format in Apache Storm and as a serialization backend supported by Apache Parquet.

Thrift has had a lot of client libraries for different languages come out over the years, so it tends to work everywhere.

IIRC, CapNProto and FlatBuffers were each built as fresh takes on protobuf. So I imagine they are similar, with IDLs and serialization approaches of their own. I took a quick look at CapNProto's Python module documentation and it looks very similar to thriftpy in philosophy, but I don't know its intricate details as I haven't used it.

This guide to Thrift covers all the important details of its schema approach and is meant as a cross-language guide. It should be the official docs, but alas, it's not!

https://diwakergupta.github.io/thrift-missing-guide/

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#28

More protobuf implementations is certainly good, Google's have been really really lacking for us. It would be really nice if there was a pure Python implementation that didn't use tons of unnecessary metaprogramming.

It's really interesting to me that the top two comments on this story both wish Protocol Buffers were different, but seem to be asking for opposite things. Your comment wishes that there was a pure-Python implementation that doesn't do metaprogramming. That would imply doing code generation, but having the generated code be more "concrete", so-to-speak (containing all of the actual implementation). However, a differe…

I think we learned a long time ago that though codegen is one path to speedy code, it is not the only path.

The thriftpy module dynamically generates a Python module that is based on parsing the thrift schema. That happens once: at module import time. After that, Python has cached the module in sys.modules and it doesn't need to be evaluated again during the program's runtime.

The module contains efficient Python classes that are Python bytecode just the same way "compiled" classes would be. But, thriftpy has Cython implementation of Thrift protocols and transports that compile down to C, not Python bytecode. Thus, the thriftpy impl's are not only more dynamic and less cumbersome than the codegen alternative, but they are also faster.

In the Python community, we prefer to take other approaches to achieve execution speed.

Dynamically generating a Python module is not "metaprogramming". It is not black magic. You can generate a module yourself simply by instantiating a Python module type. Hooking the import statement is an officially supported part of the language, and done by many libraries.

This may all seem very worrying to a C++ or Java programmer, but in the Python community we have been doing dynamism with execution speed for about 10 years now, and we haven't regretted any of the results!

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#29
post #3

Earlier quoted context omitted.

Don't bother, go directly to cap'n proto: https://capnproto.org

There are a couple of reasons I chose FB over Can'n Proto, Python and full C++ doesn't work on MSVC/Windows (this might have changed recently especially given the Clang compiler integration into MS backend) Second problem is Capnproto doesn't have structs, ie. you want something like struct Vec3 {x: float, y: float, z: float} in CapnProto you have to either make Vec3 a ref type (+ pointer size and lookup indirection…

> the serialization part isn't separate they are in one library

Although Cap'n Proto's C++ implementation is hosted in a single git repository, it does compile to several distinct libraries. You can use just the core serialization/deserialization part, libcapnp, if that's all you need. There are separate components, libcapnpc and libcapnp-rpc, for dynamic reflection and the object-capability remote procedure call system.

Re: Pyrobuf: A Cython Alternative to Google's Python Protobuf Library

#30

More protobuf implementations is certainly good, Google's have been really really lacking for us. It would be really nice if there was a pure Python implementation that didn't use tons of unnecessary metaprogramming.

It's really interesting to me that the top two comments on this story both wish Protocol Buffers were different, but seem to be asking for opposite things. Your comment wishes that there was a pure-Python implementation that doesn't do metaprogramming. That would imply doing code generation, but having the generated code be more "concrete", so-to-speak (containing all of the actual implementation). However, a differe…

I've certainly observed the same I think :) and yeah pleasing everyone is impossible -- I think though there's a somewhat coherent way to put at least part of those two things together.

Not wanting code generation might come from people who don't like the additional build step and artifact deployment. I don't like that either, but what I mean by "less metaprogramming" is that the code that's generated currently makes copious use of descriptors, metaclasses, and other "complex" things in a way that makes it extremely JIT-unfriendly on PyPy. We had to completely abandon it in favor of using protoc-c wrapped via CFFI. So I care about speed (almost entirely -- protobuf powers systems for us that do ~350K messages per second).

(I do though think it would be possible to satisfy both complaints simultaneously. I spent about 5 minutes trying to write a codegen-less, pure Python implementation: https://github.com/Julian/pb/tree/master/pb but I didn't find the Proto3 docs mature enough to figure out the binary protocol at the time. Not sure there's enough there to see what direction I was trying to go in, but I have given this a shot before).

And thanks for the reply, I do agree that there is some conflicting concerns involved here.

Post reply on HN