Live data from Hacker News

What happens if you write a TCP stack in Python?

jvns.ca

31–40 of 125 posts

Re: What happens if you write a TCP stack in Python?

#31
post #13

Earlier quoted context omitted.

Indeed

It confuses me that people make such a big deal of their little 20 lines of code toy projects.

I don't think it's presented as a "big deal." I read it as a fun little experiment.

Re: What happens if you write a TCP stack in Python?

#32
post #29
post #23

The idea that Python is so slow that it's confusing TCP sounds wrong to me. I think it's more likely that your packet capture scheme is slow. It looks like you're using scapy, which I assume is in turn using libpcap... which may be buffering (in high-performance network monitoring, the packet capture interface goes out of its way to buffer). Which is something you can turn off. About 13 years ago, I wrote my own prog…

my favorite thing about writing blog posts is comments like this. Thank you! I didn't consider that the packet capture interface might do buffering. That might explain a lot of the problems I was having :)

This is a great project. Keep playing with it! You might find that the serverside of TCP is more useful to have in Python than the clientside (having a userland IP/TCP serverside allows you to create fake hosts out of thin air).

Re: What happens if you write a TCP stack in Python?

#33
If an 8MHz microcontroller is fast enough to implement TCP, then Python should be fast enough too.

Here is my two cents on the expirement:

1. You don't really have to ack every packet, you have to order them, drop duplicates and ack the last one.

2. Google ignores the TCP flow control algorithm and sends the first few hundred packets very quickly without waiting for acks. They do this to beat the latency of the flow control algorithm. That's why you end up with so many packets on your side. You could just try anything but google, and you would probably see that you have a less insane packet storm.

Re: What happens if you write a TCP stack in Python?

#34
post #13

Earlier quoted context omitted.

Indeed

It confuses me that people make such a big deal of their little 20 lines of code toy projects.

More confusing part for me is that I knew there is TCP/IP stack for ages. TCP stack is all new to me.

Re: What happens if you write a TCP stack in Python?

#36
post #13

Earlier quoted context omitted.

Indeed

It confuses me that people make such a big deal of their little 20 lines of code toy projects.

It confuses me that instead of finding these projects uninteresting (as you claim) you took the time and effort to write a reply in a thread just to belittle and dismiss the article.

I for one found it interesting/fun.

Re: What happens if you write a TCP stack in Python?

#37

Fun read. Does anyone here know how to deal with the Python being slow at sending ACK packets problem? Or is it a built-in limitation that comes with dealing with high level languages?

I'd start with the question of 'why so much ACKing?'. In TCP you don't normally ACK every packet. You really only need one ACK per receive window, not one ACK per packet which is what the op's code appears to be trying to do. It also doesn't look like op's code is setting the window size anywhere so in effect she's saying to google's server 'ok got that one, send me a bunch more' and trying to do that for every packet at fast as possible is creating a runaway congestion problem.

The best solution doesn't have anything to do with python, it's to implement window size (flow control) and only ack when you need to.

ps - It's been several years since I've worked on a TCP stack so please correct anything I'm remembered incorrectly

Re: What happens if you write a TCP stack in Python?

#38
post #28
post #7

Earlier quoted context omitted.

The answer to questions about performance is always "profile it". I mean, sure, you could probably get a crude speedup by just running it under pypy or some such, but the real answer is to profile it, see where the bottleneck is, and fix it. As the other post suggests, this is not a problem in all high-level languages; it's possible to write very high-performance code in Haskell or Ocaml, for example. But python's se…

So you're saying that late binding is the most significant reason why Python is slow? Is slowness just an inherent tradeoff in using a language that supports this powerful feature?

I'm not an expert but that's my understanding. Remember that it applies to some scopes as well as to object properties. In benchmarks the big divide I've seen is between languages that allow this and languages that don't.

You can do sophisticated things where you compile objects assuming they won't be overridden and then back out the compile if something touches an object (the JVM does similar things where it will compile a never-overridden method as non-virtual and then detect when the class hierarchy changes), but that requires a lot of complexity that goes against the goals of CPython.

Nowadays I mostly work in Scala, and anywhere where I would have used such a technique I find there's a way to do it "statically". So I'd be interested if there are good examples of what makes this a "powerful" technique, and to see if I can't replicate them "statically" with enough typeclasses etc.

Re: What happens if you write a TCP stack in Python?

#39

Fun read. Does anyone here know how to deal with the Python being slow at sending ACK packets problem? Or is it a built-in limitation that comes with dealing with high level languages?

Implement it in C. Also its likely more a python problem than a "high level" language problem.

This has nothing to do with Python or C. More likely he has a bug in his code.
Post reply on HN