ソースを参照

[cs][java] don't alloc excessive cell on Array resize (closes #5475)

Aleksandr Kuzmenko 6 年 前
コミット
e24798614b
2 ファイル変更4 行追加2 行削除
  1. 2 1
      std/cs/_std/Array.hx
  2. 2 1
      std/java/_std/Array.hx

+ 2 - 1
std/cs/_std/Array.hx

@@ -31,6 +31,7 @@ final class Array<T> implements ArrayAccess<T> {
 	private var __a:NativeArray<T>;
 
 	@:skipReflection static var __hx_toString_depth = 0;
+	@:skipReflection static inline final __hx_defaultCapacity = 4;
 
 #if erase_generics
 	inline private static function ofNative<X>(native:NativeArray<Dynamic>):Array<X>
@@ -166,7 +167,7 @@ final class Array<T> implements ArrayAccess<T> {
 	{
 		if (length >= __a.Length)
 		{
-			var newLen = (length << 1) + 1;
+			var newLen = length == 0 ? __hx_defaultCapacity : (length << 1);
 			var newarr = new NativeArray(newLen);
 			__a.CopyTo(newarr, 0);
 

+ 2 - 1
std/java/_std/Array.hx

@@ -36,6 +36,7 @@ import java.NativeArray;
 	private var __a:NativeArray<T>;
 
 	@:skipReflection static var __hx_toString_depth = 0;
+	@:skipReflection static inline final __hx_defaultCapacity = 4;
 
 	@:functionCode('
 			return new Array<X>(_native);
@@ -143,7 +144,7 @@ import java.NativeArray;
 		var length = length;
 		if (length >= __a.length)
 		{
-			var newLen = (length << 1) + 1;
+			var newLen = length == 0 ? __hx_defaultCapacity : (length << 1);
 			var newarr = new NativeArray(newLen);
 			System.arraycopy(__a, 0, newarr, 0, __a.length);