From 9dfc2d924d440560adb2db13c758b2c5b3b7dd47 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 31 May 2017 13:53:48 -0700 Subject: [core] Collection-level immutability Introduce a second level of immutability, over each of the collections held by a style: sources, images, and layers. Tracking immutability at this level allows us to short-circuit significant portions of the RenderStyle update logic via a simple equality check, greatly improving performance. --- include/mbgl/style/image.hpp | 2 +- include/mbgl/util/immutable.hpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/mbgl/style/image.hpp b/include/mbgl/style/image.hpp index 55760ca503..ff3bfedf46 100644 --- a/include/mbgl/style/image.hpp +++ b/include/mbgl/style/image.hpp @@ -24,7 +24,7 @@ public: bool isSdf() const; class Impl; - Immutable impl; + Immutable baseImpl; }; } // namespace style diff --git a/include/mbgl/util/immutable.hpp b/include/mbgl/util/immutable.hpp index 2cdce8772d..eb26c0d282 100644 --- a/include/mbgl/util/immutable.hpp +++ b/include/mbgl/util/immutable.hpp @@ -115,4 +115,19 @@ Immutable staticImmutableCast(const Immutable& u) { return Immutable(std::static_pointer_cast(u.ptr)); } +/** + * Constrained mutation of an immutable reference. Makes a temporarily-mutable copy of the + * input Immutable using the inner type's copy constructor, runs the given callable on the + * mutable copy, and then freezes the copy and reassigns it to the input reference. + * + * Note that other Immutables referring to the same inner instance are not affected; they + * continue to referencing the original immutable instance. + */ +template +void mutate(Immutable& immutable, Fn&& fn) { + Mutable mut = makeMutable(*immutable); + std::forward(fn)(*mut); + immutable = std::move(mut); +} + } // namespace mbgl -- cgit v1.2.1