根据AMF 3 specification AMF 将数组存储为单独的、针对密集数组进行优化的方式,并且还以优化的方式存储 int 以最小化用于小 int 的字节数。这样,您的 0-99 值数组将存储为:
00000000 0x09 ; array marker
00000001 0x81 0x49 ; array length in U29A format = 100
00000003 0x01 ; UTF8-empty - this array is all dense
00000004 to 000000CB - integer data, first integer marker 0x04, then an integer in U29.
所有整数都小于 128,因此它们表示为等于整数的单字节。这使得数组中的 small int 占用 2 个字节,而不是完整的 4 个字节,大整数甚至 8 个字节。
现在,对于 Vector,由于默认情况下 Vector 是密集的,最好不要将整数转换为 U29 格式,因为任何大于 0x40000000 的内容都会转换为 DOUBLE(又名Number),这对向量不利,所以Vector.<int> 按原样存储,向量内每个整数 4 个字节。
00000000 0x0D ; vector-int marker
00000001 0x81 0x49 ; U29 length of the vector = 100
00000003 0x00 ; fixed vector marker, 0 is not fixed
00000004 to 00000193 - integers in U32
因此,对于 small 整数,数组占用的空间比整数向量少,但对于大整数,数组每个存储的整数最多可占用 9 个字节,而向量将始终使用 4 个字节每个整数。
考虑对您的代码进行以下更改:
var arr:Array = [];
var vec:Vector.<int> = new Vector.<int>;
for (var i:int = 0; i < 100; ++i) {
var x:int=Math.floor(Math.random()*4294967295); // 0 to 0xFFFFFFFE
arr.push(x);
vec.push(x);
trace(x);
}
var b:ByteArray;
b = new ByteArray();
b.writeObject(arr);
trace("arr",b.length); // some variable value will be shown, up to 724
b = new ByteArray();
b.writeObject(vec);
trace("vec",b.length); // here the value will always be 404