[YANGTOOLS-166] Generated builder cpu/memory efficiency Created: 16/May/14  Updated: 10/Apr/22  Due: 20/May/14  Resolved: 16/May/14

Status: Resolved
Project: yangtools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement
Reporter: Robert Varga Assignee: Martin Vitez
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Operating System: All
Platform: All



 Description   

The builders we generate are not entirely memory/cpu efficient. Specifically the way augmentations are retained is sub-optimal, creating undue pressure. A typical object looks like this:

private static final class TlvsImpl implements Tlvs {

public Class<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs> getImplementedInterface()

{ return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs.class; }

private final OfList _ofList;
private Map<Class<? extends Augmentation<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs>>, Augmentation<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs>> augmentation = new HashMap<>();

private TlvsImpl(TlvsBuilder builder)

{ this._ofList = builder.getOfList(); this.augmentation.putAll(builder.augmentation); }

Note how "augmentation" allocation is disconnected from the actually filling – this potentially inefficient. So move the allocation from field to constructor, so we can size the map appropriately.

Second, most of augmentation instances end up being empty or only a few entries. So check for these circumstances and allocate optimized versions of these maps:

switch (builder.augmentation.size()) {
case 0:
this.augmentation = Collections.emptyMap();
break;
case 1:
final MapEntry<...> e = builder.augmentation.entrySet().iterator().next();
this.augmentation = Collections.singletonMap(e.getKey(), e.getValue());
break;
default:
this.augmentation = new HashMap<>(builder.augmentation);
}

This change is API-safe and should be applied to hydrogen/stable as well.



 Comments   
Comment by Martin Vitez [ 16/May/14 ]

Proposed patch:

https://git.opendaylight.org/gerrit/#/c/7106/

Generated at Wed Feb 07 20:52:27 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.