microsoft / microsoft/STL

<functional>: std::function: avoid one indirection for SFO

Open
#969 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

performance vNext
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.