[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 |
||
| Issue Links: |
|
||||||||
| 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; We generate the default constructor for use with JMX: @ConstructorProperties("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() { 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(). |