Live data from Hacker News

Show HN: Python can make 3M+ WebSocket keys per second

github.com

51–60 of 75 posts

Re: Show HN: Python can make 3M+ WebSocket keys per second

#51

The hardest part of debugging Python is "hitting the wall" when you come to a native library (compiled C code). And Python has achieved a lot of speed-ups from Py2 to Py3 by adding more compiled C code. This is a real blocker for understanding the foundation library. On the other hand, in Java, with a few exceptions around Swing (native painting for GUIs) in Java, almost everything is written in pure Java, so you can…

Likewise, in C#, almost everything is written in pure C#. It also gives you more control over memory allocations than Java, which is nice when trying to squeeze more performance out of the code.

    It also gives you more control over memory allocations than Java
Cool! I didn't know about this. Can you share an example? It might foster some good discussion. I haven't written any serious C# in about 10 years now. I still love that language. To me, it's like Java with all the rough edges sanded down. In that era, Visual Studio was the only choice for dev and needed the IntelliJ plug-in. I always thought that was a bit goofy, but nothing against the language itself. And, the COM+ integration is legendary if you need to run on Win32.

Re: Show HN: Python can make 3M+ WebSocket keys per second

#52

Earlier quoted context omitted.

Java is faster than C#, that's not "bike-shedding farce", that's a documented, and benchmarked fact JSon serialization: 20+% faster: https://www.techempower.com/benchmarks/#section=data-r21&tes... Single query: 20+% faster: https://www.techempower.com/benchmarks/#section=data-r21&tes... Multiple query: 20+% faster: https://www.techempower.com/benchmarks/#section=data-r21&tes... Data update: 15% faster: https://www.te…

Why is this being downvoted? Sure the opening line isn't the most gentle, but these are some crazy benchmarks. I never saw this website before. First link: How is it possible that Java can achieve same perf as C??? I write a lot of Java, and none of my stuff is close to C. I guess about 50% the speed -- which fine for my needs. One reason (totally unscientific, of course) I think Java is faster: The virtual machine h…

> The virtual machine has been open source for longer. So more academics have looked at, run experiments, written papers... that are then read by the core Java team and sometimes implemented. C# is a bit behind, but should catch-up one day.

That's what it is indeed, more R&D went into Java

C# will catch up eventually, specially now that they invest massively on PGO/AOT and ways to minimize heap allocations (stackallock and struct reference for example)

Re: Show HN: Python can make 3M+ WebSocket keys per second

#53
post #30
post #20

Earlier quoted context omitted.

On the other hand, you still have to learn and use Java. I think learning Python and a touch of C is easier than trying to learn Java. Heck I'd go so far as to say C is a lot easier than Java. Frankly I don't understand why having to learn a little bit of C to debug a python program is such a red flag or wall to you, considering the sheer amount of learning one has to do to use Java at all. You're setting up an uber-…

> You're setting up an uber-sophisticated IDE You just install it. > learning a little C and potentially fixing an up-to-date and beloved library A romantic thought, but 99% of the time I'm just going to do a workaround or a local patch. I don't use Java anymore, but I don't hate it. I think it has some verbose conventions, but I vastly prefer it to C's extremely terse conventions. Nowadays I try to do as much in Typ…

Monkey patching in-house proprietary libraries without source code is also such a nice feature of Java. (Can this be done with C#? I assume yes.)

Re: Show HN: Python can make 3M+ WebSocket keys per second

#54
post #43

Earlier quoted context omitted.

It’s cool that languages support this kind of stuff. So if you’re writing python the employs this technique and you still want to keep the “OS agnostic” characteristic of python does that mean you’d have to compile multiple C++ binaries and check the OS to see which one to run?

Alternatively, you could compile the C++ (or rust or zig) to WebAssembly, and a single binary would run anywhere.

WWouldn't you lose some of the performance benefit?

Re: Show HN: Python can make 3M+ WebSocket keys per second

#55
post #4
post #2

The title should be “optimization-demo” (original title) or “Replacing parts of Python programs by C++ can be easy and profitable”. They replace Python code that makes 5 calls into native code by code that makes 1 call that makes those 5 calls, and get a speed up from 869k calls per second to 3.15m calls per second, so a snarky title could even be “Python-to-native calls are slow”. They could even measure it by addin…

Did you take a look at the C++ implementation of the hashing function they did? I didn't see a single call made there. They replaced python code that makes 5 calls into a single call.

I didn’t. I expect that it makes a difference, though for the observation that Python native calls are relatively slow.

Re: Show HN: Python can make 3M+ WebSocket keys per second

#56

Earlier quoted context omitted.

Java is faster than C#, that's not "bike-shedding farce", that's a documented, and benchmarked fact JSon serialization: 20+% faster: https://www.techempower.com/benchmarks/#section=data-r21&tes... Single query: 20+% faster: https://www.techempower.com/benchmarks/#section=data-r21&tes... Multiple query: 20+% faster: https://www.techempower.com/benchmarks/#section=data-r21&tes... Data update: 15% faster: https://www.te…

Why is this being downvoted? Sure the opening line isn't the most gentle, but these are some crazy benchmarks. I never saw this website before. First link: How is it possible that Java can achieve same perf as C??? I write a lot of Java, and none of my stuff is close to C. I guess about 50% the speed -- which fine for my needs. One reason (totally unscientific, of course) I think Java is faster: The virtual machine h…

> Why is this being downvoted? Sure the opening line isn't the most gentle, but these are some crazy benchmarks. I never saw this website before.

I guess people don't like getting fact checked

Re: Show HN: Python can make 3M+ WebSocket keys per second

#57
post #43

Earlier quoted context omitted.

It’s cool that languages support this kind of stuff. So if you’re writing python the employs this technique and you still want to keep the “OS agnostic” characteristic of python does that mean you’d have to compile multiple C++ binaries and check the OS to see which one to run?

Alternatively, you could compile the C++ (or rust or zig) to WebAssembly, and a single binary would run anywhere.

Thanks for this. This is something I wouldn’t have considered.

Re: Show HN: Python can make 3M+ WebSocket keys per second

#58

Earlier quoted context omitted.

Likewise, in C#, almost everything is written in pure C#. It also gives you more control over memory allocations than Java, which is nice when trying to squeeze more performance out of the code.

It also gives you more control over memory allocations than Java Cool! I didn't know about this. Can you share an example? It might foster some good discussion. I haven't written any serious C# in about 10 years now. I still love that language. To me, it's like Java with all the rough edges sanded down. In that era, Visual Studio was the only choice for dev and needed the IntelliJ plug-in. I always thought that was a…

c# has structs and span, which makes memore allocation more easy. in java the thing gets easier as well (soon) but java still has no value types and than there is sun.misc.Unsafe which is still a special case and a really really old api, in jdk 19 some new apis were introduced which lifts some of these stuff like panama preview and a new vector api.

Re: Show HN: Python can make 3M+ WebSocket keys per second

#59

Earlier quoted context omitted.

Likewise, in C#, almost everything is written in pure C#. It also gives you more control over memory allocations than Java, which is nice when trying to squeeze more performance out of the code.

It also gives you more control over memory allocations than Java Cool! I didn't know about this. Can you share an example? It might foster some good discussion. I haven't written any serious C# in about 10 years now. I still love that language. To me, it's like Java with all the rough edges sanded down. In that era, Visual Studio was the only choice for dev and needed the IntelliJ plug-in. I always thought that was a…

Value types have gained more functionality in recent versions.

You can quickly create record structs or value tuples (that auto-implement Equals(obj) & GetHashCode()) and pass them around by reference instead of copying.

  record struct RGBA(byte R, byte G, byte B, byte A);

  // allocate a small collection on the stack
  Span colors = stackalloc RGBA[3]
  {
      new RGBA(255, 0, 0, 255),
      new RGBA(0, 255, 0, 255),
      new RGBA(0, 0, 255, 255)
  };

  // get value by reference
  ref var c = ref colors[2]; 
  c = new RGBA(127, 127, 127, 255); 
  
  // prints: RGBA { R = 127, G = 127, B = 127, A = 255 }
  Console.WriteLine(colors[2]); 

  // value tuple
  (byte R, byte G, byte B, byte A) white = (255,255,255,255);
Heap allocated generic collections of value types have better data locality than collections of reference types.

Ref struct is a type of struct that is always stack allocated and cannot be promoted to the managed heap or boxed. `Span` is ref struct.

Spans allow you to create views over contiguous regions of memory that is located on the stack or on the heap or over native memory. You can pass spans to methods that then read/modify the data in the view.

And many other features that help you manage memory or avoid allocations.

For example. When working with interfaces you can avoid boxing allocation of value types by using a generic type constrained to the interface instead of the interface.

Re: Show HN: Python can make 3M+ WebSocket keys per second

#60

Earlier quoted context omitted.

Why is this being downvoted? Sure the opening line isn't the most gentle, but these are some crazy benchmarks. I never saw this website before. First link: How is it possible that Java can achieve same perf as C??? I write a lot of Java, and none of my stuff is close to C. I guess about 50% the speed -- which fine for my needs. One reason (totally unscientific, of course) I think Java is faster: The virtual machine h…

> Why is this being downvoted? Sure the opening line isn't the most gentle, but these are some crazy benchmarks. I never saw this website before. I guess people don't like getting fact checked

because you should really look into these frameworks if you really think its a good comparsion. if you exclude all the unserious java frameworks you will be stuck with vertx, after that there is a huge list of c# and than there is the rest of java. I mean yes, these frameworks might be the next big thing in the java world, but once they are there, they will at least loose 20% of performance. (p.s. you can also remove the first c# entry, because no sane person will write everything as a middleware)
Post reply on HN