Skip to content

refactor(effects): inline one-use C result adapters - #981

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
tachytelicdetonation:contrib/inline-effect-adapters
Sep 25, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
tachytelicdetonation:contrib/inline-effect-adapters

Conversation

@tachytelicdetonation

Copy link
Copy Markdown
Contributor

IO.get_env and UDP.bind each had a C helper with one caller, an output pointer, and a second error check in the effect handler. Fold the helper into that handler so it returns the Bend result directly.

Net −27 lines / −136 ttok, across two effect files. The environment lookup still rejects embedded NULs and reports ENOENT for misses. UDP still validates the port, closes a failed socket, and saves errno before closing. No comments or features removed.

Verification on macOS arm64:

  • Eleven existing IO tests passed through default execution, emitted JS (Bun), and native C: 33/33, matching the baseline. Includes environment ownership/misses, NULs, failed IO.try, UDP loopback, parking, polling, truncation, and invalid addresses/ports.
  • Held UDP port 19571 open externally; the existing native loopback test recovered by trying the next port before and after the change.
  • All 16 runtime benchmarks emitted byte-identical C and JS against main (a4952426).
  • Repository gate: 46/46; git diff --check passed.

The private cluster test/performance gates and Linux execution were not run.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you, both inlines read right: we walked every path in get_env.c and udp_bind.c against main and ran tests/io in the C and JS lanes with no change. One more of the same shape is left: bend2/effs/tcp_listen.c has the identical one-use adapter (tcp_listen(port, &out) plus a run function that re-checks q != 0). If you fold that one in too, the PR is complete by its own title and we will merge it.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

TCP.listen had the same one-use C helper as get_env and udp_bind: an
output pointer plus a second error check in its run function. Fold it
into tcp_listen_run so it returns the Bend result directly. The socket
still closes on every failure and errno is saved before the close.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@tachytelicdetonation

Copy link
Copy Markdown
Contributor Author

@nicolas-abril
Folded tcp_listen.c the same way in 2ce7c00 (−10 lines, −47 ttok; PR total now −37 lines / −183 ttok). The socket still closes on every failure path and errno is saved before the close. Checked on macOS arm64: all 7 TCP.listen tests in tests/io pass through native C, the whole tests/io C lane gives the same results before and after, a second listen on a busy port still reports "Address already in use", and the repo gate is 46/46.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks @tachytelicdetonation! The tcp_listen fold reads right and matches the other two, every failure path still closes the socket, and the TCP, UDP and get_env tests pass in the C lane on top of current main. Merging 🙏

@nicolas-abril
nicolas-abril merged commit a9142b1 into bendlang:main Sep 25, 2026
@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
nicolas-abril pushed a commit to tachytelicdetonation/bend that referenced this pull request Sep 28, 2026
Nine C effects spelled "Fail on w->code, else Done(v)" by hand. io_res in
the runtime's Io section says it once; being a macro, v is still only
built on success. Effects on a handle still spell out the tuple that hands
it back. print.c and write.c lose their one-use adapters, as get_env.c and
udp_bind.c do in bendlang#981.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants