Uploaded image for project: 'mdsal'
  1. mdsal
  2. MDSAL-130

Java Binding: improve union type implementation

XMLWordPrintable

    • 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)

      { 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().

            Unassigned Unassigned
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: