-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeasurementCache.h
More file actions
211 lines (177 loc) · 6.24 KB
/
MeasurementCache.h
File metadata and controls
211 lines (177 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once
#include <CoreGraphics/CGBase.h>
#include <React/RCTUtils.h>
#include <list>
#include <mutex>
#include <react/renderer/graphics/Float.h>
#include <string>
#include <tuple>
#include <unordered_map>
namespace facebook::react {
struct HashUtils {
static inline size_t combine(size_t h, size_t v)
{
return h ^ (v + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2));
}
template <typename T> static inline void hash_one(size_t &seed, const T &v)
{
seed = combine(seed, std::hash<T>{}(v));
}
};
enum class MarkdownFlavor : uint8_t {
CommonMark = 0,
GitHub = 1,
};
struct MeasurementCacheKey {
std::string markdown;
CGFloat maxWidth;
bool allowTrailingMargin;
bool allowFontScaling;
double maxFontSizeMultiplier;
bool md4cFlagsUnderline;
bool md4cFlagsLatexMath;
size_t styleFingerprint;
CGFloat fontScale;
MarkdownFlavor flavor;
bool operator==(const MeasurementCacheKey &other) const
{
return std::tie(markdown, maxWidth, allowTrailingMargin, allowFontScaling, maxFontSizeMultiplier,
md4cFlagsUnderline, md4cFlagsLatexMath, styleFingerprint, fontScale, flavor) ==
std::tie(other.markdown, other.maxWidth, other.allowTrailingMargin, other.allowFontScaling,
other.maxFontSizeMultiplier, other.md4cFlagsUnderline, other.md4cFlagsLatexMath,
other.styleFingerprint, other.fontScale, other.flavor);
}
};
struct MeasurementCacheKeyHash {
size_t operator()(const MeasurementCacheKey &key) const
{
size_t h = 0;
HashUtils::hash_one(h, key.markdown);
HashUtils::hash_one(h, key.maxWidth);
HashUtils::hash_one(h, key.allowTrailingMargin);
HashUtils::hash_one(h, key.allowFontScaling);
HashUtils::hash_one(h, key.maxFontSizeMultiplier);
HashUtils::hash_one(h, key.md4cFlagsUnderline);
HashUtils::hash_one(h, key.md4cFlagsLatexMath);
HashUtils::hash_one(h, key.styleFingerprint);
HashUtils::hash_one(h, key.fontScale);
HashUtils::hash_one(h, static_cast<uint8_t>(key.flavor));
return h;
}
};
struct CachedSize {
CGFloat width;
CGFloat height;
};
template <typename StyleStruct> inline size_t computeStyleFingerprint(const StyleStruct &s)
{
size_t h = 0;
auto hashFields = [&](auto... args) { (HashUtils::hash_one(h, args), ...); };
auto hashTextLayout = [&](const auto &item) {
hashFields(item.fontFamily, item.fontSize, item.fontWeight, item.marginTop, item.marginBottom, item.lineHeight);
};
// Block Elements
hashTextLayout(s.paragraph);
hashFields(s.paragraph.textAlign);
hashTextLayout(s.h1);
hashFields(s.h1.textAlign);
hashTextLayout(s.h2);
hashFields(s.h2.textAlign);
hashTextLayout(s.h3);
hashFields(s.h3.textAlign);
hashTextLayout(s.h4);
hashFields(s.h4.textAlign);
hashTextLayout(s.h5);
hashFields(s.h5.textAlign);
hashTextLayout(s.h6);
hashFields(s.h6.textAlign);
hashTextLayout(s.blockquote);
hashFields(s.blockquote.borderWidth, s.blockquote.gapWidth);
hashTextLayout(s.list);
hashFields(s.list.bulletSize, s.list.markerWidth, s.list.markerFontWeight, s.list.gapWidth, s.list.marginLeft);
// Code & Inlines
hashFields(s.codeBlock.fontFamily, s.codeBlock.fontSize, s.codeBlock.fontWeight, s.codeBlock.marginTop,
s.codeBlock.marginBottom, s.codeBlock.lineHeight, s.codeBlock.padding, s.codeBlock.borderRadius,
s.codeBlock.borderWidth);
hashFields(s.code.fontFamily, s.code.fontSize);
hashFields(s.link.fontFamily, s.strong.fontFamily, s.strong.fontWeight, s.em.fontFamily, s.em.fontStyle);
// Visual/Spacing Elements
hashFields(s.image.height, s.image.marginTop, s.image.marginBottom);
hashFields(s.inlineImage.size);
hashFields(s.thematicBreak.height, s.thematicBreak.marginTop, s.thematicBreak.marginBottom);
// Complex Components
hashTextLayout(s.table);
hashFields(s.table.headerFontFamily, s.table.cellPaddingHorizontal, s.table.cellPaddingVertical, s.table.borderWidth,
s.table.borderRadius);
hashFields(s.math.fontSize, s.math.padding, s.math.marginTop, s.math.marginBottom, s.math.textAlign);
hashFields(s.taskList.checkboxSize, s.taskList.checkboxBorderRadius);
return h;
}
template <typename PropsType>
inline MeasurementCacheKey buildMeasurementCacheKey(const PropsType &props, CGFloat maxWidth, CGFloat fontScale,
MarkdownFlavor flavor)
{
return MeasurementCacheKey{
.markdown = props.markdown,
.maxWidth = maxWidth,
.allowTrailingMargin = props.allowTrailingMargin,
.allowFontScaling = props.allowFontScaling,
.maxFontSizeMultiplier = props.maxFontSizeMultiplier,
.md4cFlagsUnderline = props.md4cFlags.underline,
.md4cFlagsLatexMath = props.md4cFlags.latexMath,
.styleFingerprint = computeStyleFingerprint(props.markdownStyle),
.fontScale = fontScale,
.flavor = flavor,
};
}
/**
* Thread-safe global measurement cache using an LRU (Least Recently Used) strategy.
* This ensures O(1) lookups while automatically discarding the oldest entries.
*/
class MeasurementCache {
public:
struct CacheEntry {
MeasurementCacheKey key;
CachedSize size;
};
static MeasurementCache &shared()
{
static MeasurementCache instance;
return instance;
}
bool get(const MeasurementCacheKey &key, CachedSize &outSize)
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = map_.find(key);
if (it == map_.end()) {
return false;
}
list_.splice(list_.begin(), list_, it->second);
outSize = it->second->size;
return true;
}
void set(const MeasurementCacheKey &key, CachedSize size)
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = map_.find(key);
if (it != map_.end()) {
it->second->size = size;
list_.splice(list_.begin(), list_, it->second);
return;
}
list_.push_front({key, size});
map_[key] = list_.begin();
if (map_.size() > kMaxEntries) {
auto &lastEntry = list_.back();
map_.erase(lastEntry.key);
list_.pop_back();
}
}
private:
MeasurementCache() = default;
static constexpr size_t kMaxEntries = 512;
mutable std::mutex mutex_;
std::list<CacheEntry> list_;
std::unordered_map<MeasurementCacheKey, std::list<CacheEntry>::iterator, MeasurementCacheKeyHash> map_;
};
} // namespace facebook::react