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

Add boolean parameter to clear memory for edge labels from thor #2789

Merged

Conversation

keygang
Copy link
Contributor

@keygang keygang commented Jan 21, 2021

Add boolean parameter to clear memory for edge labels from thor path algorithms.

It will be good for mobile platforms to free extra memory because building a route is a rare task. It frees up about 50mb.

@keygang keygang marked this pull request as ready for review January 21, 2021 13:14
src/thor/astar_bss.cc Outdated Show resolved Hide resolved
@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch 7 times, most recently from a0cd449 to 7390d53 Compare January 25, 2021 13:19
src/thor/astar_bss.cc Outdated Show resolved Hide resolved
src/thor/astar_bss.cc Outdated Show resolved Hide resolved
@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch 2 times, most recently from c49d5a0 to 45e0f0a Compare January 26, 2021 13:42
@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch from 45e0f0a to 8c619dd Compare January 27, 2021 08:37
@kevinkreiser
Copy link
Member

looks like the tests are running into what i expected: #2808

I would suggest we reserve max_reach in the reach constructor by passing it to the dijkstra constructor.

@kevinkreiser
Copy link
Member

@keygang i cleaned up that issue with the resevering too much by default in the reach check on this branch. with any luck the performacne will be unimpacted and we can merge after checking that

@kevinkreiser
Copy link
Member

bad news! performance is absolutely aweful compared to master... im not quite sure where the hell its going though... @genadz any thoughts?

Comment on lines 45 to 48
if (edgelabels_.size() > max_reserved_labels_count_) {
edgelabels_.resize(max_reserved_labels_count_);
edgelabels_.shrink_to_fit();
}
edgelabels_.resize(clear_reserved_memory_ ? 0 : max_reserved_labels_count_);
edgelabels_.shrink_to_fit();
Copy link
Member

Choose a reason for hiding this comment

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

my suggestion was wrong here. when you dont have clear_reserved_memory_ set to true and the edges labels container is smaller than max_reserved_labels_count_ then you dont actually have to resize and shrink_to_fit.\

auto reservation = clear_reserved_memory_ ? 0 : max_reserved_labels_count_; // do this in the constructor and save it as a member variable?
if (edgelabels_forward_.size() > reservation) {
    edgelabels_forward_.resize(reservation);
    edgelabels_forward_.shrink_to_fit();
}

Copy link
Member

Choose a reason for hiding this comment

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

i tried fixing this in bidirectional a* and dijkstras but its still slow... where else are we running into this, it clearly has to be this bit of the code that is causign the performane bottleneck. i'll look at it with fresh eyes in a day or so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kevinkreiser
you haven't watched this PR again?

@kevinkreiser
Copy link
Member

@keygang id love to merge this but there is something wrong with the performance. its incredibly slow. we need to concentrate on what these changes are doing to make the code slower. maybe start with master and add one change at a time to see which one is doing it. i had some th oughts about where it could be comign from but wasnt able to figure out why. maybe @genadz could also take a quick look since hes also been in there recently?

@genadz
Copy link
Contributor

genadz commented Mar 9, 2021

@keygang id love to merge this but there is something wrong with the performance. its incredibly slow. we need to concentrate on what these changes are doing to make the code slower. maybe start with master and add one change at a time to see which one is doing it. i had some th oughts about where it could be comign from but wasnt able to figure out why. maybe @genadz could also take a quick look since hes also been in there recently?

@keygang @kevinkreiser I found a reason of bad performance for default case (clear_reserved_memory=false). Function Algorithm::Clear() is called on each request. In that function we initialize (vector::resize) labels vector with max_reserved_labels_count elements and then immediately clear it. So, we shouldn't call resize if number of elements less than allowed capacity.

@keygang
Copy link
Contributor Author

keygang commented Mar 11, 2021

@keygang id love to merge this but there is something wrong with the performance. its incredibly slow. we need to concentrate on what these changes are doing to make the code slower. maybe start with master and add one change at a time to see which one is doing it. i had some th oughts about where it could be comign from but wasnt able to figure out why. maybe @genadz could also take a quick look since hes also been in there recently?

@keygang @kevinkreiser I found a reason of bad performance for default case (clear_reserved_memory=false). Function Algorithm::Clear() is called on each request. In that function we initialize (vector::resize) labels vector with max_reserved_labels_count elements and then immediately clear it. So, we shouldn't call resize if number of elements less than allowed capacity.

@genadz
#2789 (comment)
yes, but @kevinkreiser checked without resize when size is small the max_reserved_labels

@genadz
Copy link
Contributor

genadz commented Mar 11, 2021

yes, but @kevinkreiser checked without resize when size is small the max_reserved_labels

i tried fixing this in bidirectional a* and dijkstras but its still slow... where else are we running into this, it clearly has to be this bit of the code that is causign the performane bottleneck. i'll look at it with fresh eyes in a day or so

you should fix this in all algorithms, because you call Clear() here https://github.com/valhalla/valhalla/blob/master/src/thor/worker.cc#L343-L356 per each request

@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch 2 times, most recently from 9f9f38e to 5aff53a Compare March 11, 2021 10:39
@kevinkreiser
Copy link
Member

kevinkreiser commented Mar 11, 2021

I found a reason of bad performance for

@genadz yeah i saw that too and made the change locally but still saw poor performance. I'm not sure what the heck i was doing wrong (probably just too many things at once). If someone has the time that this no longer shows the performance regression i think we can get it reviewed and merged

@genadz
Copy link
Contributor

genadz commented Nov 1, 2021

@keygang @kevinkreiser I tested this branch (with clear_reserved_memory=false) locally against master and found out that this branch is slower by ~2%. interesting why

@kevinkreiser
Copy link
Member

yeah i would expect no change for defaults. maybe its the loki thing

@genadz
Copy link
Contributor

genadz commented Nov 1, 2021

yeah i would expect no change for defaults. maybe its the loki thing

Checking...

@genadz
Copy link
Contributor

genadz commented Nov 1, 2021

yeah i would expect no change for defaults. maybe its the loki thing

Checking...

@keygang @kevinkreiser sorry, I checked timings again and didn't notice any difference. so, probably some other local stuff hit timings when I measured them first time.

@dnesbitt61
Copy link
Member

I have found that performance drops if EdgeLabels needs to be resized during route computation. One can check the reserved size against the # of labels at the end of computation (I always look at the log statement in FormPath).

@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch from d6e77f8 to 3a2b4a5 Compare November 2, 2021 04:31
@keygang
Copy link
Contributor Author

keygang commented Nov 2, 2021

I have found that performance drops if EdgeLabels needs to be resized during route computation. One can check the reserved size against the # of labels at the end of computation (I always look at the log statement in FormPath).

Yes, when the flag is true, we need to resize labels during route computation. But we need to clean up labels in order not to take up extra memory on mobile platforms which we don't use, because these labels take up a lot of space

@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch 6 times, most recently from 732000a to 17786e0 Compare November 2, 2021 05:06
@@ -32,8 +32,10 @@ class PathAlgorithm {
/**
* Constructor
*/
PathAlgorithm()
: interrupt(nullptr), has_ferry_(false), not_thru_pruning_(true), expansion_callback_() {
PathAlgorithm(uint32_t max_reserved_labels_count, bool clear_reserved_memory)
Copy link
Contributor

@genadz genadz Nov 2, 2021

Choose a reason for hiding this comment

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

imho, it's better to pass property tree into the constructor. in that case we would have common parameters parsing

config.get<uint32_t>("max_reserved_labels_count", kInitialEdgeLabelCountBD),
config.get<bool>("clear_reserved_memory", false)

only once here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah good, remark, but how set kInitialEdgeLabelCountBD beautifully in PathAlgorithm? In different algos we set different kInitialEdgeLabelCountBD, for example for Astar we set 1000000 but for multimodal we set 200000
@genadz

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can update config by default value and forward it to PathAlgoritm as a copy, is it good?

Copy link
Contributor

Choose a reason for hiding this comment

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

hm, I don't see a good way how to set different max_reserved_labels_count for different algorithms and keep parsing only in PathAlgorithm. so, probably for now it's okay to have constructor as you proposed

PathAlgorithm(uint32_t max_reserved_labels_count, bool clear_reserved_memory)

@kevinkreiser WDYT ?

Copy link
Member

Choose a reason for hiding this comment

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

@genadz @keygang i agree, its completley fine to keep it that way until we think of something cleaner

@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch from 17786e0 to 2c0f0be Compare November 2, 2021 12:08
genadz
genadz previously approved these changes Nov 2, 2021
genadz
genadz previously approved these changes Nov 2, 2021
@keygang keygang force-pushed the sb-clear-reserved-memory-param-thor branch from 5d50dbb to 8248848 Compare November 3, 2021 11:38
@kevinkreiser kevinkreiser merged commit 1d3133d into valhalla:master Nov 3, 2021
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

Successfully merging this pull request may close these issues.

None yet

4 participants