Stream: general

Topic: cap_std fs_utf8 efficiency


view this post on Zulip Charlie Barto (Nov 28 2022 at 11:11):

I'm looking at cap_std::fs_utf8 and seeing that it's calling from_utf8 on the supplied paths quite frequently, which is Ok(path.as_ref().as_std_path().to_path_buf()), This is dispite it only really needing a Path, not a PathBuf. Is there any particular reason for this or is fs_utf8 just not widely used?

It looks like maybe a real conversion might be required for arf_strings, but even they shouldn't have any additional data for valid UTF-8

view this post on Zulip Dan Gohman (Nov 28 2022 at 20:34):

Good find! There isn't a particular reason for it; the original code used arf_strings, and we didn't notice this when arf_strings were made optional.

view this post on Zulip Charlie Barto (Nov 29 2022 at 02:46):

oddly this issue was introduced after that

view this post on Zulip Charlie Barto (Nov 29 2022 at 02:48):

oh, no, that's not true; it was introduced in the same commit

view this post on Zulip Charlie Barto (Nov 29 2022 at 02:52):

actually, looking at the subsequent code I don't really see why they would need this allocation _either_, since afaict an arf_string of valid utf-8 that said valid utf-8

view this post on Zulip Dan Gohman (Nov 29 2022 at 17:03):

An arf_string of an ill-formed string needs an allocation.

view this post on Zulip Dan Gohman (Nov 29 2022 at 17:03):

I believe from_utf8 has returned a PathBuf since it was first introduced.

view this post on Zulip Dan Gohman (Dec 15 2022 at 22:41):

I've now submitted https://github.com/bytecodealliance/cap-std/pull/285 which fixes this, for the non-arf-strings case.

For users not using arf-strings, there's no need to allocate a Utf8PathBuf for filesystem operations that take a path. Thanks for Charlie Barto on zulip for noticing this!

Last updated: Nov 22 2024 at 16:03 UTC