-
Notifications
You must be signed in to change notification settings - Fork 443
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
Return ranges instead of vectors #6
Comments
What happens, when somebody doesn't use the When you can show me a way to avoid such situation I will rethink this issue. But anyway, is this additional memory allocation really such a bottleneck? |
The user should not save the |
Also, since you mentioned usability by game engines in #5, I should note that the needless allocations required by returning a |
the next feature I would like to add to RTTR is the possibility to filter the returned properties.
These properties will not lie consecutively in memory. I would need some kind of on demand iterator. |
Lazy forward ranges are a common feature of any range library. :) I might suggest you read through Eric Niebler's rather extensive writings on what is likely going to become a major C++ library feature in the future (possibly even a TS by C++17). Even if it's not in C++ proper, the general concepts and ideas are pretty useful. Various game engines have been using them for years, ever since Alexandrescu and co popularized them in the D programming language. The simple version that's more like current C++ would indeed just be a new iterator type that knows how to find the next element with a special form to represent the
And of course, if someone actually did want that in a vector, it becomes as simple as:
Ranges are neat. Views, like I originally suggested, are just a particular type of range. |
The class "array_range" is used to get a view into the stored properties, instead copying all properties together into a std::vector, which allocates heap memory. Remark: The memory usage will be slightly higher, because all properties including base properties of a type are now stored inside an own vector. The class "flat_map" is used for internal usage; it is a map with contiguous memory for key and values. This branch is to implement the request of issue #6 To Do: Implement the same for methods, constructors, enumerations and types.
@seanmiddleditch |
RTTR functions like
get_properties
currently return astd::vector
object. Creation of this object requires allocating backing memory every single time thatget_properties
is invoked.A superior approach would be to return a "range" type like an
array_view
instead of thevector
in such interfaces. This type would still allow using C++ iteration and range-based-for over the returned elements, but would remove the use of allocation.An
array_view
may be too simple to use for some of the data structures used in RTTR's internals, of course, but aniterator_range
can also be written that operates on a custom iterator type that is able to properly introspect RTTR's data.Using such range types judiciously removes all need to ever allocate temporary containers while querying or enumerating the type database with very little (if any) loss of features currently present in RTTR.
The text was updated successfully, but these errors were encountered: