Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #832

Closed
abates opened this issue Mar 17, 2025 · 11 comments
Closed

Refactoring #832

abates opened this issue Mar 17, 2025 · 11 comments

Comments

@abates
Copy link

abates commented Mar 17, 2025

Hello,

I've recently been playing with this package, and ended up doing a ton of refactoring in my fork. I'm wondering if you have any interest in the changes I've made? The changes end up reducing the main code by about 25% and the tests by about 75%. I think the code base is close to functionally equivalent.

If you have no interest in these changes, I totally understand. If this is the case, do you have any issue with me publishing under a different name (something like async-unifi maybe)? I will definitely keep the original commits as well as MIT license and attribution if this is the case.

Thanks for all the work you've put into this pacakge!

@Kane610
Copy link
Owner

Kane610 commented Mar 17, 2025

Hello!

I only got to look at it shortly, will look more, in general I'd be happy to make improvements. I welcome contributions.

I'll look a bit more tomorrow. I suggest small PRs moving forward and keeping tests as much as possible intact will keep home assistant integration working, which is my main concern

Lets have a discussion in a couple of days what could be a good starting point

@Kane610
Copy link
Owner

Kane610 commented Mar 19, 2025

Hey! I've looked through it and I like a lot of the changes,

Two things I see as important here

  1. moving the library over needs to be taken in steps to be able to better grasp each change and properly sync changes with the unifi integration
  2. there has been some performance reviews of the library and its important to keep it in a similar state

If you're ok with this we can get going.

/R

@abates
Copy link
Author

abates commented Mar 19, 2025

Hi... This seems reasonable to me. Is there a particular area that you'd like to start?

Regarding performance, I did change from using orjson to Python's builtin json parser. I know that orjson claims to have better performance than the builtin parser, so if there is a performance bottleneck that is something we can look at.

I'm curious in what situations has performance come up? I've used Python's json parser to manage tens of thousands of results in single json documents and the bottleneck is almost always waiting for the response to return from the remote host, not parsing the data within Python.

@thecode
Copy link

thecode commented Mar 19, 2025

I'm curious in what situations has performance come up? I've used Python's json parser to manage tens of thousands of results in single json documents and the bottleneck is almost always waiting for the response to return from the remote host, not parsing the data within Python.

Eventually for Home Assistant which is still support running Raspberry PI3, with large number of integrations that use JSON it starts to accumulate.

Anyhow my best suggestion (also with similar on a repo that I maintain) is to start with smaller PRs as long as things are not related and can be done in separate PRs, it helps reviewing faster and if something is still in discussion other PRs can progress if they don't based on each other.

@abates
Copy link
Author

abates commented Mar 19, 2025

@thecode That's fair.. I use Homeassistant on a Pi (although I'm using a 4), so I'm familiar with some of the resource constraints there.

@Kane610
Copy link
Owner

Kane610 commented Mar 20, 2025

Regarding performance, I didn't even notice your change for orjson, I don't see a reason for that change though, it's very well used HA as well. Im also not sure about two other changes, renaming controller to client (we already have clients in the library, might be confusing), and moving all web requests into the main class rather than keeping it separate under interfaces. Lets discuss each change as we move along.

I meant more that one of the major changes is that you create new objects more often than before and also go through all values, where as properties are used currently so no parsing of data unnecessarily. Not sure if that would make much difference or not, more that Im pointing it out. Not sure if @bdraco has any input on this suggestion.

If it is possible start with api_handlers or models/api preferably in controllable chunks.

I forgot to ask what triggered your rewrite of the library.

@bdraco
Copy link
Contributor

bdraco commented Mar 20, 2025

The JSON overhead was the largest single factor in performance. I do know that I already can't run this on my RPI4b with a large (/21) network without making Home Assistant sluggishly unresponsive in the UI because all the state writes overload the event loop. That's not a problem for a RPI5 or a faster system though, and not a lot of users are going to have a /21. However, will definitely get some complaints if things get slower.

@abates
Copy link
Author

abates commented Mar 20, 2025

Switching back to orjson is easy enough. I was trying to remove as many dependencies as possible, mostly to see what was the bare minimum needed to remain functionally equivalent.

The reason I started hacking away at this was because I wanted to add some additional functionality and I found the existing approach confusing, especially the tests.

I get the feeling that people are happy with how aiounifi is written as-is, and that's fine with me. The changes I want aren't something I care to see implemented in Homeassistant, but in a completely different product that doesn't have to use aiounifi.

@Kane610
Copy link
Owner

Kane610 commented Mar 21, 2025

I get the feeling that people are happy with how aiounifi is written as-is, and that's fine with me. The changes I want aren't something I care to see implemented in Homeassistant, but in a completely different product that doesn't have to use aiounifi.

It is up to you, it will be some effort. I am happy going forward improving the library further, I don't think it will ever be perfect, but more eyes on it with different experiences will help make it better

@abates
Copy link
Author

abates commented Mar 21, 2025

So I wasn't volunteering to open PRs, only asking if there was interest in the changes. Since there is no objection, I will continue with the fork and release a different named package on pypi.

Thanks again for building this client in the first place.

@abates abates closed this as completed Mar 21, 2025
@Kane610
Copy link
Owner

Kane610 commented Mar 22, 2025

I would have much preferred a collaboration and improving this library than seeing more forks and alternatives out there. More eyes and experiences on one code base would benefit more people. But if thats the way you want it that is of course fine as well.

Maybe i will take inspiration from your changes in the future.

Take care

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants