Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

(perf) std::list => std::vector - #99

Closed
mathieucarbou wants to merge 1 commit into
mainfrom
vector
Closed

mathieucarbou wants to merge 1 commit into
mainfrom
vector

Conversation

@mathieucarbou

Copy link
Copy Markdown
Owner

No description provided.

@mathieucarbou mathieucarbou self-assigned this Sep 8, 2024
@mathieucarbou

mathieucarbou commented Sep 8, 2024 •

Copy link
Copy Markdown
Owner Author

@vortigont : would you be able to look at this pr please ?
I think slowing down removal of handlers and rewrites to decrease memory used for each item is acceptable. There are not a lot of removal of rewrites and handlers in an app.

@mathieucarbou
mathieucarbou force-pushed the vector branch 2 times, most recently from 5b36a91 to 96bbef2 Compare September 8, 2024 10:54
@mathieucarbou mathieucarbou changed the title std::list => std::vector std::list <=> std::vector Sep 8, 2024
Comment thread src/ESPAsyncWebServer.h Outdated

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will speed up request processing by avoiding array reallocations at the expense of taking a little more memory because adding interesting headers is done for each entering request

Comment thread src/ESPAsyncWebServer.h Outdated
Comment on lines 630 to 631

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will decrease memory usage using array at the expense of slower insertion and removal which is acceptable for rewrites and handlers which are not often added or removed

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it depends on realization actually. vector usually preallocate certain size on initialization, so in case of rewrites it might take mem even when no rules are added at all. So I'm not sure that this change makes much sense considering mem allocation. Access pattern could be another concern but since container keeps pointers to other objects and those are accessed/added sequentially and removed randomly I was thinking that linked list could be a good compromise choice here.

@vortigont

Copy link
Copy Markdown
Collaborator

Hey @mathieucarbou!
Was quite busy lately, mostly skipped lot's of your latest work.
Pls, give me a day or two to check on this carefully.
Tnx!

@mathieucarbou
mathieucarbou force-pushed the vector branch 2 times, most recently from a5432f5 to 46fef12 Compare September 9, 2024 22:05
@mathieucarbou mathieucarbou changed the title std::list <=> std::vector (perf) std::list => std::vector Sep 10, 2024
@vortigont

Copy link
Copy Markdown
Collaborator

Hey @mathieucarbou! Pls see my comments inline. Will try to suggest some ideas on weekend, still busy with main job :(

@mathieucarbou

Copy link
Copy Markdown
Owner Author

Hey @mathieucarbou! Pls see my comments inline. Will try to suggest some ideas on weekend, still busy with main job :(

no problem. i am still testing the middleware PR.
Note: I don't see your comment, but don't worry, I'll wait.

@vortigont vortigont left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comments

Comment thread src/WebServer.cpp Outdated

@vortigont vortigont Sep 9, 2024 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks as a design gap for me.
Comparing a const char* pointers is not the same as comparing the literals it points too. There could be false negatives where two different pointers points to two identical literals but in different mem places, i.e. one comes from a const char* in ROM and another via some UI's acquired var.
Here is a nice place to use std::string_view but it needs c++17. Otherwise have to use a String or std::string to do actual string comparison here if needed or simple std::strcmp if no wide unicode magic required.

Comment thread src/WebServer.cpp Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the most ugly part of the legacy code as for me. Comparing a pointer to an object with a list of unique pointers is so wrong. If it's a unique pointer then it can't share lifetime of an object with any other pointer by design. It's a hack due to keeping compat method allowing to add handlers via pointer but having and unique_ptr container.
I do not have an easy way to make it right and not to change the API. Maybe postpone the change for some breaking the API major release.
I would rather expanded the AsyncWebHandler class with some kind of id member or make a container of std::pair with id:AsyncWebHandler* where id is autogenerated on adding the handler. It's just one of the possible options to consider.

Comment thread src/ESPAsyncWebServer.h Outdated
Comment on lines 630 to 631

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it depends on realization actually. vector usually preallocate certain size on initialization, so in case of rewrites it might take mem even when no rules are added at all. So I'm not sure that this change makes much sense considering mem allocation. Access pattern could be another concern but since container keeps pointers to other objects and those are accessed/added sequentially and removed randomly I was thinking that linked list could be a good compromise choice here.

@mathieucarbou

Copy link
Copy Markdown
Owner Author

@vortigont : ok thanks ! I will close the PR then :-) let's revisit that later. Thanks!

@vortigont

Copy link
Copy Markdown
Collaborator

maybe split it into different smaller tasks would be easier to implement/discuss

@mathieucarbou

Copy link
Copy Markdown
Owner Author

maybe split it into different smaller tasks would be easier to implement/discuss

What do you have in mind ?

Do you mean have a project board where we could list some tasks to improve the project?

@mathieucarbou

Copy link
Copy Markdown
Owner Author

@mathieucarbou
mathieucarbou deleted the vector branch September 13, 2024 23:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants