26
26
package java .util ;
27
27
28
28
import jdk .internal .util .ArraysSupport ;
29
+ import jdk .internal .vm .annotation .ForceInline ;
29
30
import jdk .internal .vm .annotation .IntrinsicCandidate ;
30
31
31
32
import java .io .Serializable ;
@@ -3534,12 +3535,22 @@ public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]>
3534
3535
* @since 1.6
3535
3536
*/
3536
3537
public static byte [] copyOf (byte [] original , int newLength ) {
3538
+ if (newLength == original .length ) {
3539
+ return copyOf (original );
3540
+ }
3537
3541
byte [] copy = new byte [newLength ];
3538
3542
System .arraycopy (original , 0 , copy , 0 ,
3539
3543
Math .min (original .length , newLength ));
3540
3544
return copy ;
3541
3545
}
3542
3546
3547
+ @ ForceInline
3548
+ private static byte [] copyOf (byte [] original ) {
3549
+ byte [] copy = new byte [original .length ];
3550
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3551
+ return copy ;
3552
+ }
3553
+
3543
3554
/**
3544
3555
* Copies the specified array, truncating or padding with zeros (if necessary)
3545
3556
* so the copy has the specified length. For all indices that are
@@ -3558,12 +3569,22 @@ public static byte[] copyOf(byte[] original, int newLength) {
3558
3569
* @since 1.6
3559
3570
*/
3560
3571
public static short [] copyOf (short [] original , int newLength ) {
3572
+ if (newLength == original .length ) {
3573
+ return copyOf (original );
3574
+ }
3561
3575
short [] copy = new short [newLength ];
3562
3576
System .arraycopy (original , 0 , copy , 0 ,
3563
3577
Math .min (original .length , newLength ));
3564
3578
return copy ;
3565
3579
}
3566
3580
3581
+ @ ForceInline
3582
+ private static short [] copyOf (short [] original ) {
3583
+ short [] copy = new short [original .length ];
3584
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3585
+ return copy ;
3586
+ }
3587
+
3567
3588
/**
3568
3589
* Copies the specified array, truncating or padding with zeros (if necessary)
3569
3590
* so the copy has the specified length. For all indices that are
@@ -3582,12 +3603,22 @@ public static short[] copyOf(short[] original, int newLength) {
3582
3603
* @since 1.6
3583
3604
*/
3584
3605
public static int [] copyOf (int [] original , int newLength ) {
3606
+ if (newLength == original .length ) {
3607
+ return copyOf (original );
3608
+ }
3585
3609
int [] copy = new int [newLength ];
3586
3610
System .arraycopy (original , 0 , copy , 0 ,
3587
3611
Math .min (original .length , newLength ));
3588
3612
return copy ;
3589
3613
}
3590
3614
3615
+ @ ForceInline
3616
+ private static int [] copyOf (int [] original ) {
3617
+ int [] copy = new int [original .length ];
3618
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3619
+ return copy ;
3620
+ }
3621
+
3591
3622
/**
3592
3623
* Copies the specified array, truncating or padding with zeros (if necessary)
3593
3624
* so the copy has the specified length. For all indices that are
@@ -3606,12 +3637,22 @@ public static int[] copyOf(int[] original, int newLength) {
3606
3637
* @since 1.6
3607
3638
*/
3608
3639
public static long [] copyOf (long [] original , int newLength ) {
3640
+ if (newLength == original .length ) {
3641
+ return copyOf (original );
3642
+ }
3609
3643
long [] copy = new long [newLength ];
3610
3644
System .arraycopy (original , 0 , copy , 0 ,
3611
3645
Math .min (original .length , newLength ));
3612
3646
return copy ;
3613
3647
}
3614
3648
3649
+ @ ForceInline
3650
+ private static long [] copyOf (long [] original ) {
3651
+ long [] copy = new long [original .length ];
3652
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3653
+ return copy ;
3654
+ }
3655
+
3615
3656
/**
3616
3657
* Copies the specified array, truncating or padding with null characters (if necessary)
3617
3658
* so the copy has the specified length. For all indices that are valid
@@ -3630,12 +3671,22 @@ public static long[] copyOf(long[] original, int newLength) {
3630
3671
* @since 1.6
3631
3672
*/
3632
3673
public static char [] copyOf (char [] original , int newLength ) {
3674
+ if (newLength == original .length ) {
3675
+ return copyOf (original );
3676
+ }
3633
3677
char [] copy = new char [newLength ];
3634
3678
System .arraycopy (original , 0 , copy , 0 ,
3635
3679
Math .min (original .length , newLength ));
3636
3680
return copy ;
3637
3681
}
3638
3682
3683
+ @ ForceInline
3684
+ private static char [] copyOf (char [] original ) {
3685
+ char [] copy = new char [original .length ];
3686
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3687
+ return copy ;
3688
+ }
3689
+
3639
3690
/**
3640
3691
* Copies the specified array, truncating or padding with zeros (if necessary)
3641
3692
* so the copy has the specified length. For all indices that are
@@ -3654,12 +3705,22 @@ public static char[] copyOf(char[] original, int newLength) {
3654
3705
* @since 1.6
3655
3706
*/
3656
3707
public static float [] copyOf (float [] original , int newLength ) {
3708
+ if (newLength == original .length ) {
3709
+ return copyOf (original );
3710
+ }
3657
3711
float [] copy = new float [newLength ];
3658
3712
System .arraycopy (original , 0 , copy , 0 ,
3659
3713
Math .min (original .length , newLength ));
3660
3714
return copy ;
3661
3715
}
3662
3716
3717
+ @ ForceInline
3718
+ private static float [] copyOf (float [] original ) {
3719
+ float [] copy = new float [original .length ];
3720
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3721
+ return copy ;
3722
+ }
3723
+
3663
3724
/**
3664
3725
* Copies the specified array, truncating or padding with zeros (if necessary)
3665
3726
* so the copy has the specified length. For all indices that are
@@ -3678,12 +3739,22 @@ public static float[] copyOf(float[] original, int newLength) {
3678
3739
* @since 1.6
3679
3740
*/
3680
3741
public static double [] copyOf (double [] original , int newLength ) {
3742
+ if (newLength == original .length ) {
3743
+ return copyOf (original );
3744
+ }
3681
3745
double [] copy = new double [newLength ];
3682
3746
System .arraycopy (original , 0 , copy , 0 ,
3683
3747
Math .min (original .length , newLength ));
3684
3748
return copy ;
3685
3749
}
3686
3750
3751
+ @ ForceInline
3752
+ private static double [] copyOf (double [] original ) {
3753
+ double [] copy = new double [original .length ];
3754
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3755
+ return copy ;
3756
+ }
3757
+
3687
3758
/**
3688
3759
* Copies the specified array, truncating or padding with {@code false} (if necessary)
3689
3760
* so the copy has the specified length. For all indices that are
@@ -3702,12 +3773,22 @@ public static double[] copyOf(double[] original, int newLength) {
3702
3773
* @since 1.6
3703
3774
*/
3704
3775
public static boolean [] copyOf (boolean [] original , int newLength ) {
3776
+ if (newLength == original .length ) {
3777
+ return copyOf (original );
3778
+ }
3705
3779
boolean [] copy = new boolean [newLength ];
3706
3780
System .arraycopy (original , 0 , copy , 0 ,
3707
3781
Math .min (original .length , newLength ));
3708
3782
return copy ;
3709
3783
}
3710
3784
3785
+ @ ForceInline
3786
+ private static boolean [] copyOf (boolean [] original ) {
3787
+ boolean [] copy = new boolean [original .length ];
3788
+ System .arraycopy (original , 0 , copy , 0 , original .length );
3789
+ return copy ;
3790
+ }
3791
+
3711
3792
/**
3712
3793
* Copies the specified range of the specified array into a new array.
3713
3794
* The initial index of the range ({@code from}) must lie between zero
@@ -3789,6 +3870,13 @@ public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? exte
3789
3870
return copy ;
3790
3871
}
3791
3872
3873
+ @ ForceInline
3874
+ private static void checkLength (int from , int to ) {
3875
+ if (to < from ) {
3876
+ throw new IllegalArgumentException (from + " > " + to );
3877
+ }
3878
+ }
3879
+
3792
3880
/**
3793
3881
* Copies the specified range of the specified array into a new array.
3794
3882
* The initial index of the range ({@code from}) must lie between zero
@@ -3816,9 +3904,17 @@ public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? exte
3816
3904
* @since 1.6
3817
3905
*/
3818
3906
public static byte [] copyOfRange (byte [] original , int from , int to ) {
3907
+ // Tickle the JIT to fold special cases optimally
3908
+ if (from != 0 || to != original .length )
3909
+ return copyOfRangeByte (original , from , to );
3910
+ else // from == 0 && to == original.length
3911
+ return copyOf (original );
3912
+ }
3913
+
3914
+ @ ForceInline
3915
+ private static byte [] copyOfRangeByte (byte [] original , int from , int to ) {
3916
+ checkLength (from , to );
3819
3917
int newLength = to - from ;
3820
- if (newLength < 0 )
3821
- throw new IllegalArgumentException (from + " > " + to );
3822
3918
byte [] copy = new byte [newLength ];
3823
3919
System .arraycopy (original , from , copy , 0 ,
3824
3920
Math .min (original .length - from , newLength ));
@@ -3852,9 +3948,17 @@ public static byte[] copyOfRange(byte[] original, int from, int to) {
3852
3948
* @since 1.6
3853
3949
*/
3854
3950
public static short [] copyOfRange (short [] original , int from , int to ) {
3951
+ // Tickle the JIT to fold special cases optimally
3952
+ if (from != 0 || to != original .length )
3953
+ return copyOfRangeShort (original , from , to );
3954
+ else // from == 0 && to == original.length
3955
+ return copyOf (original );
3956
+ }
3957
+
3958
+ @ ForceInline
3959
+ private static short [] copyOfRangeShort (short [] original , int from , int to ) {
3960
+ checkLength (from , to );
3855
3961
int newLength = to - from ;
3856
- if (newLength < 0 )
3857
- throw new IllegalArgumentException (from + " > " + to );
3858
3962
short [] copy = new short [newLength ];
3859
3963
System .arraycopy (original , from , copy , 0 ,
3860
3964
Math .min (original .length - from , newLength ));
@@ -3888,9 +3992,17 @@ public static short[] copyOfRange(short[] original, int from, int to) {
3888
3992
* @since 1.6
3889
3993
*/
3890
3994
public static int [] copyOfRange (int [] original , int from , int to ) {
3995
+ // Tickle the JIT to fold special cases optimally
3996
+ if (from != 0 || to != original .length )
3997
+ return copyOfRangeInt (original , from , to );
3998
+ else // from == 0 && to == original.length
3999
+ return copyOf (original );
4000
+ }
4001
+
4002
+ @ ForceInline
4003
+ private static int [] copyOfRangeInt (int [] original , int from , int to ) {
4004
+ checkLength (from , to );
3891
4005
int newLength = to - from ;
3892
- if (newLength < 0 )
3893
- throw new IllegalArgumentException (from + " > " + to );
3894
4006
int [] copy = new int [newLength ];
3895
4007
System .arraycopy (original , from , copy , 0 ,
3896
4008
Math .min (original .length - from , newLength ));
@@ -3924,9 +4036,17 @@ public static int[] copyOfRange(int[] original, int from, int to) {
3924
4036
* @since 1.6
3925
4037
*/
3926
4038
public static long [] copyOfRange (long [] original , int from , int to ) {
4039
+ // Tickle the JIT to fold special cases optimally
4040
+ if (from != 0 || to != original .length )
4041
+ return copyOfRangeLong (original , from , to );
4042
+ else // from == 0 && to == original.length
4043
+ return copyOf (original );
4044
+ }
4045
+
4046
+ @ ForceInline
4047
+ private static long [] copyOfRangeLong (long [] original , int from , int to ) {
4048
+ checkLength (from , to );
3927
4049
int newLength = to - from ;
3928
- if (newLength < 0 )
3929
- throw new IllegalArgumentException (from + " > " + to );
3930
4050
long [] copy = new long [newLength ];
3931
4051
System .arraycopy (original , from , copy , 0 ,
3932
4052
Math .min (original .length - from , newLength ));
@@ -3960,9 +4080,17 @@ public static long[] copyOfRange(long[] original, int from, int to) {
3960
4080
* @since 1.6
3961
4081
*/
3962
4082
public static char [] copyOfRange (char [] original , int from , int to ) {
4083
+ // Tickle the JIT to fold special cases optimally
4084
+ if (from != 0 || to != original .length )
4085
+ return copyOfRangeChar (original , from , to );
4086
+ else // from == 0 && to == original.length
4087
+ return copyOf (original );
4088
+ }
4089
+
4090
+ @ ForceInline
4091
+ private static char [] copyOfRangeChar (char [] original , int from , int to ) {
4092
+ checkLength (from , to );
3963
4093
int newLength = to - from ;
3964
- if (newLength < 0 )
3965
- throw new IllegalArgumentException (from + " > " + to );
3966
4094
char [] copy = new char [newLength ];
3967
4095
System .arraycopy (original , from , copy , 0 ,
3968
4096
Math .min (original .length - from , newLength ));
@@ -3996,9 +4124,17 @@ public static char[] copyOfRange(char[] original, int from, int to) {
3996
4124
* @since 1.6
3997
4125
*/
3998
4126
public static float [] copyOfRange (float [] original , int from , int to ) {
4127
+ // Tickle the JIT to fold special cases optimally
4128
+ if (from != 0 || to != original .length )
4129
+ return copyOfRangeFloat (original , from , to );
4130
+ else // from == 0 && to == original.length
4131
+ return copyOf (original );
4132
+ }
4133
+
4134
+ @ ForceInline
4135
+ private static float [] copyOfRangeFloat (float [] original , int from , int to ) {
4136
+ checkLength (from , to );
3999
4137
int newLength = to - from ;
4000
- if (newLength < 0 )
4001
- throw new IllegalArgumentException (from + " > " + to );
4002
4138
float [] copy = new float [newLength ];
4003
4139
System .arraycopy (original , from , copy , 0 ,
4004
4140
Math .min (original .length - from , newLength ));
@@ -4032,9 +4168,17 @@ public static float[] copyOfRange(float[] original, int from, int to) {
4032
4168
* @since 1.6
4033
4169
*/
4034
4170
public static double [] copyOfRange (double [] original , int from , int to ) {
4171
+ // Tickle the JIT to fold special cases optimally
4172
+ if (from != 0 || to != original .length )
4173
+ return copyOfRangeDouble (original , from , to );
4174
+ else // from == 0 && to == original.length
4175
+ return copyOf (original );
4176
+ }
4177
+
4178
+ @ ForceInline
4179
+ private static double [] copyOfRangeDouble (double [] original , int from , int to ) {
4180
+ checkLength (from , to );
4035
4181
int newLength = to - from ;
4036
- if (newLength < 0 )
4037
- throw new IllegalArgumentException (from + " > " + to );
4038
4182
double [] copy = new double [newLength ];
4039
4183
System .arraycopy (original , from , copy , 0 ,
4040
4184
Math .min (original .length - from , newLength ));
@@ -4068,9 +4212,17 @@ public static double[] copyOfRange(double[] original, int from, int to) {
4068
4212
* @since 1.6
4069
4213
*/
4070
4214
public static boolean [] copyOfRange (boolean [] original , int from , int to ) {
4215
+ // Tickle the JIT to fold special cases optimally
4216
+ if (from != 0 || to != original .length )
4217
+ return copyOfRangeBoolean (original , from , to );
4218
+ else // from == 0 && to == original.length
4219
+ return copyOf (original );
4220
+ }
4221
+
4222
+ @ ForceInline
4223
+ private static boolean [] copyOfRangeBoolean (boolean [] original , int from , int to ) {
4224
+ checkLength (from , to );
4071
4225
int newLength = to - from ;
4072
- if (newLength < 0 )
4073
- throw new IllegalArgumentException (from + " > " + to );
4074
4226
boolean [] copy = new boolean [newLength ];
4075
4227
System .arraycopy (original , from , copy , 0 ,
4076
4228
Math .min (original .length - from , newLength ));
0 commit comments