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

Use std::shared_ptr in case if ENABLE_THREAD_SAFE_TILE_REF_COUNT is ON #3067

Merged
merged 11 commits into from
May 13, 2021

Conversation

SiarheiFedartsou
Copy link
Member

@SiarheiFedartsou SiarheiFedartsou commented May 8, 2021

Issue

During implementation of #2730 we learnt that using of std::shared_ptr(which has such ability) has certain performance impact, that's why we ended up using boost::intrusive_ptr. Then we added ENABLE_THREAD_SAFE_TILE_REF_COUNT to work around issues with non thread-safe reference counter in multithreading environment. Since ENABLE_THREAD_SAFE_TILE_REF_COUNT is supposed to be used in the cases when performance is not top-1 priority and sometimes it is very useful to be able to get weak pointer to tile I propose to use std::shared_ptr as tile pointer in case of ENABLE_THREAD_SAFE_TILE_REF_COUNT(instead of boost::intrusive_ptr with thread-safe counter).

Tasklist

  • Add tests
  • Add #fixes with the issue number that this PR addresses
  • Update the docs with any new request parameters or changes to behavior described
  • Update the changelog

Requirements / Relations

Link any requirements here. Other pull requests this PR is based on?

@SiarheiFedartsou SiarheiFedartsou marked this pull request as ready for review May 8, 2021 19:22
averkhaturau
averkhaturau previously approved these changes May 8, 2021
Copy link
Contributor

@averkhaturau averkhaturau left a comment

Choose a reason for hiding this comment

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

lgtm

@mandeepsandhu
Copy link
Contributor

Is getting a weak pointer from a shared ptr the main motivation for this change?

kevinkreiser
kevinkreiser previously approved these changes May 10, 2021
Copy link
Member

@kevinkreiser kevinkreiser left a comment

Choose a reason for hiding this comment

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

Looks good to me. I wish we could have just used shared_ptr regardless but the perf penalty was too much.

@SiarheiFedartsou
Copy link
Member Author

Is getting a weak pointer from a shared ptr the main motivation for this change?

Yes. Do you have alternative ideas how it can be solved?

@mandeepsandhu
Copy link
Contributor

Is getting a weak pointer from a shared ptr the main motivation for this change?

Yes. Do you have alternative ideas how it can be solved?

No, I think your solution is good! I was wondering if we still need to have intrusive_ptr for the non-thread safe usecase?

Minor optional nit: we could rename ENABLE_THREAD_SAFE_TILE_REF_COUNT to something like ENABLE_THREAD_SAFE_TILE_PTR (since we're not explicitly managing a ref counter). I'm fine leaving it as-is too.

@kevinkreiser
Copy link
Member

@mandeepsandhu the only reason we have it at all is performance issues. iirc it was about a 6% performance reduction

@mandeepsandhu
Copy link
Contributor

@mandeepsandhu the only reason we have it at all is performance issues. iirc it was about a 6% performance reduction

6% reduction compared to using shared_ptr in single or multi-threaded context?

@kevinkreiser
Copy link
Member

24 thread load test using mem mapped tar with no locking needed (every thread has its own graph reader)

@danpat
Copy link
Member

danpat commented May 11, 2021

@kevinkreiser it's usually strongly encouraged to use const references to avoid triggering shared_ptr internal mutexes - is not doing that everywhere possibly the source of the slowdown, and perhaps a less kafkaesque fix might be to fix the usage pattern of graph tile pointers?

@kevinkreiser
Copy link
Member

kevinkreiser commented May 11, 2021

@danpat good question! at the end of the day the various TileCache implementations hold the original data. it is possible that you clear the tile cache while still holding a reference to a graph tile. owning your own shared_ptr removes the possibility that the one you have gets deallocated out from underneath you but this was only a feature we gained recently (when @kkaefer and @averkhaturau did the tilecache and shared_ptr refactors). i guess the real question is who owns the lifetime of the tile, i could see a case for the caller to want to take ownership and that happens by default when returning non const reference but we could force use-cases that need that to opt in by copying the shared_ptr explicitly. the way the code is written today i'm pretty certain we'd be fine switch to returning a reference to the shared_ptr from the tilecache. downstream users of the library though may rely on the ability to transfer the lifetime of the tile in question out of the tile cache and into the callers scope. @mskurydin might have some opinions there too possibly

@SiarheiFedartsou
Copy link
Member Author

Minor optional nit: we could rename ENABLE_THREAD_SAFE_TILE_REF_COUNT to something like ENABLE_THREAD_SAFE_TILE_PTR (since we're not explicitly managing a ref counter). I'm fine leaving it as-is too.

The proposed name is definitely better, but I don't want to break backward compatibility(some Valhalla users use this flag already) and probably I'll leave it as is.

averkhaturau
averkhaturau previously approved these changes May 12, 2021
kevinkreiser
kevinkreiser previously approved these changes May 12, 2021
@SiarheiFedartsou SiarheiFedartsou merged commit 04c2468 into master May 13, 2021
@nilsnolde nilsnolde deleted the sf-shared-ptr branch February 24, 2024 15:01
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

5 participants