Skip to content

coverage: Don't convert filename/symbol strings to CString for FFI #114005

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

Merged
merged 2 commits into from
Aug 11, 2023

Conversation

Zalathar
Copy link
Contributor

LLVM APIs are usually perfectly happy to accept pointer/length strings, as long as we supply a suitable length value when creating a StringRef or std::string.

This lets us avoid quite a few intermediate CString copies during coverage codegen. It also lets us use an IndexSet<Symbol> (instead of an IndexSet<CString>) when building the deduplicated filename table.

@rustbot
Copy link
Collaborator

rustbot commented Jul 24, 2023

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 24, 2023
@Zalathar
Copy link
Contributor Author

@rustbot label +A-code-coverage

@rustbot rustbot added the A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) label Jul 24, 2023
@Zalathar
Copy link
Contributor Author

My original goal was just to store symbols in the filenames set, and only convert to CString just before passing the strings through FFI.

But after digging deeper, I found that the C++ code didn't really need nul-terminated strings at all.

Comment on lines +358 to +359
pointers.as_ptr(),
pointers.len(),
lengths.as_ptr(),
lengths.len(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a separate length parameter for the string lengths, so that on the Rust side it's clear that we aren't accidentally omitting the length part of a pointer/length argument pair.

The C++ code then verifies that the two lengths are the same, as they always should be.

Comment on lines -174 to -176
let c_filename = CString::new(file_name.to_string())
.expect("null error converting filename to C string");
debug!(" file_id: {} = '{:?}'", current_file_id, c_filename);
let (filenames_index, _) = self.filenames.insert_full(c_filename);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Notice that the old code here needs to convert every filename (Symbol) into a CString to insert it into the set of filenames, even though there will usually already be a duplicate entry (created by another function in the same source file).

Symbols are already naturally good for checking whether two strings are identical, so it makes sense to use symbols as set entries, and then only resolve them to strings later when we actually need to pass those strings through FFI.

@Zalathar Zalathar force-pushed the no-cstr branch 5 times, most recently from 318d478 to a4e08cf Compare July 30, 2023 23:28
@Zalathar Zalathar force-pushed the no-cstr branch 3 times, most recently from c77ce64 to df18f3d Compare August 3, 2023 01:40
@jackh726
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 10, 2023

📌 Commit 4b154bc has been approved by jackh726

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 10, 2023
@bors
Copy link
Collaborator

bors commented Aug 10, 2023

⌛ Testing commit 4b154bc with merge a9b2c6a...

@bors
Copy link
Collaborator

bors commented Aug 11, 2023

☀️ Test successful - checks-actions
Approved by: jackh726
Pushing a9b2c6a to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 11, 2023
@bors bors merged commit a9b2c6a into rust-lang:master Aug 11, 2023
@rustbot rustbot added this to the 1.73.0 milestone Aug 11, 2023
@Zalathar Zalathar deleted the no-cstr branch August 11, 2023 01:50
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a9b2c6a): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

Max RSS (memory usage)

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

Cycles

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

Binary size

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

Bootstrap: 632.053s -> 631.684s (-0.06%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants