Live data from Hacker News

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

github.com

31–36 of 36 posts

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

#31

Earlier quoted context omitted.

Perhaps they should do more marketing, because speaking as a C++ developer: I picked between protobufs, flatbuffers and capt'n proto because they were easy to use, had active communities, and they had websites which explained how and why I should use their compiler/library/protocol. When I search the web for information about asn.1, I find very little that is of practical use. What library should I use? Why should I…

you are dismissing an old and proven technology because you feel it is not marketing to you effectively, which is the backend to the global telecom system (ss7) for the past two decades? why is this a movement in computer science to throw out old things that work? this is confusing to me like nosql. but "good marketing" is something that is called cap'n proto, sounds like a joke? if you are curious here is a good blo…

Well, the only location many programmers nowadays stumble over ASN.1 is in certificates. And ASN.1 parsing from them has a history of massive security issues and results in massive warnings of "do not touch!".

And yes, "good marketing" in the form of readily available and well-documented libraries for the languages we use is a very important factor.

I bet the telecom sector has their battle-tested libraries for ASN.1, or at least for the parts they use. Are they open-source? Are they available for all languages wanted? No? Then why would I use ASN.1, just to use a "standard", if it means using worse code or writing it myself?

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

#32

Earlier quoted context omitted.

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…

Ah, that's very interesting -- your desire for more "pure" Python comes from a desire for speed. (Some people who wish the generated code was more concrete want this so the generated code is more readable).

I am curious what led you to the conclusion that descriptors and metaclasses were making Python protobuf JIT-unfriendly. I ask because most of the "meta" stuff happens at startup, when you first import the module. It uses a metaclass to generate a bunch of Python methods, but after that they are just regular Python methods, and should be as easy for PyPy to optimize as anything else. There is a bit of reflection happening in some code-paths (like the __init__ method does loop over a list of fields to decide how to initialize them), but the metaclass at least is pretty much gone after import.

For this reason, I suspect that even if we ditched all the metaclass stuff, you'd see PyPy performance pretty close to what you're seeing now. The only thing I could see potentially making a more significant difference is if there were generated parsing code that switches on field number, instead of looking up fields by number in a dictionary. But given that Python doesn't have an actual switch statement, this might not be an improvement at all.

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

#33

Earlier quoted context omitted.

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 cla…

For what it's worth, I prefer the approach you describe. I have advocated for it for a long time. I've been writing C extensions to accelerate parsing in dynamic languages for 10 years, and I'm very familiar with this sort of dynamism.

It's interesting though that you describe this approach as more Pythonic, because one of the most outspoken critics of this approach is a hard-core Python guy that I work with who has lots of experience with the Python ecosystem. He feels very strongly that it is more Pythonic to have very flat/concrete generated code that is transparent to the reader, and really does not like the idea of hooking import and generating everything at runtime.

This is exactly what I am describing about how it is hard to please everybody. Different people appear to have fiercely different opinions about what is idiomatic.

When we wrote the Ruby protobuf implementation this year, we took an approach much more along the lines of what I personally prefer. The extension is mostly implemented in C, and it's very easy to build types at runtime. It doesn't directly import .proto files (which I would have preferred) because there was still some desire from others to have some kind of code generation. So the approach we took was to use a Ruby-like DSL for describing the protobuf schema. But really this is nothing but a translation of the .proto file into the Ruby DSL. ie. take the .proto file:

  syntax = "proto3";

  message Test {
    int32 foo = 1;
    double bar = 2;
    Test test = 3;
  }
The "generated code" for this is simply:

  require 'google/protobuf'
  
  Google::Protobuf::DescriptorPool.generated_pool.build do
    add_message "Test" do
      optional :foo, :int32, 1
      optional :bar, :double, 2
      optional :test, :message, 3, "Test"
    end
  end

  Test = Google::Protobuf::DescriptorPool.generated_pool.lookup("Test").msgclass

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

#34

Earlier quoted context omitted.

Perhaps they should do more marketing, because speaking as a C++ developer: I picked between protobufs, flatbuffers and capt'n proto because they were easy to use, had active communities, and they had websites which explained how and why I should use their compiler/library/protocol. When I search the web for information about asn.1, I find very little that is of practical use. What library should I use? Why should I…

you are dismissing an old and proven technology because you feel it is not marketing to you effectively, which is the backend to the global telecom system (ss7) for the past two decades? why is this a movement in computer science to throw out old things that work? this is confusing to me like nosql. but "good marketing" is something that is called cap'n proto, sounds like a joke? if you are curious here is a good blo…

Sorry to disappoint, but I don't really consider myself a computer scientist. My degree was EE with a focus on communications. I then spent a five years shuffling bits between custom protocols for unmanned vehicles.

The data format isn't hard. It doesn't particularly impress me that asn.1 has worked for 20 years when I've seen hand-rolled formats do the same. The hard part of the process is defining the actual messages anyway.

The part that is valuable is the library and the tooling that makes efficient serialization easy. If it's not easy, it's not useful because I already know how to do it the hard way.

The marketing has a point, by the way. It's an indication that there are people who are willing to put in effort to get others to adopt their tooling. That's a good indication that when I have a problem, that there will be somebody else who's willing to put in effort to help me. Given that I have zero budget to pay for support, that's important.

Thanks for the link, though. There's definitely some useful information there.

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

#35

Earlier quoted context omitted.

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…

Ah, that's very interesting -- your desire for more "pure" Python comes from a desire for speed. (Some people who wish the generated code was more concrete want this so the generated code is more readable). I am curious what led you to the conclusion that descriptors and metaclasses were making Python protobuf JIT-unfriendly. I ask because most of the "meta" stuff happens at startup, when you first import the module.…

Hi

The python code that's generated is ok, but the problem is that everything is very dynamic. If you want this to work fast on PyPy, it should really generate a bunch of getters and setters that use attribute access and not go through generic __getattr__ that does some dictionary lookups. Additionally a lot of code is written in a way that creates a lot of temporary lists iterating over all fields and double checking stuff - this is bound to be way slower than a simple C/Java stuff that does the very simplest "check type - attribute access" sort of stuff.

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

#36

If this gets python 3 support before google's official implementation I would be much more inclined to give it a shot, but it doesn't look like it's compatible yet either.

Try now - I just merged in some changes to make it Python 3 compatible.
Post reply on HN