1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 4 Jun 2020 22:43:29 -0400
Subject: [PATCH] Optimize Light Engine
Massive update to light to improve performance and chunk loading/generation.
1) Massive bit packing/unpacking optimizations and inlining.
A lot of performance has to do with constant packing and unpacking of bits.
We now inline a most bit operations, and re-use base x/y/z bits in many places.
This helps with cpu level processing to just do all the math at once instead
of having to jump in and out of function calls.
This much logic also is likely over the JVM Inline limit for JIT too.
2) Applied a few of JellySquid's Phosphor mod optimizations such as
- ensuring we don't notify neighbor chunks when neighbor chunk doesn't need to be notified
- reduce hasLight checks in initializing light, and prob some more, they are tagged JellySquid where phosphor influence was used.
3) Optimize hot path accesses to getting updating chunk to have less branching
4) Optimize getBlock accesses to have less branching, and less unpacking
5) Have a separate urgent bucket for chunk light tasks. These tasks will always cut in line over non blocking light tasks.
6) Retain chunk priority while light tasks are enqueued. So if a task comes in at high priority but the queue is full
of tasks already at a lower priority, before the task was simply added to the end. Now it can cut in line to the front.
this applies for both urgent and non urgent tasks.
7) Buffer non urgent tasks even if queueUpdate is called multiple times to improve efficiency.
8) Fix NPE risk that crashes server in getting nibble data
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
index 7c87ef638d538093e944341525a1027be5d15a0e..3a5a9aba111fed8b68818b95332b723ee2bce2ac 100644
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
@@ -1045,7 +1045,7 @@ public class ChunkProviderServer extends IChunkProvider {
if (ChunkProviderServer.this.tickDistanceManager()) {
return true;
} else {
- ChunkProviderServer.this.lightEngine.queueUpdate();
+ //ChunkProviderServer.this.lightEngine.queueUpdate(); // Paper - not needed
return super.executeNext() || execChunkTask; // Paper
}
} finally {
diff --git a/src/main/java/net/minecraft/server/LightEngineBlock.java b/src/main/java/net/minecraft/server/LightEngineBlock.java
index 07fadc21ee12138b52cc77c50da536fec5b032f5..a61d0a27e9525505eedaec8cde44216e807eb9a8 100644
--- a/src/main/java/net/minecraft/server/LightEngineBlock.java
+++ b/src/main/java/net/minecraft/server/LightEngineBlock.java
@@ -13,9 +13,11 @@ public final class LightEngineBlock extends LightEngineLayer<LightEngineStorageB
}
private int d(long i) {
- int j = BlockPosition.b(i);
- int k = BlockPosition.c(i);
- int l = BlockPosition.d(i);
+ // Paper start - inline math
+ int j = (int) (i >> 38);
+ int k = (int) ((i << 52) >> 52);
+ int l = (int) ((i << 26) >> 38);
+ // Paper end
IBlockAccess iblockaccess = this.a.c(j >> 4, l >> 4);
return iblockaccess != null ? iblockaccess.h(this.f.d(j, k, l)) : 0;
@@ -30,25 +32,33 @@ public final class LightEngineBlock extends LightEngineLayer<LightEngineStorageB
} else if (k >= 15) {
return k;
} else {
- int l = Integer.signum(BlockPosition.b(j) - BlockPosition.b(i));
- int i1 = Integer.signum(BlockPosition.c(j) - BlockPosition.c(i));
- int j1 = Integer.signum(BlockPosition.d(j) - BlockPosition.d(i));
+ // Paper start - reuse math - credit to JellySquid for idea
+ int jx = (int) (j >> 38);
+ int jy = (int) ((j << 52) >> 52);
+ int jz = (int) ((j << 26) >> 38);
+ int ix = (int) (i >> 38);
+ int iy = (int) ((i << 52) >> 52);
+ int iz = (int) ((i << 26) >> 38);
+ int l = Integer.signum(jx - ix);
+ int i1 = Integer.signum(jy - iy);
+ int j1 = Integer.signum(jz - iz);
+ // Paper end
EnumDirection enumdirection = EnumDirection.a(l, i1, j1);
if (enumdirection == null) {
return 15;
} else {
//MutableInt mutableint = new MutableInt(); // Paper - share mutableint, single threaded
- IBlockData iblockdata = this.a(j, mutableint);
-
- if (mutableint.getValue() >= 15) {
+ IBlockData iblockdata = this.getBlockOptimized(jx, jy, jz, mutableint); // Paper
+ int blockedLight = mutableint.getValue(); // Paper
+ if (blockedLight >= 15) { // Paper
return 15;
} else {
- IBlockData iblockdata1 = this.a(i, (MutableInt) null);
+ IBlockData iblockdata1 = this.getBlockOptimized(ix, iy, iz); // Paper
VoxelShape voxelshape = this.a(iblockdata1, i, enumdirection);
VoxelShape voxelshape1 = this.a(iblockdata, j, enumdirection.opposite());
- return VoxelShapes.b(voxelshape, voxelshape1) ? 15 : k + Math.max(1, mutableint.getValue());
+ return VoxelShapes.b(voxelshape, voxelshape1) ? 15 : k + Math.max(1, blockedLight); // Paper
}
}
}
@@ -56,14 +66,19 @@ public final class LightEngineBlock extends LightEngineLayer<LightEngineStorageB
@Override
protected void a(long i, int j, boolean flag) {
- long k = SectionPosition.e(i);
+ // Paper start - reuse unpacking, credit to JellySquid (Didn't do full optimization though)
+ int x = (int) (i >> 38);
+ int y = (int) ((i << 52) >> 52);
+ int z = (int) ((i << 26) >> 38);
+ long k = SectionPosition.blockPosAsSectionLong(x, y, z);
+ // Paper end
EnumDirection[] aenumdirection = LightEngineBlock.e;
int l = aenumdirection.length;
for (int i1 = 0; i1 < l; ++i1) {
EnumDirection enumdirection = aenumdirection[i1];
- long j1 = BlockPosition.a(i, enumdirection);
- long k1 = SectionPosition.e(j1);
+ long j1 = BlockPosition.getAdjacent(x, y, z, enumdirection); // Paper
+ long k1 = SectionPosition.getAdjacentFromBlockPos(x, y, z, enumdirection); // Paper
if (k == k1 || ((LightEngineStorageBlock) this.c).g(k1)) {
this.b(i, j1, j, flag);
@@ -88,27 +103,37 @@ public final class LightEngineBlock extends LightEngineLayer<LightEngineStorageB
}
}
- long j1 = SectionPosition.e(i);
- NibbleArray nibblearray = ((LightEngineStorageBlock) this.c).a(j1, true);
+ // Paper start
+ int baseX = (int) (i >> 38);
+ int baseY = (int) ((i << 52) >> 52);
+ int baseZ = (int) ((i << 26) >> 38);
+ long j1 = SectionPosition.blockPosAsSectionLong(baseX, baseY, baseZ);
+ NibbleArray nibblearray = this.c.updating.getUpdatingOptimized(j1);
+ // Paper end
EnumDirection[] aenumdirection = LightEngineBlock.e;
int k1 = aenumdirection.length;
for (int l1 = 0; l1 < k1; ++l1) {
EnumDirection enumdirection = aenumdirection[l1];
- long i2 = BlockPosition.a(i, enumdirection);
+ // Paper start
+ int newX = baseX + enumdirection.getAdjacentX();
+ int newY = baseY + enumdirection.getAdjacentY();
+ int newZ = baseZ + enumdirection.getAdjacentZ();
+ long i2 = BlockPosition.asLong(newX, newY, newZ);
if (i2 != j) {
- long j2 = SectionPosition.e(i2);
+ long j2 = SectionPosition.blockPosAsSectionLong(newX, newY, newZ);
+ // Paper end
NibbleArray nibblearray1;
if (j1 == j2) {
nibblearray1 = nibblearray;
} else {
- nibblearray1 = ((LightEngineStorageBlock) this.c).a(j2, true);
+ nibblearray1 = ((LightEngineStorageBlock) this.c).updating.getUpdatingOptimized(j2); // Paper
}
if (nibblearray1 != null) {
- int k2 = this.b(i2, i, this.a(nibblearray1, i2));
+ int k2 = this.b(i2, i, this.getNibbleLightInverse(nibblearray1, newX, newY, newZ)); // Paper
if (l > k2) {
l = k2;
diff --git a/src/main/java/net/minecraft/server/LightEngineGraphSection.java b/src/main/java/net/minecraft/server/LightEngineGraphSection.java
index 2eb37fb5796d423a70fa7321899a19b6625bbecc..13d067f48647dea63ef1bf3a2a3e0868074ba75f 100644
--- a/src/main/java/net/minecraft/server/LightEngineGraphSection.java
+++ b/src/main/java/net/minecraft/server/LightEngineGraphSection.java
@@ -13,14 +13,20 @@ public abstract class LightEngineGraphSection extends LightEngineGraph {
@Override
protected void a(long i, int j, boolean flag) {
+ // Paper start
+ int x = (int) (i >> 42);
+ int y = (int) (i << 44 >> 44);
+ int z = (int) (i << 22 >> 42);
+ // Paper end
for (int k = -1; k <= 1; ++k) {
for (int l = -1; l <= 1; ++l) {
for (int i1 = -1; i1 <= 1; ++i1) {
- long j1 = SectionPosition.a(i, k, l, i1);
+ if (k == 0 && l == 0 && i1 == 0) continue; // Paper
+ long j1 = (((long) (x + k) & 4194303L) << 42) | (((long) (y + l) & 1048575L)) | (((long) (z + i1) & 4194303L) << 20); // Paper
- if (j1 != i) {
+ //if (j1 != i) { // Paper - checked above
this.b(i, j1, j, flag);
- }
+ //} // Paper
}
}
}
@@ -31,10 +37,15 @@ public abstract class LightEngineGraphSection extends LightEngineGraph {
protected int a(long i, long j, int k) {
int l = k;
+ // Paper start
+ int x = (int) (i >> 42);
+ int y = (int) (i << 44 >> 44);
+ int z = (int) (i << 22 >> 42);
+ // Paper end
for (int i1 = -1; i1 <= 1; ++i1) {
for (int j1 = -1; j1 <= 1; ++j1) {
for (int k1 = -1; k1 <= 1; ++k1) {
- long l1 = SectionPosition.a(i, i1, j1, k1);
+ long l1 = (((long) (x + i1) & 4194303L) << 42) | (((long) (y + j1) & 1048575L)) | (((long) (z + k1) & 4194303L) << 20); // Paper
if (l1 == i) {
l1 = Long.MAX_VALUE;
diff --git a/src/main/java/net/minecraft/server/LightEngineLayer.java b/src/main/java/net/minecraft/server/LightEngineLayer.java
index 3e9731bcdd4c6370c11ffc93f9fafcaf60fe932b..b5d5dd1075fd6aabbfbbd60f219b76593fc54a76 100644
--- a/src/main/java/net/minecraft/server/LightEngineLayer.java
+++ b/src/main/java/net/minecraft/server/LightEngineLayer.java
@@ -11,10 +11,37 @@ public abstract class LightEngineLayer<M extends LightEngineStorageArray<M>, S e
protected final EnumSkyBlock b;
protected final S c;
private boolean f;
- protected final BlockPosition.MutableBlockPosition d = new BlockPosition.MutableBlockPosition();
+ protected final BlockPosition.MutableBlockPosition d = new BlockPosition.MutableBlockPosition(); protected final BlockPosition.MutableBlockPosition pos = d; // Paper
private final long[] g = new long[2];
- private final IBlockAccess[] h = new IBlockAccess[2];
+ private final IChunkAccess[] h = new IChunkAccess[2]; // Paper
+ // Paper start - see fully commented out method below (look for Bedrock)
+ // optimized method with less branching for when scenarios arent needed.
+ // avoid using mutable version if can
+ protected final IBlockData getBlockOptimized(int x, int y, int z, MutableInt mutableint) {
+ IChunkAccess iblockaccess = this.a(x >> 4, z >> 4);
+
+ if (iblockaccess == null) {
+ mutableint.setValue(16);
+ return Blocks.BEDROCK.getBlockData();
+ } else {
+ this.pos.setValues(x, y, z);
+ IBlockData iblockdata = iblockaccess.getType(x, y, z);
+ mutableint.setValue(iblockdata.b(this.a.getWorld(), this.pos));
+ return iblockdata.l() && iblockdata.e() ? iblockdata : Blocks.AIR.getBlockData();
+ }
+ }
+ protected final IBlockData getBlockOptimized(int x, int y, int z) {
+ IChunkAccess iblockaccess = this.a(x >> 4, z >> 4);
+
+ if (iblockaccess == null) {
+ return Blocks.BEDROCK.getBlockData();
+ } else {
+ IBlockData iblockdata = iblockaccess.getType(x, y, z);
+ return iblockdata.l() && iblockdata.e() ? iblockdata : Blocks.AIR.getBlockData();
+ }
+ }
+ // Paper end
public LightEngineLayer(ILightAccess ilightaccess, EnumSkyBlock enumskyblock, S s0) {
super(16, 256, 8192);
this.a = ilightaccess;
@@ -33,7 +60,7 @@ public abstract class LightEngineLayer<M extends LightEngineStorageArray<M>, S e
}
@Nullable
- private IBlockAccess a(int i, int j) {
+ private IChunkAccess a(int i, int j) { // Paper
long k = ChunkCoordIntPair.pair(i, j);
for (int l = 0; l < 2; ++l) {
@@ -42,7 +69,7 @@ public abstract class LightEngineLayer<M extends LightEngineStorageArray<M>, S e
}
}
- IBlockAccess iblockaccess = this.a.c(i, j);
+ IChunkAccess iblockaccess = (IChunkAccess) this.a.c(i, j); // Paper
for (int i1 = 1; i1 > 0; --i1) {
this.g[i1] = this.g[i1 - 1];
@@ -59,37 +86,39 @@ public abstract class LightEngineLayer<M extends LightEngineStorageArray<M>, S e
Arrays.fill(this.h, (Object) null);
}
- protected IBlockData a(long i, @Nullable MutableInt mutableint) {
- if (i == Long.MAX_VALUE) {
- if (mutableint != null) {
- mutableint.setValue(0);
- }
-
- return Blocks.AIR.getBlockData();
- } else {
- int j = SectionPosition.a(BlockPosition.b(i));
- int k = SectionPosition.a(BlockPosition.d(i));
- IBlockAccess iblockaccess = this.a(j, k);
-
- if (iblockaccess == null) {
- if (mutableint != null) {
- mutableint.setValue(16);
- }
-
- return Blocks.BEDROCK.getBlockData();
- } else {
- this.d.g(i);
- IBlockData iblockdata = iblockaccess.getType(this.d);
- boolean flag = iblockdata.l() && iblockdata.e();
-
- if (mutableint != null) {
- mutableint.setValue(iblockdata.b(this.a.getWorld(), (BlockPosition) this.d));
- }
-
- return flag ? iblockdata : Blocks.AIR.getBlockData();
- }
- }
- }
+ // Paper start - comment out, see getBlockOptimized
+// protected IBlockData a(long i, @Nullable MutableInt mutableint) {
+// if (i == Long.MAX_VALUE) {
+// if (mutableint != null) {
+// mutableint.setValue(0);
+// }
+//
+// return Blocks.AIR.getBlockData();
+// } else {
+// int j = SectionPosition.a(BlockPosition.b(i));
+// int k = SectionPosition.a(BlockPosition.d(i));
+// IBlockAccess iblockaccess = this.a(j, k);
+//
+// if (iblockaccess == null) {
+// if (mutableint != null) {
+// mutableint.setValue(16);
+// }
+//
+// return Blocks.BEDROCK.getBlockData();
+// } else {
+// this.d.g(i);
+// IBlockData iblockdata = iblockaccess.getType(this.d);
+// boolean flag = iblockdata.l() && iblockdata.e();
+//
+// if (mutableint != null) {
+// mutableint.setValue(iblockdata.b(this.a.getWorld(), (BlockPosition) this.d));
+// }
+//
+// return flag ? iblockdata : Blocks.AIR.getBlockData();
+// }
+// }
+// }
+ // Paper end
protected VoxelShape a(IBlockData iblockdata, long i, EnumDirection enumdirection) {
return iblockdata.l() ? iblockdata.a(this.a.getWorld(), this.d.g(i), enumdirection) : VoxelShapes.a();
@@ -124,8 +153,9 @@ public abstract class LightEngineLayer<M extends LightEngineStorageArray<M>, S e
return i == Long.MAX_VALUE ? 0 : 15 - this.c.i(i);
}
+ protected int getNibbleLightInverse(NibbleArray nibblearray, int x, int y, int z) { return 15 - nibblearray.a(x & 15, y & 15, z & 15); } // Paper - x/y/z version of below
protected int a(NibbleArray nibblearray, long i) {
- return 15 - nibblearray.a(SectionPosition.b(BlockPosition.b(i)), SectionPosition.b(BlockPosition.c(i)), SectionPosition.b(BlockPosition.d(i)));
+ return 15 - nibblearray.a((int) (i >> 38) & 15, (int) ((i << 52) >> 52) & 15, (int) ((i << 26) >> 38) & 15); // Paper
}
@Override
diff --git a/src/main/java/net/minecraft/server/LightEngineSky.java b/src/main/java/net/minecraft/server/LightEngineSky.java
index f0b57784006752e031800a12a1a3c1a5945c636b..32b52ca2462fa206b1184025cb3837d6c326db2d 100644
--- a/src/main/java/net/minecraft/server/LightEngineSky.java
+++ b/src/main/java/net/minecraft/server/LightEngineSky.java
@@ -29,21 +29,25 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
return k;
} else {
//MutableInt mutableint = new MutableInt(); // Paper - share mutableint, single threaded
- IBlockData iblockdata = this.a(j, mutableint);
-
- if (mutableint.getValue() >= 15) {
+ // Paper start - use x/y/z and optimized block lookup
+ int jx = (int) (j >> 38);
+ int jy = (int) ((j << 52) >> 52);
+ int jz = (int) ((j << 26) >> 38);
+ IBlockData iblockdata = this.getBlockOptimized(jx, jy, jz, mutableint);
+ int blockedLight = mutableint.getValue();
+ if (blockedLight >= 15) {
+ // Paper end
return 15;
} else {
- int l = BlockPosition.b(i);
- int i1 = BlockPosition.c(i);
- int j1 = BlockPosition.d(i);
- int k1 = BlockPosition.b(j);
- int l1 = BlockPosition.c(j);
- int i2 = BlockPosition.d(j);
- boolean flag = l == k1 && j1 == i2;
- int j2 = Integer.signum(k1 - l);
- int k2 = Integer.signum(l1 - i1);
- int l2 = Integer.signum(i2 - j1);
+ // Paper start - inline math
+ int ix = (int) (i >> 38);
+ int iy = (int) ((i << 52) >> 52);
+ int iz = (int) ((i << 26) >> 38);
+ boolean flag = ix == jx && iz == jz;
+ int j2 = Integer.signum(jx - ix);
+ int k2 = Integer.signum(jy - iy);
+ int l2 = Integer.signum(jz - iz);
+ // Paper end
EnumDirection enumdirection;
if (i == Long.MAX_VALUE) {
@@ -52,7 +56,7 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
enumdirection = EnumDirection.a(j2, k2, l2);
}
- IBlockData iblockdata1 = this.a(i, (MutableInt) null);
+ IBlockData iblockdata1 = i == Long.MAX_VALUE ? Blocks.AIR.getBlockData() : this.getBlockOptimized(ix, iy, iz); // Paper
VoxelShape voxelshape;
if (enumdirection != null) {
@@ -82,9 +86,9 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
}
}
- boolean flag1 = i == Long.MAX_VALUE || flag && i1 > l1;
+ boolean flag1 = i == Long.MAX_VALUE || flag && iy > jy; // Paper rename vars to iy > jy
- return flag1 && k == 0 && mutableint.getValue() == 0 ? 0 : k + Math.max(1, mutableint.getValue());
+ return flag1 && k == 0 && blockedLight == 0 ? 0 : k + Math.max(1, blockedLight); // Paper
}
}
}
@@ -92,10 +96,14 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
@Override
protected void a(long i, int j, boolean flag) {
- long k = SectionPosition.e(i);
- int l = BlockPosition.c(i);
- int i1 = SectionPosition.b(l);
- int j1 = SectionPosition.a(l);
+ // Paper start
+ int baseX = (int) (i >> 38);
+ int baseY = (int) ((i << 52) >> 52);
+ int baseZ = (int) ((i << 26) >> 38);
+ long k = SectionPosition.blockPosAsSectionLong(baseX, baseY, baseZ);
+ int i1 = baseY & 15;
+ int j1 = baseY >> 4;
+ // Paper end
int k1;
if (i1 != 0) {
@@ -110,15 +118,16 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
k1 = l1;
}
- long i2 = BlockPosition.a(i, 0, -1 - k1 * 16, 0);
- long j2 = SectionPosition.e(i2);
+ int newBaseY = baseY + (-1 - k1 * 16); // Paper
+ long i2 = BlockPosition.asLong(baseX, newBaseY, baseZ); // Paper
+ long j2 = SectionPosition.blockPosAsSectionLong(baseX, newBaseY, baseZ); // Paper
if (k == j2 || ((LightEngineStorageSky) this.c).g(j2)) {
this.b(i, i2, j, flag);
}
- long k2 = BlockPosition.a(i, EnumDirection.UP);
- long l2 = SectionPosition.e(k2);
+ long k2 = BlockPosition.asLong(baseX, baseY + 1, baseZ); // Paper
+ long l2 = SectionPosition.blockPosAsSectionLong(baseX, baseY + 1, baseZ); // Paper
if (k == l2 || ((LightEngineStorageSky) this.c).g(l2)) {
this.b(i, k2, j, flag);
@@ -133,8 +142,8 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
int k3 = 0;
while (true) {
- long l3 = BlockPosition.a(i, enumdirection.getAdjacentX(), -k3, enumdirection.getAdjacentZ());
- long i4 = SectionPosition.e(l3);
+ long l3 = BlockPosition.asLong(baseX + enumdirection.getAdjacentX(), baseY - k3, baseZ + enumdirection.getAdjacentZ()); // Paper
+ long i4 = SectionPosition.blockPosAsSectionLong(baseX + enumdirection.getAdjacentX(), baseY - k3, baseZ + enumdirection.getAdjacentZ()); // Paper
if (k == i4) {
this.b(i, l3, j, flag);
@@ -172,26 +181,36 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
}
}
- long j1 = SectionPosition.e(i);
- NibbleArray nibblearray = ((LightEngineStorageSky) this.c).a(j1, true);
+ // Paper start
+ int baseX = (int) (i >> 38);
+ int baseY = (int) ((i << 52) >> 52);
+ int baseZ = (int) ((i << 26) >> 38);
+ long j1 = SectionPosition.blockPosAsSectionLong(baseX, baseY, baseZ);
+ NibbleArray nibblearray = this.c.updating.getUpdatingOptimized(j1);
+ // Paper end
EnumDirection[] aenumdirection = LightEngineSky.e;
int k1 = aenumdirection.length;
for (int l1 = 0; l1 < k1; ++l1) {
EnumDirection enumdirection = aenumdirection[l1];
- long i2 = BlockPosition.a(i, enumdirection);
- long j2 = SectionPosition.e(i2);
+ // Paper start
+ int newX = baseX + enumdirection.getAdjacentX();
+ int newY = baseY + enumdirection.getAdjacentY();
+ int newZ = baseZ + enumdirection.getAdjacentZ();
+ long i2 = BlockPosition.asLong(newX, newY, newZ);
+ long j2 = SectionPosition.blockPosAsSectionLong(newX, newY, newZ);
+ // Paper end
NibbleArray nibblearray1;
if (j1 == j2) {
nibblearray1 = nibblearray;
} else {
- nibblearray1 = ((LightEngineStorageSky) this.c).a(j2, true);
+ nibblearray1 = ((LightEngineStorageSky) this.c).updating.getUpdatingOptimized(j2); // Paper
}
if (nibblearray1 != null) {
if (i2 != j) {
- int k2 = this.b(i2, i, this.a(nibblearray1, i2));
+ int k2 = this.b(i2, i, this.getNibbleLightInverse(nibblearray1, newX, newY, newZ)); // Paper
if (l > k2) {
l = k2;
@@ -206,7 +225,7 @@ public final class LightEngineSky extends LightEngineLayer<LightEngineStorageSky
j2 = SectionPosition.a(j2, EnumDirection.UP);
}
- NibbleArray nibblearray2 = ((LightEngineStorageSky) this.c).a(j2, true);
+ NibbleArray nibblearray2 = this.c.updating.getUpdatingOptimized(j2); // Paper
if (i2 != j) {
int l2;
diff --git a/src/main/java/net/minecraft/server/LightEngineStorage.java b/src/main/java/net/minecraft/server/LightEngineStorage.java
index b8b06c790adfa0246b1a6fb5eab1f63bf5ef8b0b..b98e60772bad7e06845b50fdc11e98c0ea775d3d 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorage.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorage.java
@@ -20,9 +20,9 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
protected final LongSet c = new LongOpenHashSet();
protected final LongSet d = new LongOpenHashSet();
protected volatile M e_visible; protected final Object visibleUpdateLock = new Object(); // Paper - diff on change, should be "visible" - force compile fail on usage change
- protected final M f; // Paper - diff on change, should be "updating"
+ protected final M f; protected final M updating; // Paper - diff on change, should be "updating"
protected final LongSet g = new LongOpenHashSet();
- protected final LongSet h = new LongOpenHashSet();
+ protected final LongSet h = new LongOpenHashSet(); LongSet dirty = h; // Paper - OBFHELPER
protected final Long2ObjectMap<NibbleArray> i = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap());
private final LongSet n = new LongOpenHashSet();
private final LongSet o = new LongOpenHashSet();
@@ -30,33 +30,33 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
protected volatile boolean j;
protected LightEngineStorage(EnumSkyBlock enumskyblock, ILightAccess ilightaccess, M m0) {
- super(3, 16, 256);
+ super(3, 256, 256); // Paper - bump expected size of level sets to improve collisions and reduce rehashing (seen a lot of it)
this.l = enumskyblock;
this.m = ilightaccess;
- this.f = m0;
+ this.f = m0; updating = m0; // Paper
this.e_visible = m0.b(); // Paper - avoid copying light data
this.e_visible.d(); // Paper - avoid copying light data
}
- protected boolean g(long i) {
- return this.a(i, true) != null;
+ protected final boolean g(long i) { // Paper - final to help inlining
+ return this.updating.getUpdatingOptimized(i) != null; // Paper - inline to avoid branching
}
@Nullable
protected NibbleArray a(long i, boolean flag) {
// Paper start - avoid copying light data
if (flag) {
- return this.a(this.f, i);
+ return this.updating.getUpdatingOptimized(i);
} else {
synchronized (this.visibleUpdateLock) {
- return this.a(this.e_visible, i);
+ return this.e_visible.lookup.apply(i);
}
}
// Paper end - avoid copying light data
}
@Nullable
- protected NibbleArray a(M m0, long i) {
+ protected final NibbleArray a(M m0, long i) { // Paper
return m0.c(i);
}
@@ -70,27 +70,57 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
protected abstract int d(long i);
protected int i(long i) {
- long j = SectionPosition.e(i);
- NibbleArray nibblearray = this.a(j, true);
+ // Paper start - reuse and inline math, use Optimized Updating path
+ final int x = (int) (i >> 38);
+ final int y = (int) ((i << 52) >> 52);
+ final int z = (int) ((i << 26) >> 38);
+ long j = SectionPosition.blockPosAsSectionLong(x, y, z);
+ NibbleArray nibblearray = this.updating.getUpdatingOptimized(j);
+ // BUG: Sometimes returns null and crashes, try to recover, but to prevent crash just return no light.
+ if (nibblearray == null) {
+ nibblearray = this.e_visible.lookup.apply(j);
+ }
+ if (nibblearray == null) {
+ System.err.println("Null nibble, preventing crash " + BlockPosition.fromLong(i));
+ return 0;
+ }
- return nibblearray.a(SectionPosition.b(BlockPosition.b(i)), SectionPosition.b(BlockPosition.c(i)), SectionPosition.b(BlockPosition.d(i)));
+ return nibblearray.a(x & 15, y & 15, z & 15); // Paper - inline operations
+ // Paper end
}
protected void b(long i, int j) {
- long k = SectionPosition.e(i);
+ // Paper start - cache part of the math done in loop below
+ int x = (int) (i >> 38);
+ int y = (int) ((i << 52) >> 52);
+ int z = (int) ((i << 26) >> 38);
+ long k = SectionPosition.blockPosAsSectionLong(x, y, z);
+ // Paper end
if (this.g.add(k)) {
this.f.a(k);
}
NibbleArray nibblearray = this.a(k, true);
-
- nibblearray.a(SectionPosition.b(BlockPosition.b(i)), SectionPosition.b(BlockPosition.c(i)), SectionPosition.b(BlockPosition.d(i)), j);
-
- for (int l = -1; l <= 1; ++l) {
- for (int i1 = -1; i1 <= 1; ++i1) {
- for (int j1 = -1; j1 <= 1; ++j1) {
- this.h.add(SectionPosition.e(BlockPosition.a(i, i1, j1, l)));
+ nibblearray.a(x & 15, y & 15, z & 15, j); // Paper - use already calculated x/y/z
+
+ // Paper start - credit to JellySquid for a major optimization here:
+ /*
+ * An extremely important optimization is made here in regards to adding items to the pending notification set. The
+ * original implementation attempts to add the coordinate of every chunk which contains a neighboring block position
+ * even though a huge number of loop iterations will simply map to block positions within the same updating chunk.
+ *
+ * Our implementation here avoids this by pre-calculating the min/max chunk coordinates so we can iterate over only
+ * the relevant chunk positions once. This reduces what would always be 27 iterations to just 1-8 iterations.
+ *
+ * @reason Use faster implementation
+ * @author JellySquid
+ */
+ for (int z2 = (z - 1) >> 4; z2 <= (z + 1) >> 4; ++z2) {
+ for (int x2 = (x - 1) >> 4; x2 <= (x + 1) >> 4; ++x2) {
+ for (int y2 = (y - 1) >> 4; y2 <= (y + 1) >> 4; ++y2) {
+ this.dirty.add(SectionPosition.asLong(x2, y2, z2));
+ // Paper end
}
}
}
@@ -122,17 +152,23 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
}
if (k >= 2 && j != 2) {
- if (this.p.contains(i)) {
- this.p.remove(i);
- } else {
+ if (!this.p.remove(i)) { // Paper - remove useless contains - credit to JellySquid
+ //this.p.remove(i); // Paper
+ //} else { // Paper
this.f.a(i, this.j(i));
this.g.add(i);
this.k(i);
- for (int l = -1; l <= 1; ++l) {
- for (int i1 = -1; i1 <= 1; ++i1) {
- for (int j1 = -1; j1 <= 1; ++j1) {
- this.h.add(SectionPosition.e(BlockPosition.a(i, i1, j1, l)));
+ // Paper start - reuse x/y/z and only notify valid chunks - Credit to JellySquid (See above method for notes)
+ int x = (int) (i >> 38);
+ int y = (int) ((i << 52) >> 52);
+ int z = (int) ((i << 26) >> 38);
+
+ for (int z2 = (z - 1) >> 4; z2 <= (z + 1) >> 4; ++z2) {
+ for (int x2 = (x - 1) >> 4; x2 <= (x + 1) >> 4; ++x2) {
+ for (int y2 = (y - 1) >> 4; y2 <= (y + 1) >> 4; ++y2) {
+ this.dirty.add(SectionPosition.asLong(x2, y2, z2));
+ // Paper end
}
}
}
@@ -158,9 +194,9 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
return SectionPosition.e(j) == i;
});
} else {
- int j = SectionPosition.c(SectionPosition.b(i));
- int k = SectionPosition.c(SectionPosition.c(i));
- int l = SectionPosition.c(SectionPosition.d(i));
+ int j = (int) (i >> 42) << 4; // Paper - inline
+ int k = (int) (i << 44 >> 44) << 4; // Paper - inline
+ int l = (int) (i << 22 >> 42) << 4; // Paper - inline
for (int i1 = 0; i1 < 16; ++i1) {
for (int j1 = 0; j1 < 16; ++j1) {
@@ -187,7 +223,7 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
NibbleArray nibblearray;
while (longiterator.hasNext()) {
- i = (Long) longiterator.next();
+ i = longiterator.nextLong(); // Paper
this.a(lightenginelayer, i);
NibbleArray nibblearray1 = (NibbleArray) this.i.remove(i);
@@ -205,7 +241,7 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
longiterator = this.p.iterator();
while (longiterator.hasNext()) {
- i = (Long) longiterator.next();
+ i = longiterator.nextLong(); // Paper
this.l(i);
}
@@ -216,12 +252,13 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
Entry entry;
long j;
+ NibbleArray test = null; // Paper
while (objectiterator.hasNext()) {
entry = (Entry) objectiterator.next();
j = entry.getLongKey();
- if (this.g(j)) {
+ if ((test = this.updating.getUpdatingOptimized(j)) != null) { // Paper - dont look up nibble twice
nibblearray = (NibbleArray) entry.getValue();
- if (this.f.c(j) != nibblearray) {
+ if (test != nibblearray) { // Paper
this.a(lightenginelayer, j);
this.f.a(j, nibblearray);
this.g.add(j);
@@ -234,14 +271,14 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
longiterator = this.i.keySet().iterator();
while (longiterator.hasNext()) {
- i = (Long) longiterator.next();
+ i = longiterator.nextLong(); // Paper
this.b(lightenginelayer, i);
}
} else {
longiterator = this.n.iterator();
while (longiterator.hasNext()) {
- i = (Long) longiterator.next();
+ i = longiterator.nextLong(); // Paper
this.b(lightenginelayer, i);
}
}
@@ -262,15 +299,20 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
private void b(LightEngineLayer<M, ?> lightenginelayer, long i) {
if (this.g(i)) {
- int j = SectionPosition.c(SectionPosition.b(i));
- int k = SectionPosition.c(SectionPosition.c(i));
- int l = SectionPosition.c(SectionPosition.d(i));
+ // Paper start
+ int secX = (int) (i >> 42);
+ int secY = (int) (i << 44 >> 44);
+ int secZ = (int) (i << 22 >> 42);
+ int j = secX << 4; // baseX
+ int k = secY << 4; // baseY
+ int l = secZ << 4; // baseZ
+ // Paper end
EnumDirection[] aenumdirection = LightEngineStorage.k;
int i1 = aenumdirection.length;
for (int j1 = 0; j1 < i1; ++j1) {
EnumDirection enumdirection = aenumdirection[j1];
- long k1 = SectionPosition.a(i, enumdirection);
+ long k1 = SectionPosition.getAdjacentFromSectionPos(secX, secY, secZ, enumdirection); // Paper - avoid extra unpacking
if (!this.i.containsKey(k1) && this.g(k1)) {
for (int l1 = 0; l1 < 16; ++l1) {
diff --git a/src/main/java/net/minecraft/server/LightEngineStorageArray.java b/src/main/java/net/minecraft/server/LightEngineStorageArray.java
index 37c44a89f28c44915fcae5a7e2c4797b1c123723..549c2551c2b59730bf53a80f8706d388d96ad932 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorageArray.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorageArray.java
@@ -5,13 +5,18 @@ import javax.annotation.Nullable;
public abstract class LightEngineStorageArray<M extends LightEngineStorageArray<M>> {
- private final long[] b = new long[2];
- private final NibbleArray[] c = new NibbleArray[2];
+ // private final long[] b = new long[2]; // Paper - unused
+ private final NibbleArray[] c = new NibbleArray[]{NibbleArray.EMPTY_NIBBLE_ARRAY, NibbleArray.EMPTY_NIBBLE_ARRAY}; private final NibbleArray[] cache = c; // Paper - OBFHELPER
private boolean d;
protected final com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object<NibbleArray> data; // Paper - avoid copying light data
protected final boolean isVisible; // Paper - avoid copying light data
- java.util.function.Function<Long, NibbleArray> lookup; // Paper - faster branchless lookup
+ // Paper start - faster lookups with less branching, use interface to avoid boxing instead of Function
+ public final NibbleArrayAccess lookup;
+ public interface NibbleArrayAccess {
+ NibbleArray apply(long id);
+ }
+ // Paper end
// Paper start - avoid copying light data
protected LightEngineStorageArray(com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object<NibbleArray> data, boolean isVisible) {
if (isVisible) {
@@ -19,12 +24,14 @@ public abstract class LightEngineStorageArray<M extends LightEngineStorageArray<
}
this.data = data;
this.isVisible = isVisible;
+ // Paper end - avoid copying light data
+ // Paper start - faster lookups with less branching
if (isVisible) {
lookup = data::getVisibleAsync;
} else {
- lookup = data::getUpdating;
+ lookup = data.getUpdatingMap()::get; // jump straight the sub map
}
- // Paper end - avoid copying light data
+ // Paper end
this.c();
this.d = true;
}
@@ -34,7 +41,9 @@ public abstract class LightEngineStorageArray<M extends LightEngineStorageArray<
public void a(long i) {
if (this.isVisible) { throw new IllegalStateException("writing to visible data"); } // Paper - avoid copying light data
NibbleArray updating = this.data.getUpdating(i); // Paper - pool nibbles
- this.data.queueUpdate(i, new NibbleArray().markPoolSafe(updating.getCloneIfSet())); // Paper - avoid copying light data - pool safe clone
+ NibbleArray nibblearray = new NibbleArray().markPoolSafe(updating.getCloneIfSet()); // Paper
+ nibblearray.lightCacheKey = i; // Paper
+ this.data.queueUpdate(i, nibblearray); // Paper - avoid copying light data - pool safe clone
if (updating.cleaner != null) MCUtil.scheduleTask(2, updating.cleaner, "Light Engine Release"); // Paper - delay clean incase anything holding ref was still using it
this.c();
}
@@ -43,34 +52,34 @@ public abstract class LightEngineStorageArray<M extends LightEngineStorageArray<
return lookup.apply(i) != null; // Paper - avoid copying light data
}
- @Nullable
- public final NibbleArray c(long i) { // Paper - final
- if (this.d) {
- for (int j = 0; j < 2; ++j) {
- if (i == this.b[j]) {
- return this.c[j];
- }
- }
- }
-
- NibbleArray nibblearray = lookup.apply(i); // Paper - avoid copying light data
+ // Paper start - less branching as we know we are using cache and updating
+ public final NibbleArray getUpdatingOptimized(final long i) { // Paper - final
+ final NibbleArray[] cache = this.cache;
+ if (cache[0].lightCacheKey == i) return cache[0];
+ if (cache[1].lightCacheKey == i) return cache[1];
+ final NibbleArray nibblearray = this.lookup.apply(i); // Paper - avoid copying light data
if (nibblearray == null) {
return null;
} else {
- if (this.d) {
- for (int k = 1; k > 0; --k) {
- this.b[k] = this.b[k - 1];
- this.c[k] = this.c[k - 1];
- }
-
- this.b[0] = i;
- this.c[0] = nibblearray;
- }
-
+ cache[1] = cache[0];
+ cache[0] = nibblearray;
return nibblearray;
}
}
+ // Paper end
+
+ @Nullable
+ public final NibbleArray c(final long i) { // Paper - final
+ // Paper start - optimize visible case or missed updating cases
+ if (this.d) {
+ // short circuit to optimized
+ return getUpdatingOptimized(i);
+ }
+
+ return this.lookup.apply(i);
+ // Paper end
+ }
@Nullable
public NibbleArray d(long i) {
@@ -80,13 +89,14 @@ public abstract class LightEngineStorageArray<M extends LightEngineStorageArray<
public void a(long i, NibbleArray nibblearray) {
if (this.isVisible) { throw new IllegalStateException("writing to visible data"); } // Paper - avoid copying light data
+ nibblearray.lightCacheKey = i; // Paper
this.data.queueUpdate(i, nibblearray); // Paper - avoid copying light data
}
public void c() {
for (int i = 0; i < 2; ++i) {
- this.b[i] = Long.MAX_VALUE;
- this.c[i] = null;
+ // this.b[i] = Long.MAX_VALUE; // Paper - Unused
+ this.c[i] = NibbleArray.EMPTY_NIBBLE_ARRAY; // Paper
}
}
diff --git a/src/main/java/net/minecraft/server/LightEngineStorageBlock.java b/src/main/java/net/minecraft/server/LightEngineStorageBlock.java
index 292d8c742d3be41ba8ad7fb7f1251dc7f790b62b..5b7b7506f5d1a7578fb54a578891324dfcec3f03 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorageBlock.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorageBlock.java
@@ -10,10 +10,14 @@ public class LightEngineStorageBlock extends LightEngineStorage<LightEngineStora
@Override
protected int d(long i) {
- long j = SectionPosition.e(i);
- NibbleArray nibblearray = this.a(j, false);
-
- return nibblearray == null ? 0 : nibblearray.a(SectionPosition.b(BlockPosition.b(i)), SectionPosition.b(BlockPosition.c(i)), SectionPosition.b(BlockPosition.d(i)));
+ // Paper start
+ int baseX = (int) (i >> 38);
+ int baseY = (int) ((i << 52) >> 52);
+ int baseZ = (int) ((i << 26) >> 38);
+ long j = (((long) (baseX >> 4) & 4194303L) << 42) | (((long) (baseY >> 4) & 1048575L)) | (((long) (baseZ >> 4) & 4194303L) << 20);
+ NibbleArray nibblearray = this.e_visible.lookup.apply(j);
+ return nibblearray == null ? 0 : nibblearray.a(baseX & 15, baseY & 15, baseZ & 15);
+ // Paper end
}
public static final class a extends LightEngineStorageArray<LightEngineStorageBlock.a> {
diff --git a/src/main/java/net/minecraft/server/LightEngineStorageSky.java b/src/main/java/net/minecraft/server/LightEngineStorageSky.java
index a35e7b392c74fadf2760d1fc2021e98d33858cb5..944094e8e770cc8c0205ef2aa6c48fff55d74639 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorageSky.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorageSky.java
@@ -22,7 +22,12 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
@Override
protected int d(long i) {
- long j = SectionPosition.e(i);
+ // Paper start
+ int baseX = (int) (i >> 38);
+ int baseY = (int) ((i << 52) >> 52);
+ int baseZ = (int) ((i << 26) >> 38);
+ long j = SectionPosition.blockPosAsSectionLong(baseX, baseY, baseZ);
+ // Paper end
int k = SectionPosition.c(j);
synchronized (this.visibleUpdateLock) { // Paper - avoid copying light data
LightEngineStorageSky.a lightenginestoragesky_a = (LightEngineStorageSky.a) this.e_visible; // Paper - avoid copying light data - must be after lock acquire
@@ -43,7 +48,7 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
}
}
- return nibblearray.a(SectionPosition.b(BlockPosition.b(i)), SectionPosition.b(BlockPosition.c(i)), SectionPosition.b(BlockPosition.d(i)));
+ return nibblearray.a(baseX & 15, (int) ((i << 52) >> 52) & 15, (int) baseZ & 15); // Paper - y changed above
} else {
return 15;
}
@@ -162,7 +167,7 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
if (k != ((LightEngineStorageSky.a) this.f).b && SectionPosition.c(j) < k) {
NibbleArray nibblearray1;
- while ((nibblearray1 = this.a(j, true)) == null) {
+ while ((nibblearray1 = this.updating.getUpdatingOptimized(j)) == null) { // Paper
j = SectionPosition.a(j, EnumDirection.UP);
}
@@ -186,7 +191,10 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
longiterator = this.m.iterator();
while (longiterator.hasNext()) {
- i = (Long) longiterator.next();
+ i = longiterator.nextLong(); // Paper
+ int baseX = (int) (i >> 42) << 4; // Paper
+ int baseY = (int) (i << 44 >> 44) << 4; // Paper
+ int baseZ = (int) (i << 22 >> 42) << 4; // Paper
j = this.c(i);
if (j != 2 && !this.n.contains(i) && this.l.add(i)) {
int l;
@@ -197,10 +205,10 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
((LightEngineStorageSky.a) this.f).a(i);
}
- Arrays.fill(this.a(i, true).asBytesPoolSafe(), (byte) -1); // Paper
- k = SectionPosition.c(SectionPosition.b(i));
- l = SectionPosition.c(SectionPosition.c(i));
- int i1 = SectionPosition.c(SectionPosition.d(i));
+ Arrays.fill(this.updating.getUpdatingOptimized(i).asBytesPoolSafe(), (byte) -1); // Paper - use optimized
+ k = baseX; // Paper
+ l = baseY; // Paper
+ int i1 = baseZ; // Paper
EnumDirection[] aenumdirection = LightEngineStorageSky.k;
int j1 = aenumdirection.length;
@@ -209,7 +217,7 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
for (int l1 = 0; l1 < j1; ++l1) {
EnumDirection enumdirection = aenumdirection[l1];
- k1 = SectionPosition.a(i, enumdirection);
+ k1 = SectionPosition.getAdjacentFromBlockPos(baseX, baseY, baseZ, enumdirection); // Paper
if ((this.n.contains(k1) || !this.l.contains(k1) && !this.m.contains(k1)) && this.g(k1)) {
for (int i2 = 0; i2 < 16; ++i2) {
for (int j2 = 0; j2 < 16; ++j2) {
@@ -242,16 +250,16 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
for (int i3 = 0; i3 < 16; ++i3) {
for (j1 = 0; j1 < 16; ++j1) {
- long j3 = BlockPosition.a(SectionPosition.c(SectionPosition.b(i)) + i3, SectionPosition.c(SectionPosition.c(i)), SectionPosition.c(SectionPosition.d(i)) + j1);
+ long j3 = BlockPosition.a(baseX + i3, baseY, baseZ + j1); // Paper
- k1 = BlockPosition.a(SectionPosition.c(SectionPosition.b(i)) + i3, SectionPosition.c(SectionPosition.c(i)) - 1, SectionPosition.c(SectionPosition.d(i)) + j1);
+ k1 = BlockPosition.a(baseX + i3, baseY - 1, baseZ + j1); // Paper
lightenginelayer.a(j3, k1, lightenginelayer.b(j3, k1, 0), true);
}
}
} else {
for (k = 0; k < 16; ++k) {
for (l = 0; l < 16; ++l) {
- long k3 = BlockPosition.a(SectionPosition.c(SectionPosition.b(i)) + k, SectionPosition.c(SectionPosition.c(i)) + 16 - 1, SectionPosition.c(SectionPosition.d(i)) + l);
+ long k3 = BlockPosition.a(baseX + k, baseY + 16 - 1, baseZ + l); // Paper
lightenginelayer.a(Long.MAX_VALUE, k3, 0, true);
}
@@ -266,11 +274,14 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
longiterator = this.n.iterator();
while (longiterator.hasNext()) {
- i = (Long) longiterator.next();
+ i = longiterator.nextLong(); // Paper
+ int baseX = (int) (i >> 42) << 4; // Paper
+ int baseY = (int) (i << 44 >> 44) << 4; // Paper
+ int baseZ = (int) (i << 22 >> 42) << 4; // Paper
if (this.l.remove(i) && this.g(i)) {
for (j = 0; j < 16; ++j) {
for (k = 0; k < 16; ++k) {
- long l3 = BlockPosition.a(SectionPosition.c(SectionPosition.b(i)) + j, SectionPosition.c(SectionPosition.c(i)) + 16 - 1, SectionPosition.c(SectionPosition.d(i)) + k);
+ long l3 = BlockPosition.a(baseX + j, baseY + 16 - 1, baseZ + k); // Paper
lightenginelayer.a(Long.MAX_VALUE, l3, 15, false);
}
diff --git a/src/main/java/net/minecraft/server/LightEngineThreaded.java b/src/main/java/net/minecraft/server/LightEngineThreaded.java
index a9dc8466278f9ec2becbcb643e6e1c973df72b82..97c41fa1306798dad08ad9797c61f13d0dff6f52 100644
--- a/src/main/java/net/minecraft/server/LightEngineThreaded.java
+++ b/src/main/java/net/minecraft/server/LightEngineThreaded.java
@@ -15,15 +15,119 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
private static final Logger LOGGER = LogManager.getLogger();
private final ThreadedMailbox<Runnable> b;
- private final ObjectList<Pair<LightEngineThreaded.Update, Runnable>> c = new ObjectArrayList();
- private final PlayerChunkMap d;
+ // Paper start
+ private static final int MAX_PRIORITIES = PlayerChunkMap.GOLDEN_TICKET + 2;
+ private final java.util.concurrent.ConcurrentLinkedQueue<Runnable> priorityChanges = new java.util.concurrent.ConcurrentLinkedQueue<>();
+
+ public void changePriority(long pair, int currentPriority, int priority) {
+ this.priorityChanges.add(() -> {
+ ChunkLightQueue remove = this.queue.buckets[currentPriority].remove(pair);
+ if (remove != null) {
+ ChunkLightQueue existing = this.queue.buckets[priority].put(pair, remove);
+ if (existing != null) {
+ remove.pre.addAll(existing.pre);
+ remove.post.addAll(existing.post);
+ }
+ }
+ });
+ }
+
+ private boolean isChunkLightStatus(long pair) {
+ PlayerChunk playerChunk = playerChunkMap.getUpdatingChunk(pair);
+ if (playerChunk == null) {
+ return false;
+ }
+ ChunkStatus status = PlayerChunk.getChunkStatus(playerChunk.getTicketLevel());
+ return status != null && status.isAtLeastStatus(ChunkStatus.LIGHT);
+ }
+
+ static class ChunkLightQueue {
+ public boolean shouldFastUpdate;
+ java.util.ArrayDeque<Runnable> pre = new java.util.ArrayDeque<Runnable>();
+ java.util.ArrayDeque<Runnable> post = new java.util.ArrayDeque<Runnable>();
+
+ ChunkLightQueue(long chunk) {}
+ }
+
+
+ // Retain the chunks priority level for queued light tasks
+ private class LightQueue {
+ private int size = 0;
+ private int lowestPriority = MAX_PRIORITIES;
+ private final it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap<ChunkLightQueue>[] buckets = new it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap[MAX_PRIORITIES];
+
+ private LightQueue() {
+ for (int i = 0; i < buckets.length; i++) {
+ buckets[i] = new it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap<>();
+ }
+ }
+
+ public final void add(long chunkId, int priority, LightEngineThreaded.Update type, Runnable run) {
+ add(chunkId, priority, type, run, false);
+ }
+ public final void add(long chunkId, int priority, LightEngineThreaded.Update type, Runnable run, boolean shouldFastUpdate) {
+ ChunkLightQueue lightQueue = this.buckets[priority].computeIfAbsent(chunkId, ChunkLightQueue::new);
+ this.size++;
+ if (type == Update.PRE_UPDATE) {
+ lightQueue.pre.add(run);
+ } else {
+ lightQueue.post.add(run);
+ }
+ if (shouldFastUpdate) {
+ lightQueue.shouldFastUpdate = true;
+ }
+
+ if (this.lowestPriority > priority) {
+ this.lowestPriority = priority;
+ }
+ }
+
+ public final boolean isEmpty() {
+ return this.size == 0;
+ }
+
+ public final int size() {
+ return this.size;
+ }
+
+ public boolean poll(java.util.List<Runnable> pre, java.util.List<Runnable> post) {
+ Runnable run;
+ while ((run = priorityChanges.poll()) != null) {
+ run.run();
+ }
+ boolean hasWork = false;
+ it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap<ChunkLightQueue>[] buckets = this.buckets;
+ while (lowestPriority < MAX_PRIORITIES && !isEmpty()) {
+ it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap<ChunkLightQueue> bucket = buckets[lowestPriority];
+ if (bucket.isEmpty()) {
+ lowestPriority++;
+ continue;
+ }
+ ChunkLightQueue queue = bucket.removeFirst();
+ this.size -= queue.pre.size() + queue.post.size();
+ pre.addAll(queue.pre);
+ post.addAll(queue.post);
+ queue.pre.clear();
+ queue.post.clear();
+ hasWork = true;
+ if (queue.shouldFastUpdate) {
+ return true;
+ }
+ }
+ return hasWork;
+ }
+ }
+
+ private final LightQueue queue = new LightQueue();
+ // Paper end
+ private final PlayerChunkMap d; private final PlayerChunkMap playerChunkMap; // Paper
private final Mailbox<ChunkTaskQueueSorter.a<Runnable>> e;
private volatile int f = 5;
private final AtomicBoolean g = new AtomicBoolean();
public LightEngineThreaded(ILightAccess ilightaccess, PlayerChunkMap playerchunkmap, boolean flag, ThreadedMailbox<Runnable> threadedmailbox, Mailbox<ChunkTaskQueueSorter.a<Runnable>> mailbox) {
super(ilightaccess, true, flag);
- this.d = playerchunkmap;
+ this.d = playerchunkmap; this.playerChunkMap = d; // Paper
this.e = mailbox;
this.b = threadedmailbox;
}
@@ -111,10 +215,10 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
private void a(int i, int j, IntSupplier intsupplier, LightEngineThreaded.Update lightenginethreaded_update, Runnable runnable) {
this.e.a(ChunkTaskQueueSorter.a(() -> {
- this.c.add(Pair.of(lightenginethreaded_update, runnable));
- if (this.c.size() >= this.f) {
- this.b();
- }
+ // Paper start
+ int priority = intsupplier.getAsInt();
+ this.queue.add(ChunkCoordIntPair.pair(i, j), priority, lightenginethreaded_update, runnable);
+ // Paper end
}, ChunkCoordIntPair.pair(i, j), intsupplier));
}
@@ -133,8 +237,27 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
public CompletableFuture<IChunkAccess> a(IChunkAccess ichunkaccess, boolean flag) {
ChunkCoordIntPair chunkcoordintpair = ichunkaccess.getPos();
- ichunkaccess.b(false);
- this.a(chunkcoordintpair.x, chunkcoordintpair.z, LightEngineThreaded.Update.PRE_UPDATE, SystemUtils.a(() -> {
+ // Paper start
+ //ichunkaccess.b(false); // Don't need to disable this
+ long pair = chunkcoordintpair.pair();
+ CompletableFuture<IChunkAccess> future = new CompletableFuture<>();
+ IntSupplier prioritySupplier = playerChunkMap.getPrioritySupplier(pair);
+ this.e.a(ChunkTaskQueueSorter.a(() -> {
+ // Chunk's no longer needed
+ if (!isChunkLightStatus(pair)) {
+ this.d.c(chunkcoordintpair); // copied from end of method to release light ticket
+ future.complete(ichunkaccess);
+ return;
+ }
+ boolean[] skippedPre = {false};
+ this.queue.add(pair, prioritySupplier.getAsInt(), LightEngineThreaded.Update.PRE_UPDATE, SystemUtils.a(() -> {
+ if (!isChunkLightStatus(pair)) {
+ this.d.c(chunkcoordintpair); // copied from end of method to release light ticket
+ future.complete(ichunkaccess);
+ skippedPre[0] = true;
+ return;
+ }
+ // Paper end
ChunkSection[] achunksection = ichunkaccess.getSections();
for (int i = 0; i < 16; ++i) {
@@ -152,55 +275,55 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
});
}
- this.d.c(chunkcoordintpair);
+ this.d.c(chunkcoordintpair); // Paper - if change, copy into !isChunkLightStatus above
}, () -> {
return "lightChunk " + chunkcoordintpair + " " + flag;
+ // Paper start - merge the 2 together
}));
- return CompletableFuture.supplyAsync(() -> {
+
+ this.queue.add(pair, prioritySupplier.getAsInt(), LightEngineThreaded.Update.POST_UPDATE, () -> {
+ if (skippedPre[0]) return; // Paper - future's already complete
ichunkaccess.b(true);
super.b(chunkcoordintpair, false);
- return ichunkaccess;
- }, (runnable) -> {
- this.a(chunkcoordintpair.x, chunkcoordintpair.z, LightEngineThreaded.Update.POST_UPDATE, runnable);
+ // Paper start
+ future.complete(ichunkaccess);
});
+ queueUpdate(); // run queue now
+ }, pair, prioritySupplier));
+ return future;
+ // Paper end
}
public void queueUpdate() {
- if ((!this.c.isEmpty() || super.a()) && this.g.compareAndSet(false, true)) {
+ if ((!this.queue.isEmpty() || super.a()) && this.g.compareAndSet(false, true)) { // Paper
this.b.a((() -> { // Paper - decompile error
this.b();
this.g.set(false);
+ queueUpdate(); // Paper - if we still have work to do, do it!
}));
}
}
+ // Paper start - replace impl
+ private final java.util.List<Runnable> pre = new java.util.ArrayList<>();
+ private final java.util.List<Runnable> post = new java.util.ArrayList<>();
private void b() {
- int i = Math.min(this.c.size(), this.f);
- ObjectListIterator<Pair<LightEngineThreaded.Update, Runnable>> objectlistiterator = this.c.iterator();
-
- Pair pair;
- int j;
-
- for (j = 0; objectlistiterator.hasNext() && j < i; ++j) {
- pair = (Pair) objectlistiterator.next();
- if (pair.getFirst() == LightEngineThreaded.Update.PRE_UPDATE) {
- ((Runnable) pair.getSecond()).run();
- }
+ int i = Math.min(queue.size(), 4);
+ boolean ran = false;
+ while (i-- > 0 && queue.poll(pre, post)) {
+ pre.forEach(Runnable::run);
+ pre.clear();
+ super.a(Integer.MAX_VALUE, true, true);
+ post.forEach(Runnable::run);
+ post.clear();
+ ran = true;
}
-
- objectlistiterator.back(j);
- super.a(Integer.MAX_VALUE, true, true);
-
- for (j = 0; objectlistiterator.hasNext() && j < i; ++j) {
- pair = (Pair) objectlistiterator.next();
- if (pair.getFirst() == LightEngineThreaded.Update.POST_UPDATE) {
- ((Runnable) pair.getSecond()).run();
- }
-
- objectlistiterator.remove();
+ if (!ran) {
+ // might have level updates to go still
+ super.a(Integer.MAX_VALUE, true, true);
}
-
+ // Paper end
}
public void a(int i) {
diff --git a/src/main/java/net/minecraft/server/NibbleArray.java b/src/main/java/net/minecraft/server/NibbleArray.java
index 8cedfdd820cc02a76607b53e0b054fc74654f907..a9795394c9b17f9f0ce4c4f9c8f51a48e950418e 100644
--- a/src/main/java/net/minecraft/server/NibbleArray.java
+++ b/src/main/java/net/minecraft/server/NibbleArray.java
@@ -8,6 +8,13 @@ import javax.annotation.Nullable;
public class NibbleArray {
// Paper start
+ static final NibbleArray EMPTY_NIBBLE_ARRAY = new NibbleArray() {
+ @Override
+ public byte[] asBytes() {
+ throw new IllegalStateException();
+ }
+ };
+ long lightCacheKey = Long.MIN_VALUE;
public static byte[] EMPTY_NIBBLE = new byte[2048];
private static final int nibbleBucketSizeMultiplier = Integer.getInteger("Paper.nibbleBucketSize", 3072);
private static final int maxPoolSize = Integer.getInteger("Paper.maxNibblePoolSize", (int) Math.min(6, Math.max(1, Runtime.getRuntime().maxMemory() / 1024 / 1024 / 1024)) * (nibbleBucketSizeMultiplier * 8));
diff --git a/src/main/java/net/minecraft/server/PlayerChunk.java b/src/main/java/net/minecraft/server/PlayerChunk.java
index 446c401b3139f8c6c0e70d883340f0140d94b752..a3bce8f13bf278af2d6870891daa9bf692b4e267 100644
--- a/src/main/java/net/minecraft/server/PlayerChunk.java
+++ b/src/main/java/net/minecraft/server/PlayerChunk.java
@@ -716,6 +716,7 @@ public class PlayerChunk {
ioPriority = com.destroystokyo.paper.io.PrioritizedTaskQueue.HIGH_PRIORITY;
}
chunkMap.world.asyncChunkTaskManager.raisePriority(location.x, location.z, ioPriority);
+ chunkMap.world.getChunkProvider().getLightEngine().changePriority(location.pair(), getCurrentPriority(), priority);
}
if (getCurrentPriority() != priority) {
this.v.a(this.location, this::getCurrentPriority, priority, this::setPriority); // use preferred priority
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index fea219dcfd5a98fc0e48fd70dc7d0fd41b2fc970..ad8a00e0fb5da5df1eb3afbf9ea50acfc89a5ff1 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -653,6 +653,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
// Paper end
}
+ protected final IntSupplier getPrioritySupplier(long i) { return c(i); } // Paper - OBFHELPER
protected IntSupplier c(long i) {
return () -> {
PlayerChunk playerchunk = this.getVisibleChunk(i);
diff --git a/src/main/java/net/minecraft/server/ThreadedMailbox.java b/src/main/java/net/minecraft/server/ThreadedMailbox.java
index 35f4d2d9591e625ab0bbeab7b606761e74965eec..698d82dd736529a8cbfad5c6bed70ab97f0064bb 100644
--- a/src/main/java/net/minecraft/server/ThreadedMailbox.java
+++ b/src/main/java/net/minecraft/server/ThreadedMailbox.java
@@ -109,7 +109,8 @@ public class ThreadedMailbox<T> implements Mailbox<T>, AutoCloseable, Runnable {
}
- @Override
+
+ public void queue(T t0) { a(t0); } @Override // Paper - OBFHELPER
public void a(T t0) {
this.a.a(t0);
this.f();
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index 2b04577c153fc4563e59b67afee12411a5de5314..d048ee2313142ba4b0fae7ef2badb9f80614bb62 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -707,6 +707,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
}
gameprofilerfiller.exit();
timings.chunkTicksBlocks.stopTiming(); // Paper
+ getChunkProvider().getLightEngine().queueUpdate(); // Paper
// Paper end
}
}
|