Skip to content

Efficient passing of structs with movable fields #10541

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ibukanov opened this issue Nov 18, 2013 · 3 comments
Closed

Efficient passing of structs with movable fields #10541

ibukanov opened this issue Nov 18, 2013 · 3 comments
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@ibukanov
Copy link
Contributor

Consider the following struct that simply wraps an array pointer:

struct Latin1String {
    data: ~[u8]
}

Due to the move semantic one cannot use Latin1String directly as argument type. However, using &Latin1String for arguments currently implies double-indirection when accessing characters even if semantically there would be no difference if internally &Latin1String is implemented as a copy via registers all fields of Latin1String.

It would be nice if Rust implement such optimization for &T arguments effectively turning that into "the most efficient way to pass read-only parameters to a function" .

Similar problem exists when a generic function takes &T argument and is instantiated with int and similar built-in types.

Note that C++ has the same problem when (const T&) leads to inefficient passing of 1-3 word-sized T like int or double forcing to use workarounds like inlined functions in headers, explicit template specialization for primitive types etc.

@thestinger
Copy link
Contributor

LLVM has the argpromotion pass for this and Rust enables it at --opt-level=3. For a dynamically linked library, I think the indirection for the symbol thunk stuff will be significantly more expensive.

@ibukanov
Copy link
Contributor Author

Nice, so &T as argument is already the most efficient way to pass arguments. As for dynamic linking I suppose requiring that &T is always passed as a copy for small-sized T can lead to binary incompatibility as it would prevent changing the size of T while staying compatible with callers. So it is not an option there.

@thestinger
Copy link
Contributor

I don't think this is possible, because the address of references can be used in safe code. A proposal to change the semantics or introduce a new feature would be necessary, not just an implementation change.

flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 6, 2023
docs fix: unknown field `allowed-locales`

changelog: [`DISALLOWED_SCRIPT_IDENTS`]: Replace the nonexistent `allowed-locales` in the docs with `allowed-scripts`.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

No branches or pull requests

2 participants