<functional>: std::function: avoid one indirection for SFO
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.1k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
@MikeGitb mentioned in https://github.com/microsoft/STL/issues/964#issuecomment-652925503
Note that the self-referencing is inherent to the Small Functor Optimization which is critically important for performance.
Are you sure? I thought Sean Parent had shown in his lightning talk "Polymorphic Task Template in Ten" (Meeting C++ 2017) how this works without the extra pointer: https://www.youtube.com/watch?v=2KGkcGtGVM4. I don't remember if there were any problems with his implementation that would prevent the same implementation strategy in std::function.
Will try to explain very briefly:
Currently std::function uses pointer to small implementation in its buffer and large implementation on heap.
It could instead both place small and large implementation in its buffer. In this case, large implementation data has to be on heap, but vptr of large implementation is still in the buffer. But both implementations are on a known location, so no pointer to it is needed.
This would result in avoiding extra indirection (both pointer size and run-time pointer chasing).
This will also make std::function not self-referencing ( #964 will be presumably fixed by compiler, but it might come handy if uses decides to move std::function objects with memcpy ).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the current std::function small-functor and heap-storage approach described in the issue, then read issue #964 and Sean Parent's referenced talk. Done would mean evaluating or implementing the proposed layout so both cases avoid the extra indirection and self-reference without changing std::function's behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100