Skip to content

Conversation

joboet
Copy link
Member

@joboet joboet commented Aug 29, 2025

The dlsym! macro always ensures that the name string is nul-terminated, so there is no need to perform the check at runtime. Also, acquire loads are generally faster than a load and a barrier, so use them. This is only false in the case where the symbol is missing, but that shouldn't matter too much.

@rustbot
Copy link
Collaborator

rustbot commented Aug 29, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 29, 2025
@joboet joboet changed the title std: optimize dlsym! macro and add a test for it std: optimize dlsym! macro and add a test for it Aug 29, 2025
@rust-log-analyzer

This comment has been minimized.

The `dlsym!` macro always ensures that the name string is nul-terminated, so there is no need to perform the check at runtime. Also, acquire loads are generally faster than a load and a barrier, so use them. This is only false in the case where the symbol is missing, but that shouldn't matter too much.
@tgross35
Copy link
Contributor

Just curious...

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 29, 2025
std: optimize `dlsym!` macro and add a test for it
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 29, 2025
Comment on lines 94 to +97
pub(crate) const fn new(name: &'static str) -> Self {
let Ok(name) = CStr::from_bytes_with_nul(name.as_bytes()) else {
panic!("not a nul-terminated string")
};
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be a bit more accurate to take a &'static CStr in the function signature and then move the CStr::from_bytes_with_nul(...).unwrap() to macro dlsym, so this function isn't ever called with invalid inputs (not that it's likely to be used anywhere else).

match self.func.load(Ordering::Acquire) {
func if func.addr() == 1 => self.initialize(),
func if func.is_null() => None,
func => Some(unsafe { mem::transmute_copy::<*mut c_void, F>(&func) }),
Copy link
Contributor

Choose a reason for hiding this comment

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

While you're here, mind adding a safety comment?

use crate::ffi::c_int;

#[test]
fn dlsym() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we have that other branch, maybe also want to add a test for:

    dlsym! {
        #[link_name = "abs"]
        fn definitely_not_abs(i: c_int) -> c_int;
    }

Comment on lines +135 to +139
const {
if size_of::<F>() != size_of::<*mut libc::c_void>() {
panic!("not a function pointer")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional, but this could probably be enforced a bit stronger with the bound F: crate::marker::FnPtr. Even if that bound is added, we should likely avoid using its .addr() method for now until it gets the planned updates (maybe worth a FIXME).

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, it's preexisting but any idea why transmute_copy is used rather than transmute? That would enforce the size constraint.

@rust-bors
Copy link

rust-bors bot commented Aug 30, 2025

☀️ Try build successful (CI)
Build commit: e2bfd7f (e2bfd7fb1f49deb317bdee4685858a4b7e85d848, parent: fe55364329579d361b1ab565728bc033a7dba07e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e2bfd7f): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 3
Improvements ✅
(secondary)
-2.7% [-2.9%, -2.5%] 6
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 3

Max RSS (memory usage)

Results (primary -1.0%, secondary -1.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.0% [2.7%, 3.3%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-9.0% [-9.0%, -9.0%] 1
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) -1.0% [-9.0%, 3.3%] 3

Cycles

Results (primary 1.9%, secondary 2.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.9% [1.9%, 1.9%] 1
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.9% [1.9%, 1.9%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 467.034s -> 466.989s (-0.01%)
Artifact size: 388.52 MiB -> 388.52 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants