Optimization: Allow use of codon python machine code compiler in HA codebase

Hi,
I think HA would benefit a lot if it was compiled to machine code. I did not think it was possible until I learn about the codon python compiler.

The fact is that HA is a relatively large codebase almost all written in python. The main problem with this language is performance, and I’ve recently discovered GitHub - exaloop/codon: A high-performance, zero-overhead, extensible Python compiler using LLVM that IMO is very promising and allows to compile python to actual machine code, using the LLVM stack (same as clang, a C++ compiler I’m very familiar with).

I’d like to hear if people think they would be interested in this because it might be a big effort to integrate codon into HA, event if I think we should start by targeting small key parts of the codebase that will greatly improve performances of HA in general.

First step would be to integrate codon itself, ideas I have:

  • have it in the CICD and provide some parts as compiled binaries
  • have it as a module/builtin and compile at load time. This would allow live patching without having to fallback to cpython
  • start by experimenting with a custom component that provide the binaries

There would also be things to consider regarding the licensing, as codon is BSL 1.1

I’d of course personally participate in this effort, I’m an experienced C++ developer and I’ve been using HA for a while. I have some knowledge of python but not that much as the projects I am working on are highly focusing on performances.
What do you think?

I am surprised to hear it is not. Ditto all the way

Where in HA would we notice the improved performance? I can barely get my NUC above 10% cpu. Most of that is from addons. I experience no lag, even dashboards load fast. Where I’d appreciate more performance is compiling for ESPHome, but that is not python.

Furthermore, the last commits (save one) are 2 months ago, most commits are by one person. The docs were last updated a year ago, async/await is still on the roadmap. Several confirmed bugs are a year old and not fixed. I would hate Home Assistent to be depending on this, while it is not so sure this is here for the long run. If the contributors pull the plug, Home Assistant dies with it?

1 Like

Pot, meet kettle. :smile:

Fair enough, but a compiler bug is worse than a regular software bug.

1 Like

You have fair points, especially about the fact it’s more or less maintained… I did not notice TBH, and that would kill the idea indeed.

Either way, I would definitely make sure HA does not have a strong dependency on this, but instead target functions using Python decorator | Codon or compiling to Python extensions | Codon. This would not stop us from using CPython event for parts that have good performance improvements, say if there is a bug introduced by codon.

About CPU usage, I’m pretty sure it would help many people even if your setup is not too CPU intensive. This is not my case.

The compiler would have to support all the same hardware architectures as standard Python it it would be a setback.

Of course. LLVM supports all architectures you can think of.

Where is all your CPU going to then, not counting addons? If it going towards massive amounts of polling, faster code is not going to help your performance.

It is not like I have a lean setup, I tend to install everything I think might be fun. I have over 20 addons and over 110 integrations. I’m running out of things to add.

I don’t want this thread to drift on how I can improve my setup. Thanks for your input tho, I hear that HA is already fast enough for you.

I was not out to improve your setup, but trying to find out where your bottlenecks are. Because I suspect you are hoping faster code will solve problems that are not caused by slow running code. Systems like Home Assistant are likely primarily IO restricted. An RPI3 would still do fine for most use cases, were it not it is too restricted on ram. And systems with sufficient ram usually have decent cpu’s.

So that is why it is a shame you did not answer the question where you are experiencing slowness that you hope to solve with faster code execution. But I’ll back down now.

Maybe you should substantiate your thoughts by pointing out parts of the code that would benefit, and what would be the performance gain that end users could expect from compiling that. Because you clearly seem to know your way around code (Honest remark, not meant as sarcastic).

2 Likes

This is a moot point if that compiler doesn’t support async. And you’d have to get the main devs on board, I highly doubt they would back this. I personally don’t see any benefit to this. Especially after the python 11 and 12 optimizations and backend rewrites.

1 Like

You are right, I don’t think it worth the effort with all of this in mind.
I am unable right now to run a profile session on the hardware that is having issues (a rpi3 indeed), it’s not even able to generate the profile, so it’s hard for me to say what is the bottleneck.

I think I’ll start by investigating the slowdown first. I suspect a database corruption leading to log flooding. Yes: all things that could have happened with a compiled language.

1 Like