Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rustix instead of libc #10

Closed
wants to merge 2 commits into from
Closed

Use rustix instead of libc #10

wants to merge 2 commits into from

Conversation

notgull
Copy link

@notgull notgull commented Apr 29, 2023

Rather than using libc::gethostname, this PR modifies this crate to use the rustix crate. On Linux, rustix uses raw system calls instead of going through libc, which can lead to lower instruction counts. This also removes unsafe code from this crate to boot.

gethostname() is implemented in both glibc and musl by calling uname() and getting the nodename, so that's how I implemented it here.

@swsnr
Copy link
Owner

swsnr commented Apr 29, 2023

Did this affect overall build time? I'm not so familiar with rustic, but I do know that its alternative nix is rather heavy which is why I went for libc initially.

src/lib.rs Show resolved Hide resolved
@swsnr
Copy link
Owner

swsnr commented Apr 29, 2023

And what about the BSDs and macOS? Is your implementation also correct on these systems?

@notgull
Copy link
Author

notgull commented Apr 29, 2023

Did this affect overall build time? I'm not so familiar with rustic, but I do know that its alternative nix is rather heavy which is why I went for libc initially.

On my machine using the latest stable cargo clean && cargo build --release reports 0.64 s on master and 3.00 s on this branch. So there is a degradation here, but not by a significant amount, and if rustix appears in the build tree it will be amortized across the build.

And what about the BSDs and macOS? Is your implementation also correct on these systems?

I added references that describe the implementation on BSD as well.

@swsnr
Copy link
Owner

swsnr commented Apr 29, 2023

What's the difference here on GitHub? Can you check the latest build for main versus this merge request?

@swsnr
Copy link
Owner

swsnr commented Apr 29, 2023

Oh and would you mind to sign your commits? 🙂

@notgull
Copy link
Author

notgull commented Apr 29, 2023

What's the difference here on GitHub? Can you check the latest build for main versus this merge request?

My bad, I meant main. It was the difference between here and Github.

Oh and would you mind to sign your commits? slightly_smiling_face

Sure! Just did that

@swsnr
Copy link
Owner

swsnr commented Apr 30, 2023

Can you rebase on main? I just realized that the pipeline didn't trigger on pull requests.

I'm still not sure whether I'd like to merge this, and I'd like to see it build on Github actions first. 0.6s to 3s is five times slower, that's quite a bit.

I really like that the code's much shorter, easier to understand and doesn't have an unsafe. But I'm not sure it's worth the price of a much slower build, less so since a libc dependency is definitely amortized across a build since it's almost guaranteed to appear somewhere else in the build tree, because it's such an essential crate.

@swsnr
Copy link
Owner

swsnr commented May 11, 2023

I think I'll pass this one. It did have a noticable impact on the build time:

$ gh run view
? Select a workflow run? Select a workflow run ✓ Move fmt check into test job, CI (main) 272h49m45s ago
? View a specific job in this run? View all jobs in this run

✓ main CI · 4843015285
Triggered via push about 11 days ago

JOBS
✓ cargo-deny in 1m34s (ID 13127555339)
✓ test (ubuntu-latest, 1.64.0) in 1m8s (ID 13127555361)
✓ test (ubuntu-latest, stable) in 13s (ID 13127555387)
✓ test (windows-latest, 1.64.0) in 1m49s (ID 13127555412)
✓ test (windows-latest, stable) in 1m14s (ID 13127555434)
✓ test (macOS-latest, 1.64.0) in 1m26s (ID 13127555462)
✓ test (macOS-latest, stable) in 29s (ID 13127555492)

For more information about a job, try: gh run view --job=<job-id>
View this run on GitHub: https://github.com/swsnr/gethostname.rs/actions/runs/4843015285
$ gh run view
? Select a workflow run ✓ Use rustix instead of libc, CI (rustix) 32h9m24s ago
? View a specific job in this run? View all jobs in this run

✓ rustix CI #10 · 4934400486
Triggered via pull_request about 1 day ago

JOBS
✓ cargo-deny in 2m21s (ID 13366453570)
✓ test (ubuntu-latest, 1.64.0) in 1m25s (ID 13366453768)
✓ test (ubuntu-latest, stable) in 35s (ID 13366453889)
✓ test (windows-latest, 1.64.0) in 2m23s (ID 13366454007)
✓ test (windows-latest, stable) in 2m13s (ID 13366454150)
✓ test (macOS-latest, 1.64.0) in 2m4s (ID 13366454278)
✓ test (macOS-latest, stable) in 35s (ID 13366454418)

For more information about a job, try: gh run view --job=<job-id>
View this run on GitHub: https://github.com/swsnr/gethostname.rs/actions/runs/4934400486

Not sure if it's a fluke, but I don't think that saving a few instructions and removing one line of unsafe justifies bringing in a rather heavy dependency such as rustix.

@swsnr swsnr closed this May 11, 2023
@notgull notgull deleted the rustix branch May 11, 2023 15:57
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.

None yet

2 participants