Skip to content
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

What's the point of RepeatedField? #84

Closed
edre opened this issue Mar 16, 2015 · 1 comment
Closed

What's the point of RepeatedField? #84

edre opened this issue Mar 16, 2015 · 1 comment

Comments

@edre
Copy link

edre commented Mar 16, 2015

Some repeated types are implemented with Vec, and some are implemented with RepeatedField. RepeatedField seems to be just using a Vec as a growable capacity, but Vec already does this internally. Is there a plan to replace all remaining RepeatedField with Vec at some point?

@stepancheg
Copy link
Owner

The idea behind RepeatedField is to reuse allocated memory for nested structures.

Suppose you have:

message A {
    repeated B b = 1;
}

message B {
    optional string s = 1;
}

If you parse A in loop, like this:

let mut a = A::new();
loop {
    a.clear();
    stream.merge_message(&mut a).unwrap();
    // process a
}

In that case after couple of iterations malloc won't be used, becase .clear() does not release memory back to malloc.

If you are using Vec, memory for B::s will be allocated and released on each iteration.

Google protobuf C++ uses this trick.

So there is no plan to replace RepeatedField with Vec by default, but if there's demand, it is possible to implement protobuf option to generate Vec.

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