fd_set implementation is inefficient

Abierto
#283 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Refactorización
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
c, wasm

Línea de trabajo

Comience revisando las cabeceras relacionadas con select referenciadas en el issue: __macro_FD_SETSIZE.h, __typedef_fd_set.h, _fd_set.h y sys/select.h de musl. Compare la representación actual de fd_set con las restricciones de POSIX y determine el alcance requerido y el impacto en la compatibilidad. Done debe incluir una dirección de implementación acordada y la validación correspondiente del comportamiento de select y FD*.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C
Estrellas
1k
Forks
268
Merge medio
1 d 3 h
PR fusionados (30 d)
20

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de WebAssembly/wasi-libc

Todos los issues de WebAssembly/wasi-libc

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.