Skip to content

Commit 0d1d945

Browse files
committed
2D: Fix various issues and minor performance optimisations
1 parent 748f407 commit 0d1d945

File tree

8 files changed

+380
-149
lines changed

8 files changed

+380
-149
lines changed

core/templates/lru.h

+46-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,21 @@
3535
#include "hash_map.h"
3636
#include "list.h"
3737

38-
template <typename TKey, typename TData, typename Hasher = HashMapHasherDefault, typename Comparator = HashMapComparatorDefault<TKey>>
38+
#if defined(__GNUC__) && !defined(__clang__)
39+
#define ADDRESS_DIAGNOSTIC_WARNING_DISABLE \
40+
_Pragma("GCC diagnostic push"); \
41+
_Pragma("GCC diagnostic ignored \"-Waddress\"");
42+
43+
#define ADDRESS_DIAGNOSTIC_POP \
44+
_Pragma("GCC diagnostic pop");
45+
#else
46+
#define ADDRESS_DIAGNOSTIC_WARNING_DISABLE
47+
#define ADDRESS_DIAGNOSTIC_POP
48+
#endif
49+
50+
template <typename TKey, typename TData, typename Hasher = HashMapHasherDefault, typename Comparator = HashMapComparatorDefault<TKey>, void (*BeforeEvict)(TKey &, TData &) = nullptr>
3951
class LRUCache {
40-
private:
52+
public:
4153
struct Pair {
4254
TKey key;
4355
TData data;
@@ -51,28 +63,39 @@ class LRUCache {
5163

5264
typedef typename List<Pair>::Element *Element;
5365

66+
private:
5467
List<Pair> _list;
5568
HashMap<TKey, Element, Hasher, Comparator> _map;
5669
size_t capacity;
5770

5871
public:
59-
const TData *insert(const TKey &p_key, const TData &p_value) {
72+
const Pair *insert(const TKey &p_key, const TData &p_value) {
6073
Element *e = _map.getptr(p_key);
6174
Element n = _list.push_front(Pair(p_key, p_value));
6275

6376
if (e) {
77+
ADDRESS_DIAGNOSTIC_WARNING_DISABLE;
78+
if constexpr (BeforeEvict != nullptr) {
79+
BeforeEvict((*e)->get().key, (*e)->get().data);
80+
}
81+
ADDRESS_DIAGNOSTIC_POP;
6482
_list.erase(*e);
6583
_map.erase(p_key);
6684
}
6785
_map[p_key] = _list.front();
6886

6987
while (_map.size() > capacity) {
7088
Element d = _list.back();
89+
ADDRESS_DIAGNOSTIC_WARNING_DISABLE
90+
if constexpr (BeforeEvict != nullptr) {
91+
BeforeEvict(d->get().key, d->get().data);
92+
}
93+
ADDRESS_DIAGNOSTIC_POP
7194
_map.erase(d->get().key);
7295
_list.pop_back();
7396
}
7497

75-
return &n->get().data;
98+
return &n->get();
7699
}
77100

78101
void clear() {
@@ -84,6 +107,17 @@ class LRUCache {
84107
return _map.getptr(p_key);
85108
}
86109

110+
bool erase(const TKey &p_key) {
111+
Element *e = _map.getptr(p_key);
112+
if (!e) {
113+
return false;
114+
}
115+
_list.move_to_front(*e);
116+
_map.erase(p_key);
117+
_list.pop_front();
118+
return true;
119+
}
120+
87121
const TData &get(const TKey &p_key) {
88122
Element *e = _map.getptr(p_key);
89123
CRASH_COND(!e);
@@ -109,6 +143,11 @@ class LRUCache {
109143
capacity = p_capacity;
110144
while (_map.size() > capacity) {
111145
Element d = _list.back();
146+
ADDRESS_DIAGNOSTIC_WARNING_DISABLE;
147+
if constexpr (BeforeEvict != nullptr) {
148+
BeforeEvict(d->get().key, d->get().data);
149+
}
150+
ADDRESS_DIAGNOSTIC_POP;
112151
_map.erase(d->get().key);
113152
_list.pop_back();
114153
}
@@ -124,4 +163,7 @@ class LRUCache {
124163
}
125164
};
126165

166+
#undef ADDRESS_DIAGNOSTIC_WARNING_DISABLE
167+
#undef ADDRESS_DIAGNOSTIC_POP
168+
127169
#endif // LRU_H

doc/classes/ProjectSettings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,10 @@
23672367
<member name="rendering/2d/batching/item_buffer_size" type="int" setter="" getter="" default="16384">
23682368
Maximum number of canvas item commands that can be batched into a single draw call.
23692369
</member>
2370+
<member name="rendering/2d/batching/uniform_set_cache_size" type="int" setter="" getter="" default="256">
2371+
Maximum number of uniform sets that will be cached by the 2D renderer when batching draw calls.
2372+
[b]Note:[/b] A project that uses a large number of unique sprite textures per frame may benefit from increasing this value.
2373+
</member>
23702374
<member name="rendering/2d/sdf/oversize" type="int" setter="" getter="" default="1">
23712375
Controls how much of the original viewport size should be covered by the 2D signed distance field. This SDF can be sampled in [CanvasItem] shaders and is used for [GPUParticles2D] collision. Higher values allow portions of occluders located outside the viewport to still be taken into account in the generated signed distance field, at the cost of performance. If you notice particles falling through [LightOccluder2D]s as the occluders leave the viewport, increase this setting.
23722376
The percentage specified is added on each axis and on both sides. For example, with the default setting of 120%, the signed distance field will cover 20% of the viewport's size outside the viewport on each side (top, right, bottom, left).

0 commit comments

Comments
 (0)