-
Bug
-
Resolution: Done
-
None
-
None
-
Operating System: All
Platform: All
-
5378
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)
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)
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().
- is duplicated by
-
MDSAL-364 Generate unions generate getValue()
- Resolved