fd_set implementation is inefficient
维护者通常 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_SETSIZEMaximum 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_SETSIZEset to1024- fd_set type
int __fds[FD_SETSIZE];-- an array of integers (fds)
- FD_* macros
- the index into
__fdsis just "the next used fd" - the value in
__fdsis the fd to check
- the index into
- end result:
- scanning the
__fdsarray takes exactly__nfdschecks (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)
- scanning the
let's look at what musl (and basically every other *NIX library out there) does:
- sys/select.h
FD_SETSIZEset to 1024unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)];-- a bitmask of fds- the index into
fds_bitsis the fd to check - the value is a single bit and whether to check the fd
- end result:
- scanning
fds_bitstakes 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 withselect()(fine)
- scanning
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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
WebAssembly/wasi-libc 的其他 Issue
-
wasip2: enabling O_APPEND with fcntl reuses the old positional output stream and overwrites the file未关闭
难度 3/5 1-2 天 新手友好度 70/100
WebAssembly/wasi-libc#919 ·
维护者通常 1 天内回复
-
wasip2/wasip3: reads never see data appended to a file after reaching EOF可能已有人在做 @libsmol 于 25 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 61/100
WebAssembly/wasi-libc#889 ·
维护者通常 1 天内回复
-
Contradicting `SOL_TCP` and `SOL_UDP` definition for p1可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 3/5 1-2 天 新手友好度 45/100
WebAssembly/wasi-libc#855 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 55/100
WebAssembly/wasi-libc#815 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
WebAssembly/wasi-libc#809 · 2 条评论 ·
维护者通常 1 天内回复
查看 WebAssembly/wasi-libc 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
OpenPrinting/cups#1826 ·
维护者通常 1 天内回复
-
Backport to 3.0 advertises a suffix that does not exist可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
389ds/389-ds-base#8152 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
RetroAchievements/rcheevos#580 · 1 条评论 ·
维护者通常 5 天内回复
-
meta:feature-request
难度 2/5 1-3 小时 新手友好度 66/100
mpv-player/mpv#18855 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复