Skip to content

optimize decodeValueTypes#89

Merged
REAndroid merged 5 commits intoREAndroid:mainfrom
Tahgolov:refactor/decode-value-types
Aug 15, 2025
Merged

optimize decodeValueTypes#89
REAndroid merged 5 commits intoREAndroid:mainfrom
Tahgolov:refactor/decode-value-types

Conversation

@Tahgolov
Copy link
Contributor

The idea is using "population count" to pre-compute final array size and eliminate usage of temporary array.

@REAndroid
Copy link
Owner

Nice approach, let me run few tests

@REAndroid
Copy link
Owner

@Tahgolov What if data is ANY (0xffff) ? It is returning all values instead of only ANY

  • Use braces {} for all conditional and looping statements

@Tahgolov
Copy link
Contributor Author

@REAndroid Got it. I'll fix it and add braces to conditions and loops.

@Tahgolov
Copy link
Contributor Author

@REAndroid Improments:

  • reuse value type bits extracted from data
  • optimize conditions for null and ANY

@REAndroid
Copy link
Owner

I found another problem, for data values out of expected enum values (e.g. data = 256) your code producing array with null elements

@Tahgolov
Copy link
Contributor Author

@REAndroid check

@REAndroid
Copy link
Owner

REAndroid commented Aug 15, 2025

@Tahgolov
One more comment: ANY is 0xffff not 0xff,

Just for future commits if it doesn't abuse your time please add a test (a documentation/description of public methods is very helpful).

Here is my suggestion for now, please correct and commit as necessary

    public static AttributeDataFormat[] decodeValueTypes(int data) {
        if ((data & 0xffff) == 0xffff) {
            return new AttributeDataFormat[]{ANY};
        }
        data &= 0xff;
        if (data == 0) {
            return null;
        }
        AttributeDataFormat[] results = new AttributeDataFormat[Integer.bitCount(data)];
        AttributeDataFormat[] valueTypes = VALUE_TYPES;
        int length = valueTypes.length - 1;
        int j = 0;
        for (int i = 0; i < length; i++) {
            if ((data & 1) != 0) {
                results[j] = valueTypes[i];
                j ++;
            }
            data = data >> 1;
        }
        return results;
    }

@Tahgolov
Copy link
Contributor Author

@REAndroid Understood, my bad. I'll write simple unit tests in the future to don't spend our time.

@REAndroid REAndroid merged commit 317c714 into REAndroid:main Aug 15, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants