简体   繁体   English

新的 C++ 代码应该使用内存资源而不是分配器吗?

[英]Should new C++ code use memory resources instead of allocators?

C++17 will bring us std::pmr::memory_resource which is a clean interface for allocating and deallocating memory. C++17 将为我们带来std::pmr::memory_resource ,它是一个用于分配和释放内存的干净接口。 Unlike the Allocator concept, it does just that and nothing more.Allocator概念不同,它仅能做到这一点,此而已。 There will also be std::pmr::polymorphic_allocator which wraps a memory resource into a classical allocator so it can be used with existing containers.还将有std::pmr::polymorphic_allocator将内存资源包装到经典分配器中,以便它可以与现有容器一起使用。

If I'm about to write a new container (or other memory-hungry) type targeting C++17 and later, should I continue programming against the Allocator concept or rather use the newer and cleaner abstraction directly?如果我要编写一个针对 C++17 及更高版本的新容器(或其他内存饥饿)类型,我应该继续针对分配器概念进行编程还是直接使用更新和更清晰的抽象?

As of now, my thoughts go like this.到目前为止,我的想法是这样的。

Reasons to continue using allocators:继续使用分配器的原因:

  • It is consistent with the standard library and existing code.它与标准库和现有代码一致。 Even the new std::pmr::* container aliases continue to use allocators.即使是新的std::pmr::*容器别名也继续使用分配器。
  • Since a memory resource can be wrapped into a std::pmr::polymorphic_allocator , the allocator interface is more general and satisfies the needs of more clients.由于可以将内存资源包装到std::pmr::polymorphic_allocator ,因此 allocator 接口更加通用,可以满足更多客户端的需求。
  • Memory resources always use run-time polymorphism so they have a minor additional run-time overhead compared to the zero-overhead abstraction that allocators can provide.内存资源始终使用运行时多态性,因此与分配器可以提供的零开销抽象相比,它们具有较小的额外运行时开销。
  • Maybe somebody actually needs the other parts of the allocator interface (such as custom pointer types) which cannot be provided by a pure memory resource.也许有人实际上需要纯内存资源无法提供的分配器接口的其他部分(例如自定义指针类型)。

Reasons to start using memory resources instead of allocators:开始使用内存资源而不是分配器的原因:

  • The allocator interface is clunky and hard to implement.分配器接口笨拙且难以实现。 The std::pmr::memory_resource interface is clean and straight-forward. std::pmr::memory_resource接口简洁明了。
  • Since memory resources are polymorphic, they don't affect the type of the container which means fewer template instantiations (and therefore maybe faster compiles and smaller executables) and enables us to move more code into separate translation units.由于内存资源是多态的,它们不会影响容器的类型,这意味着更少的模板实例(因此可能更快的编译和更小的可执行文件)并使我们能够将更多代码移动到单独的翻译单元中。
  • If an object uses a memory resource, it can always instantiate a sub-object that still uses allocators by wrapping the memory resource into a std::pmr::polymorphic_allocator .如果一个对象使用内存资源,它总是可以通过将内存资源包装到std::pmr::polymorphic_allocator来实例化仍然使用分配器的子对象。 The other way round is more difficult.反过来就更难了。
  • Memory allocation is a relatively work-intensive task anyway.无论如何,内存分配是一项相对工作密集的任务。 A single virtual function call doesn't add much overhead, relatively speaking.相对而言,单个虚函数调用不会增加太多开销。

Do there already exist any recommendations for how to use the new library feature effectively?是否已经存在关于如何有效使用新库功能的任何建议?

At this point no.在这一点上没有。

Allocators in C++ currently are much easier than they used to be. C++ 中的分配器目前比以前容易得多。

They provide both pmr (polymorphic) and classic allocator support.它们提供 pmr(多态)和经典分配器支持。

More importantly, pmr based allocation has not been in heavy use for years.更重要的是,基于 pmr 的分配多年来一直没有被大量使用。 Any weaknesses may still come to light.任何弱点都可能会暴露出来。

Fast pool based allocators, or even fixed buffer ones or sbo (small buffer optimization) extensions, may notice the virtualization overhead.基于快速池的分配器,甚至固定缓冲区分配器或 sbo(小缓冲区优化)扩展,可能会注意到虚拟化开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM