Skip to content

string embedding#2237

Closed
youknowone wants to merge 3 commits into
RustPython:mainfrom
youknowone:str-embed
Closed

string embedding#2237
youknowone wants to merge 3 commits into
RustPython:mainfrom
youknowone:str-embed

Conversation

@youknowone
Copy link
Copy Markdown
Member

No description provided.

Comment thread vm/src/obj/objstr.rs Outdated
type InnerIntRepr = u64;

#[derive(Debug)]
struct StrInner(ManuallyDrop<Box<str>>);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could put this in rustpython_common::str, maybe

@coolreader18

This comment has been minimized.

@coolreader18

This comment has been minimized.

@coolreader18

This comment has been minimized.

@youknowone
Copy link
Copy Markdown
Member Author

that sounds like more to string interning for constant string, right? I thought vm can hold a vec of PyStringRef for that.

In this PR, I am trying short string embedding here. It will reuse fat pointer memory allocated for Box.

@coolreader18
Copy link
Copy Markdown
Member

Ah, okay, that makes sense, I guess I misinterpreted what you meant by embedding

@youknowone youknowone marked this pull request as ready for review September 26, 2020 03:16
@youknowone youknowone force-pushed the str-embed branch 2 times, most recently from fda5f2a to 6aac8c7 Compare September 26, 2020 03:29
@coolreader18
Copy link
Copy Markdown
Member

Looking at the source for embed-str -- is there a reason you did (1..=MAX_SIZE).contains(&s.len()) and not s.is_empty()?

@coolreader18
Copy link
Copy Markdown
Member

coolreader18 commented Sep 26, 2020

Also, when you assume_init() for EmbeddedStr, I don't think that's actually all initialized -- you only copy_nonoverlapping() len bytes, after that I think it's uninitialized. You might want to store EmbeddedStr/EmbeddingStr as a MaybeUninit<[usize; 2]/[u8; SIZE]> and read the mode by reading only the exact byte that actually holds the metadata. Have you tried running the tests with cargo miri? That would probably help with ironing out UB

@youknowone
Copy link
Copy Markdown
Member Author

@Kroisse introduced https://docs.rs/string_cache/ for string interning

@youknowone
Copy link
Copy Markdown
Member Author

Looking at the source for embed-str -- is there a reason you did (1..=MAX_SIZE).contains(&s.len()) and not s.is_empty()?

I think that's compact expression for !s.is_empty() && s <= MAX_SIZE

About mode, i'd better to use exact byte from byte array. But does it actually need to be stored as MaybeUninit? we can access only to initailized bytes and it doesn't have any drop behavior.

@youknowone
Copy link
Copy Markdown
Member Author

I think this PR requires benchmark before merging

@coolreader18
Copy link
Copy Markdown
Member

I think that's compact expression for !s.is_empty() && s <= MAX_SIZE

Oh, lol, I completely missed the const MAX_SIZE before that, I thought MAX_SIZE was like usize::MAX or something

But does it actually need to be stored as MaybeUninit? we can access only to initailized bytes and it doesn't have any drop behavior.

Yeah, unfortunately, it's still UB even if you're not accessing them, since you can't trust that the compiler won't optimize it to access some of the uninitialized memory -- there's a good article I read about it but I can't find it right now.

@frank-king
Copy link
Copy Markdown
Contributor

It seems that the embed_str crate does not check the invalid unicode points?

@youknowone youknowone closed this Jan 4, 2026
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.

3 participants