inplace_stop_callback could reduce its size by 2 pointers

Aperta
#1,688 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

Cerca __inplace_stop_callback_base, inplace_stop_source::request_stop() e la deregistrazione dei callback. Traccia come vengono utilizzati i membri della lista e i membri relativi al completamento dell'esecuzione durante l'inserimento, l'esecuzione e la rimozione dei callback. Il lavoro è completato quando la sincronizzazione e il comportamento dei callback vengono preservati riducendo al contempo lo spazio di archiviazione della struttura come descritto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement P2

The __inplace_stop_callback_base structure is defined as follows:

  struct __inplace_stop_callback_base {
      const inplace_stop_source* __source_;
      __execute_fn_t* __execute_;
      __inplace_stop_callback_base* __next_ = nullptr;
      __inplace_stop_callback_base** __prev_ptr_ = nullptr;
      bool* __removed_during_callback_ = nullptr;
      std::atomic<bool> __callback_completed_{false};
  };

However, it's worth noting that there are two distinct phases of use of this data-structure:

  • The callback is inserted in the linked-list of items, waiting to be executed
  • The callback is actually being executed and needs synchronisation regarding the completion of the callback.

The data-members __removed_during_callback_ and __callback_completed_ are only ever used in the second phase and the data-members __next_ and __prev_ptr_ are only ever used in the first phase.

This means that these data-members can be placed into a union so that the storage for the next/prev pointers is reused for the removed/completed data-members during the second phase. Doing so would shrink the size of the __inplace_stop_callback_base structure from 6 pointers (48 bytes on 64-bit machines) to 4 pointers (32 bytes on 64-bit machines).

For example:

struct __inplace_stop_callback_base {
  const inplace_stop_source* __source_;
  __execute_fn_t* __execute_;
  struct __list_members_t {
    __inplace_stop_callback_base* __next_ = nullptr;
    __inplace_stop_callback_base** __prev_ptr_ = nullptr;
  };
  struct __execute_members_t {
    bool* __removed_during_callback_ = nullptr;
    std::atomic<bool> __callback_completed_{false};
  };
  union {
    __list_members_t __list_members_;
    __execute_members_t __execute_members_;
  };
};

The __execute_ data-member can be used as a discriminator to determine which data-members are active. The __execute_ member contains the function pointer that will be invoked and needs to be populated while the callback is sitting in the queue of callbacks to be invoked. However, we could set this data-member to nullptr when the function is being executed - we can take a copy of the function-pointer into a local variable inside inplace_stop_source::request_stop() and then set the __execute_ data-member to nullptr before initializing the __removed_during_callback_ and __callback_completed_ members. The __execute_ data-member can then be used by the callback deregistration to determine whether the callback is still a member of the list or has been or is in the process of being executed.

Lingua principale
C++
Stelle
2.4k
Fork
270
Merge medio
2g 17h
PR unite (30g)
37

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NVIDIA/stdexec

Tutte le issue di NVIDIA/stdexec

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.