blob: e9b863c8711c9598d76a0439df75b13eca08b6f6 [file] [log] [blame]
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001/*
2 * Copyright 2020 Google LLC.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_MODIFIERSPOOL
9#define SKSL_MODIFIERSPOOL
10
Ethan Nicholasdaed2592021-03-04 14:30:25 -050011#include "include/private/SkSLModifiers.h"
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050012
John Stiles586df952020-11-12 18:27:13 -050013#include <unordered_set>
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040014
15namespace SkSL {
16
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040017/**
John Stiles586df952020-11-12 18:27:13 -050018 * Deduplicates Modifiers objects and stores them in a shared pool. Modifiers are fairly heavy, and
19 * tend to be reused a lot, so deduplication can be a significant win.
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040020 */
21class ModifiersPool {
22public:
John Stilesf2872e62021-05-04 11:38:43 -040023 const Modifiers* add(const Modifiers& modifiers) {
John Stiles586df952020-11-12 18:27:13 -050024 auto [iter, wasInserted] = fModifiersSet.insert(modifiers);
25 return &*iter;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040026 }
27
John Stiles10d39d92021-05-04 16:13:14 -040028 void clear() {
29 fModifiersSet.clear();
30 }
31
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040032private:
John Stiles586df952020-11-12 18:27:13 -050033 std::unordered_set<Modifiers> fModifiersSet;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040034};
35
36} // namespace SkSL
37
38#endif