News for hackers. Stuff that matters.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by the_duke on Sunday November 15, 2020 @07:20AM (#25100605)
This is essentially some Python glue for C code.

It uses a C Redis client parser (hiredis), uvloop (Python wrapper for libuv) and the Python hashmap implementation.

All the performance critical code is implemented in C.

I appreciate the point that higher level languages can be more than fast enough for many use cases. A tremendous amount of software is written in Python, PHP, Ruby et al after all.

But this is not a good way to make that argument, since it almost refutes it's own point: if performance matters, you apparently implement it in C/C++/Rust.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by boramalper on Sunday November 15, 2020 @08:57AM (#25101200)
Hi, author here!

Copying my comment from the previous submission[x]:

This was a quite common criticism over r/Python[0] as well, saying that using C modules and then benchmarking "Python" with C isn't fair.

Perhaps it was my wording that caused this confusion, for which I am sorry, but I never meant to compare "pure" Python with C. The point I am trying to make is that, Python with C extensions can be as considerably[1] performant as C code for network or memory bound tasks.

[0]: https://www.reddit.com/r/Python/comments/awav6k/pydis_a_redi...

[1]: It's of course controversial whether 60% throughput is considerable or not.

[x]: https://news.ycombinator.com/item?id=19293590

Re:Pydis – Redis clone in 250 lines of Python, for performan... by rualca on Sunday November 15, 2020 @09:17AM (#25101333)
> The point I am trying to make is that, Python with C extensions can be as considerably[1] performant as C code for network or memory bound tasks.

You don't make that point. You instead make the point that calling C code with glue code is slower than C code alone, and the performance hit of using Python as glue code in your case is a 2x slowdown.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by noname120 on Sunday November 15, 2020 @12:42PM (#25103029)
What caused confusion is that you put “written in ~250 lines of idiomatic Python code” in the readme file and didn't mention anywhere that you were in fact using a bunch of C libraries to do the heavy work.

The disclaimer section (which contains this information) was added 44 minutes ago. And it's still misleading. For example about hiredis you write:

> Python extension that wraps protocol parsing code in hiredis.

Where is the mention that the parsing code is actually C code?

------

Last but not least you already posted this a year ago on Reddit[1] and Hacker News[2] (edit: someone else did for HN), but only (partially) addressed criticism very recently.

[1] https://www.reddit.com/r/Python/comments/awav6k/pydis_a_redi...

[2] https://news.ycombinator.com/item?id=19287717

Re:Pydis – Redis clone in 250 lines of Python, for performan... by boramalper on Sunday November 15, 2020 @12:53PM (#25103145)
I don't like your tone; this was a one-off experiment (that did not even gain much attention back then) that I did for fun, and no one is entitled to my time so I have addressed those issues when I wanted to.

I am not the one who posted here, why so aggressive?

Re:Pydis – Redis clone in 250 lines of Python, for performan... by noname120 on Sunday November 15, 2020 @01:03PM (#25103235)
Fair point, here is a pull request that addresses my criticism: https://github.com/boramalper/pydis/pull/15
Re:Pydis – Redis clone in 250 lines of Python, for performan... by pastage on Sunday November 15, 2020 @03:49PM (#25104679)
> as long as the heavy work is done by calling native non-Python code.

Do not merge that. I once realized that grep is really fast, and replaced our custom solution with bash script, but I still claim that app is written in idiomatic PHP/python/ruby. This project has a lot of problems that it depends on C code is only a symptom. Noting all those bad things in a project Readme is a trap I fall into way to often it never works.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by smoyer on Sunday November 15, 2020 @03:15PM (#25104389)
I don't think the tone was hostile and if you ignore any hint of tone you might hear, you have to admit that the title is really click-baity (yes I understand that you didn't post it). Regardless of the criticisms of the project, it's great that you're experimenting and I would be curious to know what's causing the reduction in performance ... is it only part of the program or is that the performance of the Python code being interpreted.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by boramalper on Monday November 16, 2020 @02:34AM (#25109105)
> I would be curious to know what's causing the reduction in performance ... is it only part of the program or is that the performance of the Python code being interpreted.

Mostly the latter but also a bit of the former: I could have optimised it further — at the expense of idiomaticity — but I believe gains would be marginal. Other than that, Python is not that fast so you pay some penalties there too.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by balena on Sunday November 15, 2020 @06:48PM (#25106152)
Don't listen to these mindless code golfers. Your experiment does indeed prove your point imho.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by tutfbhuf on Sunday November 15, 2020 @09:21AM (#25101372)
From your README.md:

> pydis is an experiment to disprove some of the falsehoods about performance

and your comment:

> The point I am trying to make is that, Python with C extensions can be as considerably performant as C code for network or memory bound tasks.

So your point is regarding the falsehood belief that python with heavy use of C extensions is (not) much slower (in this case ~60%) than C? I don't think that such a falsehood belief exists and therefore pydis disproves nothing.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by jnwatson on Sunday November 15, 2020 @12:09PM (#25102682)
You're missing the point. Obviously, you can write your application in Cython and have decent performance.

The entire purpose is to demonstrate that you can build a reasonable performance application built on high performance libraries and stick with idiomatic Python code.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by sushshshsh on Sunday November 15, 2020 @12:27PM (#25102871)
This, one hundred percent. There's 3 tiers of things happening here.

1) Write Redis in C. Hard for some devs who only know Python, but good performance.

2) Write Redis in Python with C libraries. Doable for a Python dev, performance hit.

3) Write Redis in pure Python. A fun experiment to see the performance limitations of Python when compared to C running on the same machine.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by ZephyrBlu on Sunday November 15, 2020 @01:51PM (#25103624)
The thing is that the title/project makes you think it's 3), when it's actually 2).

It feels a bit sensationalist to me, and I see this all the time with projects these days. It's kind of disappointing.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by UncleMeat on Sunday November 15, 2020 @04:28PM (#25105056)
But the claim is that he is dispelling common falsehoods. Are there actually people out there saying that python glue with c libraries for performance critical parts is going to be orders of magnitude slower than an entire thing written in c?
Re:Pydis – Redis clone in 250 lines of Python, for performan... by sushshshsh on Sunday November 15, 2020 @05:18PM (#25105465)
I actually don't like Python and much prefer Java, but I think #3 is really only for performance research and like, fun and games. It would be cool to have a quantified answer to "just how slow is Python"? I suspect the answer is many orders of magnitude slower.

However, if the experiment in question is, "using python glue code, can a developer string together a service that replicates the functionality of redis", then that illustrates what would actually happen in the real world.

For example, for a very junior Python developer, writing something in C is basically like asking a C developer to write assembly- possible, but they'd have to learn a lot of different things to do it.

Similarly, when I write Java, I want to be calling Arrays.sort(myarray) instead of writing my own sort (if the focus is on developer convenience and development velocity).

So, it's just a difference in what you're desiring to measure.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by rualca on Tuesday November 17, 2020 @12:03AM (#25120430)
> You're missing the point. Obviously, you can write your application in Cython and have decent performance.

"Decent" is a weasel word, which suggests that the performance hit of following this approach is tolerable or acceptable. Sometimes it is, sometimes it isn't. It really depends on where you place the glue code, and where you expect the hot path to be.

In the case of a self-described "Redis clone" which ends up being Redis itself glued back together with Python, I'm not sure you can make that case.

> The entire purpose is to demonstrate that you can build a reasonable performance application built on high performance libraries and stick with idiomatic Python code.

I'm sorry to say but that isn't much of a demonstration. I mean, Python is already widely used as glue code to hold together high performance libraries, and it's already widely known and understood that gluing together code with Python brings in a significant performance hit. And that's ok, because people do it as a tradeoff between performance and some other kind of value-added.

But in this case I fail to see what value-added is being proposed, other than the personal portfolio side of things.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by tutfbhuf on Sunday November 15, 2020 @01:44PM (#25103560)
> You're missing the point.

Well, your claim is that the author has missed the point, because I just quoted the authors point and his intended purpose. So I guess we both agree.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by mekoka on Sunday November 15, 2020 @02:56PM (#25104213)
> I don't think that such a falsehood belief exists and therefore pydis disproves nothing.

You don't think such a falsehood belief exists, therefore you don't think pydis disproves anything.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by zo1 on Monday November 16, 2020 @02:52AM (#25109200)
Some of the comments are nitpicking, for sure. A good benefit of Python is that it abstracts a lower-level language into a higher-order one. So that you get some percentage of the raw speed of the lower-level language but in a "easier-to-use", "easier-to-reason-about" and safer format.

On some level, it's like complaining that C# is just using C++ modules because the CLR that interprets the C# byte-code is written in C++.

But it misses the point. You wrote this in Python and you were able to leverage the speed of a much more complicate language for your benefit. Without touching the lower language and without worrying about it's memory-management or lib dependencies. This is a win, and it's part of the beauty of programming!

Btw, my compliments to you on writing this! Really cool project/poc.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by tylermauthe on Sunday November 15, 2020 @09:33AM (#25101470)
This experiment reminds me of a rant from Ryan Dahl on software a few years back:

> In the past year I think I have finally come to understand the ideals of Unix: file descriptors and processes orchestrated with C. It's a beautiful idea.

We just keep moving up the stack, it seems :)

Source: https://gist.github.com/cookrn/4015437

(edit: formatting)

Re:Pydis – Redis clone in 250 lines of Python, for performan... by sushshshsh on Sunday November 15, 2020 @12:28PM (#25102894)
The older I get, the more I appreciate the simplicty and power of C, text files, a terminal, and TCP/IP
Re:Pydis – Redis clone in 250 lines of Python, for performan... by brian_herman on Sunday November 15, 2020 @10:38AM (#25101963)
If you ran it would pypy would it be faster?
Re:Pydis – Redis clone in 250 lines of Python, for performan... by nemetroid on Sunday November 15, 2020 @07:39AM (#25100725)
I think the use of uvloop is reasonable, since it's not something domain specific to Redis.

But doesn't the inclusion of hiredis invalidate the entire point? It's 7500 lines of C specifically for working with Redis.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by quietbritishjim on Sunday November 15, 2020 @08:51AM (#25101155)
I agree with this.

If you write an application in Python that spends the majority of the time in C-based extensions – for example if I were to write a differential equation GUI using numpy and pyqt – then I think it's still valid to say that I wrote the application in Python. In fact even most of the standard library modules are written in C (e.g. zipfile, io, sqlite3) so it's hard to avoid this. You could say that no program is ever really written in Python but I don't think that's a useful point to make.[1]

But if you write a load of C code that's very specific to your application, wrap it in a Python extension, then call it from Python, then I don't think you could argue that you "wrote it in Python". If someone happened to have written that application-specific extension module for you then the situation is not materially different.

([1] A more useful motion is whether your code is vectorised. That is, if you want to do something with a data structure (e.g. mutate each element, find an element, do matrix multiplication), do you have to loop over the elements in Python or do you make a single call into a library that does the looping in C? Either way you're still ultimately going to be calling C code, if nothing else in the Python interpreter itself, but the distinction is obviously still important.)

Re:Pydis – Redis clone in 250 lines of Python, for performan... by formerly_proven on Sunday November 15, 2020 @08:55AM (#25101184)
The Python zipfile module is actually just that, Python. It performs well enough, with compression zlib is the limiting factor anyway.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by quietbritishjim on Sunday November 15, 2020 @09:06AM (#25101261)
OK, by my own rules that means it's "written in Python" then. But in that case what I actually meant is that majority of time in calls to zipfile is spent in pure C, not in the Python interpreter loop, and if it's based on zlib then that agrees with what I meant.

(There is an exception I didn't mention: if the zip is encrypted then the zipfile module falls back to pure Python code. The difference is very noticeable!)

Re:Pydis – Redis clone in 250 lines of Python, for performan... by lr4444lr on Sunday November 15, 2020 @08:56AM (#25101194)
uvloop also unfairly lets Python take credit for concurrent request handling performance too though, no?
Re:Pydis – Redis clone in 250 lines of Python, for performan... by ufo on Sunday November 15, 2020 @07:42AM (#25100745)
I'm not sure I see what is the problem here. Gluing together pieces of C code is one of the main reasons to use a scripting language like Python.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by timlatim on Sunday November 15, 2020 @07:50AM (#25100782)
The problem is how the author frames it in the README: "an experiment to disprove some of the falsehoods about performance and optimisation regarding software and interpreted languages in particular", "unfortunately many programmers [...] spend countless hours by making life harder for themselves in the name of marginal performance gains, often trading many other conveniences (such as type safety)".

Between the heavy use of C libraries and correctness issues (https://github.com/boramalper/pydis/issues/12), I don't see how this project proves that Python is faster or safer than other languages.

That being said, the speed with which it was developed can certainly be seen as Python's strong point.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by kortex on Sunday November 15, 2020 @10:26AM (#25101872)
I definitely see both sides. Python is meant to be used exactly like this. It's an exemplar of python-as-library-glue.

I also saw the title and clicked on it because I thought to myself "no fricken way...in pure python?" It's a touch baity.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by noname120 on Sunday November 15, 2020 @08:30AM (#25101031)
It's a failed experiment — an experiment nonetheless.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by jpgvm on Sunday November 15, 2020 @07:46AM (#25100761)
Yes but if to achieve good performance in Python you have to implement all the domain specific code (in this case the redis protocol) in C then you:

a) aren't writing Python

b) invalidates all of the points for writing in Python. i.e speed of implementation, etc.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by mattlondon on Sunday November 15, 2020 @08:01AM (#25100841)
> All the performance critical code is implemented in C.

Isn't this why people like python?

Just glue together some libraries that someone else has written and then you write a viral blog/twitter post about your <insert trendy thing> that you wrote in just 10 lines of code?

Seriously is 't this the main reason people use it? I.e. it has a big library of stuff so it is easy for beginners and/or non-programmers to use?

I'm struggling to think of any other reasons to deliberately pick python to start something new in, although it is at least a bit more same now with types.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by jefftk on Sunday November 15, 2020 @09:35AM (#25101487)
Python is an extremely expressive language, that lets you prototype and experiment very rapidly. In many cases, with some cleanups, refactorings, and calls to C modules like numpy, you can convert those prototypes into sufficiently performant real implementations.

Here's a recent write-up of mine where almost all of the time is spent in C extensions but where it was much faster to develop and code in python: https://www.jefftk.com/p/sharding-the-brigade

Re:Pydis – Redis clone in 250 lines of Python, for performan... by melenaboija on Sunday November 15, 2020 @09:34AM (#25101486)
In my professional environment we use python because of the cost, definitely not because we want to post it anywhere.

And this is not an opinion on how companies measure cost or how one language is better than another but maybe trying to explain, and I think underestimate, popularity of Python needs a little bit more insight.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by stingraycharles on Sunday November 15, 2020 @08:05AM (#25100874)
All the reasons you list are indeed reasons to use Python, but none of them require performance critical code to be written in C.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by HelloNurse on Sunday November 15, 2020 @11:07AM (#25102179)
The important technical question is how good a Redis replacement can be built with 250 lines of Python "glue code".

If performance is good enough, where are robustness and features? How large are the effort gap and the quality gap compared to the original Redis?

Re:Pydis – Redis clone in 250 lines of Python, for performan... by ketzu on Sunday November 15, 2020 @08:58AM (#25101207)
I get the point of hiredis and uvloop and agree to some aspect, but including the python hashmap implementation as "implemented in C" feels like claiming python programs do not really exist because in the end it's implemented in C/Assembler/Running CPU instructions.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by didibus on Sunday November 15, 2020 @03:44PM (#25104638)
I think that's because people are kind of forgetting what the concrete thing being discussed even is.

A programing language can be thought of in the asbtract, as simply a syntax for computation and programming.

But if you go in the concrete, a programming language implementation translates language into machine code for a particular machine.

From that angle, the Cython interpreter/compiler is pretty bad at doing that. This experiment actually shows it. It is 40% slower even if all it does is orchestrates the calls between C code.

This is true of hash map as well. Python is so bad at generating efficient machine code from its own language syntax and semantics, that it would be unreasonably slow to implement hash map in Python. Why? Because Python is really slow.

In order to speed up programs written in Python, you need to write more of your program in some other faster language, such as C.

Now if you reuse a bunch of components written in fast languages, like hash map being implemented in C, well you avoid the need to write the C code yourself. But it doesn't make Python any more efficient.

In practice though, you might be able to not have to implement 90% of your application, because there exist C libraries that do most of what you already need. Thus only 10% of your application will have degraded performance due to using Python. And for that 10%, it might be Python is a great choice, because you like the language and productivity, readability and all it gives you in order to implement that 10%, and can deal with 10% of the code being slower.

But if there doesn't exist existing fast libraries you can leverage, well more and more of your program will be Python, and thus it'll get slower and slower, unless you start using C yourself and implementing your own C code libraries.

Yes, that's true of hash map as well. For example, if for any reason you needed a hash map that behaved differently than the standard one, you would most likely need to write it in C to get any performance out of it. Writing it in Python would be just too slow.

So to recap, Python is a slow language, because there are no known interpreter/compiler for it that generate fast machine code for some real machine. This is not a falsehood, but the current reality. But it turns out most applications can leverage existing C libraries for a large amount of their functionality, thus making it that you can build an app that is reasonably performant, with the only code you need to personally implement being implemented in Python.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by pjmlp on Sunday November 15, 2020 @07:30AM (#25100674)
A better point would be .NET 5 server implementation of gRPC.

https://devblogs.microsoft.com/aspnet/grpc-performance-impro...

However Python isn't never going to get there.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by secondcoming on Sunday November 15, 2020 @07:49AM (#25100778)
But the operations are all done on Python's data structures?
Re:Pydis – Redis clone in 250 lines of Python, for performan... by VWWHFSfQ on Sunday November 15, 2020 @07:51AM (#25100786)
aren't they also written in c though
Re:Pydis – Redis clone in 250 lines of Python, for performan... by secondcoming on Sunday November 15, 2020 @07:53AM (#25100797)
Ya. This whole post confused me. I thought it was claiming that the Python port was faster. It's too early in the morning.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by boramalper on Sunday November 15, 2020 @09:01AM (#25101232)
That's taking it too far though isn't it? I mean, gcc is written in C++ too so what? :)

You might also be interested in my other comment: https://news.ycombinator.com/item?id=25101200

Re:Pydis – Redis clone in 250 lines of Python, for performan... by rfoo on Sunday November 15, 2020 @10:28AM (#25101885)
While what you said is true in general, I believe it is debatable in this case, as Redis (at least the subset implemented in this experiment) is something very very special, it is essentially a hashmap attached to the network. There aren't much stuff left if you offload the "hashmap" part and the "attached to the network" (I/O + protocol parser) part. In other words, in this case the hashmap implementation itself is the business logic of your application.
Re:Pydis – Redis clone in 250 lines of Python, for performan... by mhh__ on Sunday November 15, 2020 @08:48AM (#25101138)
The really annoying thing is is that you can quite happily write shorter code in D than in Python and have it run as fast as C++. One of the reasons D compiles so fast is because of this - the compiler isn't even specifically optimized for speed.

Everyone seems to think that writing expressive code is slow, when really it's because Python is slow and C++ is a lumbering behemoth.

Python isn't even a high-level language, think about it - it's higher level than C but the capability it has for abstraction it gives you is really just interpreted C with classes.

Re:Pydis – Redis clone in 250 lines of Python, for performan... by krelian on Sunday November 15, 2020 @09:25AM (#25101398)
Why did D never take off?
Re:Pydis – Redis clone in 250 lines of Python, for performan... by mhh__ on Sunday November 15, 2020 @09:32AM (#25101459)
Bad timing and the GC
Re:Pydis – Redis clone in 250 lines of Python, for performan... by The_rationalist on Sunday November 15, 2020 @08:43AM (#25101104)
It could be significantly faster if using io_uring