Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1 | /* |
| 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 Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 12 | |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 13 | #include <unordered_set> |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 14 | |
| 15 | namespace SkSL { |
| 16 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 17 | /** |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 18 | * 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 Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 20 | */ |
| 21 | class ModifiersPool { |
| 22 | public: |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 23 | const Modifiers* add(const Modifiers& modifiers) { |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 24 | auto [iter, wasInserted] = fModifiersSet.insert(modifiers); |
| 25 | return &*iter; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 26 | } |
| 27 | |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 28 | void clear() { |
| 29 | fModifiersSet.clear(); |
| 30 | } |
| 31 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 32 | private: |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 33 | std::unordered_set<Modifiers> fModifiersSet; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | } // namespace SkSL |
| 37 | |
| 38 | #endif |