[MDSAL-130] Java Binding: improve union type implementation Created: 17/Feb/16  Updated: 04/Aug/18  Resolved: 04/Aug/18

Status: Resolved
Project: mdsal
Component/s: Binding codegen, Binding V2 codegen
Affects Version/s: None
Fix Version/s: Fluorine

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

Operating System: All
Platform: All


Issue Links:
Duplicate
is duplicated by MDSAL-364 Generate unions generate getValue() Resolved
External issue ID: 5378

 Description   

Current implementation of unions which are composed solely of strings is quite wasteful, as it causes char[] duplication.

A typical example from ietf-inet-types is ip-prefix. We generate the following fields:

private final Ipv4Prefix _ipv4Prefix;
private final Ipv6Prefix _ipv6Prefix;
private final char[] _value;

We generate the default constructor for use with JMX:

@ConstructorProperties("value")
public IpPrefix(char[] _value)

{ java.lang.String defVal = new java.lang.String(_value); IpPrefix defInst = IpPrefixBuilder.getDefaultInstance(defVal); this._ipv4Prefix = defInst._ipv4Prefix; this._ipv6Prefix = defInst._ipv6Prefix; this._value = _value == null ? null : _value.clone(); }

This means that we have two copies of _value stored: one inside one of the prefixes and one retained via explicit clone.

As it turns out, we do a very similar thing in both normal constructors:

public IpPrefix(Ipv4Prefix _ipv4Prefix)

{ super(); this._ipv4Prefix = _ipv4Prefix; this._ipv6Prefix = null; this._value = _ipv4Prefix.getValue().toString().toCharArray(); }

public IpPrefix(Ipv6Prefix _ipv6Prefix)

{ super(); this._ipv6Prefix = _ipv6Prefix; this._ipv4Prefix = null; this._value = _ipv6Prefix.getValue().toString().toCharArray(); }

While this is generic and works for all types, for strings this ends up being inefficient.

Generated code should be modified so that if a union has only string types, _value field should not be generated and getValue() should be implemented as:

public char[] getValue() {
final String str;
if (_ipv4Prefix != null)

{ str = _ipv4Prefix.getValue(); }

else if (_ipv6Prefix != null)

{ str = _ipv6Prefix.getValue(); }

else

{ throw new IllegalStateException(...); }

return str.toCharArray();
}

Also note that this sort of optimization also applies when a union member is a boolean, as there are only two possible strings accessible via Boolean.toString().


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