-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Limit the size of cgu names when using the `-Zhuman-readable-cgu-name… #141558
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use std::borrow::Cow; | ||
use std::fmt; | ||
use std::hash::Hash; | ||
|
||
|
@@ -468,6 +469,29 @@ impl<'tcx> CodegenUnit<'tcx> { | |
hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE) | ||
} | ||
|
||
pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> { | ||
// Set a limit a somewhat below the common platform limits for file names. | ||
const MAX_CGU_NAME_LENGTH: usize = 200; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something we can query from the OS? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be less than the actual filename limit, since the extension and other bits are added later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even that aside, while on Windows we could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Browsing the limits it looks like 255 is by far the most common amongst filesystems https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A cgu name of 200 characters will still cause errors on Windows if the path to the directory it is in is 60 or more characters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the below conversation, verbatim paths can be used to workaround that. IIRC llvm handles long paths now and rustc does by virtue of using std APIs. |
||
const TRUNCATED_NAME_PREFIX: &str = "-trunc-"; | ||
if human_readable_name.len() > MAX_CGU_NAME_LENGTH { | ||
let mangled_name = Self::mangle_name(human_readable_name); | ||
// Determine a safe byte offset to truncate the name to | ||
let truncate_to = human_readable_name.floor_char_boundary( | ||
MAX_CGU_NAME_LENGTH - TRUNCATED_NAME_PREFIX.len() - mangled_name.len(), | ||
); | ||
format!( | ||
"{}{}{}", | ||
&human_readable_name[..truncate_to], | ||
TRUNCATED_NAME_PREFIX, | ||
mangled_name | ||
) | ||
.into() | ||
} else { | ||
// If the name is short enough, we can just return it as is. | ||
human_readable_name.into() | ||
} | ||
} | ||
|
||
pub fn compute_size_estimate(&mut self) { | ||
// The size of a codegen unit as the sum of the sizes of the items | ||
// within it. | ||
|
@@ -604,7 +628,7 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> { | |
let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix); | ||
|
||
if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names { | ||
cgu_name | ||
Symbol::intern(&CodegenUnit::shorten_name(cgu_name.as_str())) | ||
} else { | ||
Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str())) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a Windows dev but wouldn't it make sense to "just" use extended-length paths on Windows (whose length limit is 216/2-1 = 32,767) — prefix is
\\?\
. DocsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extended paths seem like the better solution to me as well since that means the logic to name the file will be the same regardless of platform.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are confusing paths with filenames. The limit here is a filename limit and is a limitation of the file-system, not just the OS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's practical to determine the actual limit, since there are so many factors involved (file-system, OS, etc.) not to mention that when the object files are combined into an archive, the archive file format may have its own limit.
IMO it's much better to impose a simple limit that will work everywhere. Especially given that it only affects users of this niche feature, and only CGUs with abormally long filenames.