fd_set implementation is inefficient

未关闭
#283 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
停滞
技术栈
c, wasm

调研方向

首先检查 issue 中引用的 select 相关头文件:__macro_FD_SETSIZE.h、__typedef_fd_set.h、_fd_set.h,以及 musl 的 sys/select.h。将当前的 fd_set 表示与 POSIX 约束进行比较,并确定所需的范围和兼容性影响。Done 应包括已达成一致的实现方向,以及对 select 和 FD* 行为进行相应的验证。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C
星标
1k
派生
268
平均合并
1 天 3 小时
30 天内合并 PR
20

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

WebAssembly/wasi-libc 的其他 Issue

查看 WebAssembly/wasi-libc 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。