fd_set implementation is inefficient

Aperta
#283 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
c, wasm

Direzione di ricerca

Iniziare esaminando gli header relativi a select citati nell’issue: __macro_FD_SETSIZE.h, __typedef_fd_set.h, _fd_set.h e sys/select.h di musl. Confrontare la rappresentazione attuale di fd_set con i vincoli POSIX e determinare l’ambito necessario e l’impatto sulla compatibilità. Done deve includere una direzione di implementazione concordata e la relativa validazione del comportamento di select e FD*.

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

Descrizione

cloudlibc seems to have made a design choice that POSIX does not require, and what most *NIX implementations don't support. this has led to cloudlibc using a lot more memory supporting scenarios with select() that is better served by poll(). imo this should be revisited by basically deleting the cloudlibc logic entirely.

first, let's start with what POSIX says:

  • fd_set type

    The <sys/select.h> header shall define the fd_set type as a structure.
    The <sys/select.h> header shall define the following symbolic constant, which shall have a value suitable for use in #if preprocessing directives:
    FD_SETSIZE Maximum number of file descriptors in an fd_set structure.
    The requirement for the fd_set structure to have a member fds_bits has been removed as per The Open Group Base Resolution bwg2001-005.

  • select()

    The behavior of these macros is undefined if the fd argument is less than 0 or greater than or equal to FD_SETSIZE, or if fd is not a valid file descriptor, or if any of the arguments are expressions with side-effects.

let's look at what cloudlibc does:

  • FD_SETSIZE set to 1024
  • fd_set type
    • int __fds[FD_SETSIZE]; -- an array of integers (fds)
  • FD_* macros
    • the index into __fds is just "the next used fd"
    • the value in __fds is the fd to check
  • end result:
    • scanning the __fds array takes exactly __nfds checks (good)
    • sizeof(fd_set) is 4100 bytes (bad)
    • still has a max nfds of FD_SETSIZE (fine)
    • any valid fd can be monitored with select(), not just [0, FD_SETSIZE) (good)

let's look at what musl (and basically every other *NIX library out there) does:

  • sys/select.h
    • FD_SETSIZE set to 1024
    • unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)]; -- a bitmask of fds
    • the index into fds_bits is the fd to check
    • the value is a single bit and whether to check the fd
  • end result:
    • scanning fds_bits takes longer, but most architectures have "bit is set" insns to optimize this. an unoptimized implementation would take at least 128 tests (FD_SETSIZE / 8) (meh)
    • sizeof(fd_set) is 128 bytes (good)
    • still has a max nfds of FD_SETSIZE (fine)
    • only valid fd's within [0, FD_SETSIZE) can be monitored with select() (fine)

so cloudlibc uses 4k of memory instead of 128bytes in order to be slightly faster and to support fds larger than FD_SETSIZE. but no POSIX-compliant project would ever use an fd >= FD_SETSIZE, which means they'd be making assumptions that the underlying code is cloudlibc.

WASI API isn't relevant because it doesn't define fd_set, and this is purely about the select() API which is entirely part of WASI libc.

Lingua principale
C
Stelle
1k
Fork
268
Merge medio
1g 3h
PR unite (30g)
20

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 WebAssembly/wasi-libc

Tutte le issue di WebAssembly/wasi-libc

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.