Skip to content

Commit 20bbae8

Browse files
committed
basic style cache
1 parent 74ba533 commit 20bbae8

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

haxe/ui/core/Component.hx

+24
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,30 @@ class Component extends ComponentImpl
12431243
// Style related
12441244
//***********************************************************************************************************
12451245

1246+
private var _styleCacheKey:String = null;
1247+
private var styleCacheKey(get, never):String;
1248+
private function get_styleCacheKey():String {
1249+
if (_styleCacheKey == null) {
1250+
var sb = new StringBuf();
1251+
var ref = this;
1252+
do {
1253+
sb.add(ref.className);
1254+
sb.add("_id:");
1255+
sb.add(ref.id);
1256+
sb.add("_classes:");
1257+
for (c in ref.classes) {
1258+
sb.add(c);
1259+
sb.add("|");
1260+
}
1261+
ref = ref.parentComponent;
1262+
} while (ref != null);
1263+
1264+
//_styleCacheKey = sb.toString();
1265+
return sb.toString();
1266+
}
1267+
return _styleCacheKey;
1268+
}
1269+
12461270
@:noCompletion private var _customStyle:Style = null;
12471271

12481272
/**

haxe/ui/styles/StyleSheet.hx

+32-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package haxe.ui.styles;
33
import haxe.crypto.Sha1;
44
import haxe.ui.core.Component;
55
import haxe.ui.styles.elements.AnimationKeyFrames;
6+
import haxe.ui.styles.elements.Directive;
67
import haxe.ui.styles.elements.ImportElement;
78
import haxe.ui.styles.elements.MediaQuery;
89
import haxe.ui.styles.elements.RuleElement;
@@ -142,18 +143,45 @@ class StyleSheet {
142143
}
143144
}
144145

146+
private var _componentStyleCache = new Map<String, Array<Map<String, Directive>>>();
147+
145148
public function buildStyleFor(c:Component, style:Style = null):Style {
146149
if (style == null) {
147150
style = {};
148151
}
149-
for (r in rules) {
150-
if (!r.match(c)) {
151-
continue;
152+
153+
var styleCacheKey = @:privateAccess c.styleCacheKey;
154+
155+
var cachedDirectives = _componentStyleCache.get(styleCacheKey);
156+
if (cachedDirectives != null) {
157+
for (directives in cachedDirectives) {
158+
style.mergeDirectives(directives);
159+
}
160+
/*
161+
var n = 0;
162+
for (_ in _componentStyleCache.keys()) {
163+
n++;
164+
}
165+
trace(n);
166+
*/
167+
} else {
168+
//trace(styleCacheKey);
169+
cachedDirectives = [];
170+
for (r in rules) {
171+
if (!r.match(c)) {
172+
continue;
173+
}
174+
175+
cachedDirectives.push(r.directives);
152176
}
153177

154-
style.mergeDirectives(r.directives);
178+
for (directives in cachedDirectives) {
179+
style.mergeDirectives(directives);
180+
}
181+
_componentStyleCache.set(styleCacheKey, cachedDirectives);
155182
}
156183

184+
157185
return style;
158186
}
159187
}

0 commit comments

Comments
 (0)