Skip to content

Commit ca269a7

Browse files
cache consumed prop keys
1 parent 528ce06 commit ca269a7

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

benchmark/add_unconsumed_props_benchmark.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@
1111
<script src="out.js"></script>
1212
</body>
1313
</html>
14-

lib/src/component_declaration/component_base_2.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,21 @@ abstract class UiComponent2<TProps extends UiProps> extends react.Component2
358358
@toBeGenerated
359359
PropsMetaCollection get propsMeta => throw UngeneratedError(member: #propsMeta);
360360

361+
/// Cache the flattened consumed prop keys to avoid rebuilding on every call.
362+
/// This is lazily initialized and computed only once per component instance.
363+
HashSet<String>? _cachedConsumedPropKeys;
364+
365+
/// Returns the cached flattened set of consumed prop keys, computing it if necessary.
366+
HashSet<String>? get _consumedPropKeys {
367+
if (_cachedConsumedPropKeys == null && consumedProps != null) {
368+
_cachedConsumedPropKeys = consumedProps!.fold(
369+
HashSet<String>(),
370+
(set, consumedProps) => set?..addAll(consumedProps.keys)
371+
);
372+
}
373+
return _cachedConsumedPropKeys;
374+
}
375+
361376
/// A prop modifier that passes a reference of a component's `props` to be updated with any unconsumed props.
362377
///
363378
/// Call within `modifyProps` like so:
@@ -373,10 +388,7 @@ abstract class UiComponent2<TProps extends UiProps> extends react.Component2
373388
///
374389
/// > Related [addUnconsumedDomProps]
375390
void addUnconsumedProps(Map props) {
376-
// TODO: cache this value to avoid unnecessary looping
377-
var consumedPropKeys = consumedProps?.fold(HashSet<String>(), (set, consumedProps) => set..addAll(consumedProps.keys));
378-
379-
forwardUnconsumedPropsV2(this.props, propsToUpdate: props, keysToOmit: consumedPropKeys);
391+
forwardUnconsumedPropsV2(this.props, propsToUpdate: props, keysToOmit: _consumedPropKeys);
380392
}
381393

382394
/// A prop modifier that passes a reference of a component's `props` to be updated with any unconsumed `DomProps`.
@@ -394,9 +406,7 @@ abstract class UiComponent2<TProps extends UiProps> extends react.Component2
394406
///
395407
/// > Related [addUnconsumedProps]
396408
void addUnconsumedDomProps(Map props) {
397-
var consumedPropKeys = consumedProps?.fold(HashSet<String>(), (set, consumedProps) => set..addAll(consumedProps.keys));
398-
399-
forwardUnconsumedPropsV2(this.props, propsToUpdate: props, keysToOmit: consumedPropKeys, onlyCopyDomProps: true);
409+
forwardUnconsumedPropsV2(this.props, propsToUpdate: props, keysToOmit: _consumedPropKeys, onlyCopyDomProps: true);
400410
}
401411

402412
/// Returns a copy of this component's props with React props optionally omitted, and

0 commit comments

Comments
 (0)