Skip to content

Doc idea: Describe which Rust constructs use pointer indirections and which memcpy #576

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

Closed
kornelski opened this issue May 20, 2015 · 4 comments

Comments

@kornelski
Copy link
Contributor

I've realized I don't have intuition about how Rust's constructs like Slices and Vecs are laid out in memory, and thus how much of them are inline in structs, how much can be on stack, how much gets copied around.

I've seen mentions how many fields slices and vectors have, but that's not quite enough to visualize how, e.g. Option<Box<Vec<T>>> looks like.

Slices are passed to functions as &[T], but does &[&[T]] make sense? Should it be [[T]]?

I'd love to see some diagrams that show how many pointer indirections are involved in usage of complex types.

@steveklabnik
Copy link
Member

We specifically have not and do not guarantee layout yet, so that's part of this. We only guarantee structs with repr(c)

@kornelski
Copy link
Contributor Author

I understand you don't guarantee specific layout, but what about indirection? I think you define that indirectly anyway via Sized and what's allowed in structs, what can be in recursive structs, etc.

@kornelski
Copy link
Contributor Author

e.g. I wonder if [[T]] is same as C's *T[].

It compiles as a function argument, but when I try to use it the compiler says "cannot index a value of type [[T]]".

But if I do &[&[T]] won't that create pointer-to-a-pointer somewhere, which is an unnecessary level of indirection?

Given how incredibly expensive cache misses are on modern CPUs, it's very important for me to fully understand which types and expressions have hidden pointers.

I wouldn't want to pass around pointers to something that's essentially a length+pointer by itself — in such case I'd rather copy the two words than have another pointer indirection, but I'm not quite sure which types are like that.

@kornelski kornelski changed the title Doc idea: memory layout of Rust constructs Doc idea: Describe which Rust constructs use pointer indirections and which memcpy May 21, 2015
@kornelski
Copy link
Contributor Author

This is what I was after: http://cglab.ca/~abeinges/blah/turpl/_book/data.html

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

No branches or pull requests

2 participants