I changed all my for-s (that didn't need a check before the 1st run) to do-while :)
Let's see if anyone can come up with one with less bytes !
I started to implement the same-value array-init optimization, and realized why it was so much better than the do loop - and that you can rewrite the do loop to get almost identical bytecode.
So this version saves another 9 bytes over my previous one (although if you have my optimizer enabled for both versions, it only saves 6 - and I should fix that).
var i = 6; var y = new [i] as Array<Number>; do { i--; y[i] = 0; // or zero if you need it for something else } while (i);
Anyway I tried to refactor from Array<Number> to ByteArray, and it's not worth in my case, it's adds 22 bytes to the code
I was thinking about this. Why would you need more code?
Maybe because of
which now became:var hrz = UserProfile.getHeartRateZones(currentSport);heartRateZones = []b;heartRateZones.addAll(hrz);
But again... why?
Can't you just do something like
typedef HRArray as ByteArray or Array<Number>;
var heartRateZones as HRArray;
and then when you want to create your array of 6 zeros, just do
heartRateZones = new [6]b;
but when you want to get the actual zones, do
heartRateZones = UserProfile.getHeartRateZones(currentSport);
I'll have to try. I first glance I thought it'll need lot of code to convert the bytes to Numbers or vica versa, but funny enough the elements in the ByteArray are returned as Number, so it really can work.
I did that. I had to add as Number to a few places like: var zoneHr = heartRateZones[z] as Number, because it complained that zoneHr is Any where it expects Number, but that probably should be ok. However the code size (neither the optimized and the post build) did not change even after I changed new Number[6] to new [6]b with 2.0.57
I did that
So even though you can drop the initialization of the ByteArray, (which I think ended up costing around 26 bytes for an Array), you don't get a code size improvement? But presumably its not worse?
It's not worse, it's identical. But I think part of the story is that since the original question I've optimized that part of the code - partially with tricks from you - so it looks a bit different now, i.e it doesn't fill the array with 0-s at the beginning because I anyway fill it with positive values (it's parsing a string like: "z:100,110,120,130,140,150") and the only reason for filling it up with 0-s was to cover for bad user input which I do better now.
i.e it doesn't fill the array with 0-s at the beginning
In that case, no change in the code size is exactly what I would expect.
The point of using a ByteArray was that you get the zero filling for free. ie "new [6]b" returns a ByteArray of 6 zeros, while "new [6]" returns an Array of 6 nulls. (I guess you also save a few bytes of heap memory too, because the ByteArray is smaller).