Package: icu / 63.2-2

reverse_ICU-20250.patch Patch series | download
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
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
Description: reverse ICU 63 UnicodeSet startup performance regression fix
 The following changes break several applications:
    ICU-20250 make UnicodeSet(intprop=value) faster
    - fastpath for UnicodeSet.add(new last range)
    - fewer UnicodeSet memory allocations:
      initial internal list array, exponential array growth,
      allocate strings list/set only when first one is added
    - faster CodePointTrie.getRange(): fewer calls to filter function
    - revert UnicodeSet(intprop=value) from trie ranges to range starts + lookup
    - cache per-int-prop range starts: fewer lookups
Author: Laszlo Boszormenyi (GCS) <gcs@debian.org>
Last-Update: 2019-06-08

---

--- icu-63.2.orig/source/common/characterproperties.cpp
+++ icu-63.2/source/common/characterproperties.cpp
@@ -23,9 +23,6 @@
 #include "umutex.h"
 #include "uprops.h"
 
-using icu::LocalPointer;
-using icu::Normalizer2Factory;
-using icu::Normalizer2Impl;
 using icu::UInitOnce;
 using icu::UnicodeSet;
 
@@ -33,13 +30,11 @@ namespace {
 
 UBool U_CALLCONV characterproperties_cleanup();
 
-constexpr int32_t NUM_INCLUSIONS = UPROPS_SRC_COUNT + UCHAR_INT_LIMIT - UCHAR_INT_START;
-
 struct Inclusion {
     UnicodeSet  *fSet;
     UInitOnce    fInitOnce;
 };
-Inclusion gInclusions[NUM_INCLUSIONS]; // cached getInclusions()
+Inclusion gInclusions[UPROPS_SRC_COUNT]; // cached getInclusions()
 
 UnicodeSet *sets[UCHAR_BINARY_LIMIT] = {};
 
@@ -85,22 +80,35 @@ UBool U_CALLCONV characterproperties_cle
     return TRUE;
 }
 
-void U_CALLCONV initInclusion(UPropertySource src, UErrorCode &errorCode) {
+}  // namespace
+
+U_NAMESPACE_BEGIN
+
+/*
+Reduce excessive reallocation, and make it easier to detect initialization problems.
+Usually you don't see smaller sets than this for Unicode 5.0.
+*/
+constexpr int32_t DEFAULT_INCLUSION_CAPACITY = 3072;
+
+void U_CALLCONV CharacterProperties::initInclusion(UPropertySource src, UErrorCode &errorCode) {
     // This function is invoked only via umtx_initOnce().
+    // This function is a friend of class UnicodeSet.
+
     U_ASSERT(0 <= src && src < UPROPS_SRC_COUNT);
     if (src == UPROPS_SRC_NONE) {
         errorCode = U_INTERNAL_PROGRAM_ERROR;
         return;
     }
-    U_ASSERT(gInclusions[src].fSet == nullptr);
+    UnicodeSet * &incl = gInclusions[src].fSet;
+    U_ASSERT(incl == nullptr);
 
-    LocalPointer<UnicodeSet> incl(new UnicodeSet());
-    if (incl.isNull()) {
+    incl = new UnicodeSet();
+    if (incl == nullptr) {
         errorCode = U_MEMORY_ALLOCATION_ERROR;
         return;
     }
     USetAdder sa = {
-        (USet *)incl.getAlias(),
+        (USet *)incl,
         _set_add,
         _set_addRange,
         _set_addString,
@@ -108,6 +116,7 @@ void U_CALLCONV initInclusion(UPropertyS
         nullptr // don't need removeRange()
     };
 
+    incl->ensureCapacity(DEFAULT_INCLUSION_CAPACITY, errorCode);
     switch(src) {
     case UPROPS_SRC_CHAR:
         uchar_addPropertyStarts(&sa, &errorCode);
@@ -174,15 +183,12 @@ void U_CALLCONV initInclusion(UPropertyS
     }
 
     if (U_FAILURE(errorCode)) {
+        delete incl;
+        incl = nullptr;
         return;
     }
-    if (incl->isBogus()) {
-        errorCode = U_MEMORY_ALLOCATION_ERROR;
-        return;
-    }
-    // Compact for caching.
+    // Compact for caching
     incl->compact();
-    gInclusions[src].fSet = incl.orphan();
     ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
 }
 
@@ -193,66 +199,15 @@ const UnicodeSet *getInclusionsForSource
         return nullptr;
     }
     Inclusion &i = gInclusions[src];
-    umtx_initOnce(i.fInitOnce, &initInclusion, src, errorCode);
+    umtx_initOnce(i.fInitOnce, &CharacterProperties::initInclusion, src, errorCode);
     return i.fSet;
 }
 
-void U_CALLCONV initIntPropInclusion(UProperty prop, UErrorCode &errorCode) {
-    // This function is invoked only via umtx_initOnce().
-    U_ASSERT(UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT);
-    int32_t inclIndex = UPROPS_SRC_COUNT + prop - UCHAR_INT_START;
-    U_ASSERT(gInclusions[inclIndex].fSet == nullptr);
-    UPropertySource src = uprops_getSource(prop);
-    const UnicodeSet *incl = getInclusionsForSource(src, errorCode);
-    if (U_FAILURE(errorCode)) {
-        return;
-    }
-
-    LocalPointer<UnicodeSet> intPropIncl(new UnicodeSet(0, 0));
-    if (intPropIncl.isNull()) {
-        errorCode = U_MEMORY_ALLOCATION_ERROR;
-        return;
-    }
-    int32_t numRanges = incl->getRangeCount();
-    int32_t prevValue = 0;
-    for (int32_t i = 0; i < numRanges; ++i) {
-        UChar32 rangeEnd = incl->getRangeEnd(i);
-        for (UChar32 c = incl->getRangeStart(i); c <= rangeEnd; ++c) {
-            // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
-            int32_t value = u_getIntPropertyValue(c, prop);
-            if (value != prevValue) {
-                intPropIncl->add(c);
-                prevValue = value;
-            }
-        }
-    }
-
-    if (intPropIncl->isBogus()) {
-        errorCode = U_MEMORY_ALLOCATION_ERROR;
-        return;
-    }
-    // Compact for caching.
-    intPropIncl->compact();
-    gInclusions[inclIndex].fSet = intPropIncl.orphan();
-    ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
-}
-
-}  // namespace
-
-U_NAMESPACE_BEGIN
-
 const UnicodeSet *CharacterProperties::getInclusionsForProperty(
         UProperty prop, UErrorCode &errorCode) {
     if (U_FAILURE(errorCode)) { return nullptr; }
-    if (UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT) {
-        int32_t inclIndex = UPROPS_SRC_COUNT + prop - UCHAR_INT_START;
-        Inclusion &i = gInclusions[inclIndex];
-        umtx_initOnce(i.fInitOnce, &initIntPropInclusion, prop, errorCode);
-        return i.fSet;
-    } else {
-        UPropertySource src = uprops_getSource(prop);
-        return getInclusionsForSource(src, errorCode);
-    }
+    UPropertySource src = uprops_getSource(prop);
+    return getInclusionsForSource(src, errorCode);
 }
 
 U_NAMESPACE_END
@@ -261,7 +216,7 @@ namespace {
 
 UnicodeSet *makeSet(UProperty property, UErrorCode &errorCode) {
     if (U_FAILURE(errorCode)) { return nullptr; }
-    LocalPointer<UnicodeSet> set(new UnicodeSet());
+    icu::LocalPointer<UnicodeSet> set(new UnicodeSet());
     if (set.isNull()) {
         errorCode = U_MEMORY_ALLOCATION_ERROR;
         return nullptr;
--- icu-63.2.orig/source/common/ucptrie.cpp
+++ icu-63.2/source/common/ucptrie.cpp
@@ -280,7 +280,7 @@ UChar32 getRange(const void *t, UChar32
     int32_t prevI3Block = -1;
     int32_t prevBlock = -1;
     UChar32 c = start;
-    uint32_t trieValue, value;
+    uint32_t value;
     bool haveValue = false;
     do {
         int32_t i3Block;
@@ -319,7 +319,6 @@ UChar32 getRange(const void *t, UChar32
                         return c - 1;
                     }
                 } else {
-                    trieValue = trie->nullValue;
                     value = nullValue;
                     if (pValue != nullptr) { *pValue = nullValue; }
                     haveValue = true;
@@ -358,7 +357,6 @@ UChar32 getRange(const void *t, UChar32
                             return c - 1;
                         }
                     } else {
-                        trieValue = trie->nullValue;
                         value = nullValue;
                         if (pValue != nullptr) { *pValue = nullValue; }
                         haveValue = true;
@@ -366,32 +364,23 @@ UChar32 getRange(const void *t, UChar32
                     c = (c + dataBlockLength) & ~dataMask;
                 } else {
                     int32_t di = block + (c & dataMask);
-                    uint32_t trieValue2 = getValue(trie->data, valueWidth, di);
+                    uint32_t value2 = getValue(trie->data, valueWidth, di);
+                    value2 = maybeFilterValue(value2, trie->nullValue, nullValue,
+                                              filter, context);
                     if (haveValue) {
-                        if (trieValue2 != trieValue) {
-                            if (filter == nullptr ||
-                                    maybeFilterValue(trieValue2, trie->nullValue, nullValue,
-                                                     filter, context) != value) {
-                                return c - 1;
-                            }
-                            trieValue = trieValue2;  // may or may not help
+                        if (value2 != value) {
+                            return c - 1;
                         }
                     } else {
-                        trieValue = trieValue2;
-                        value = maybeFilterValue(trieValue2, trie->nullValue, nullValue,
-                                                 filter, context);
+                        value = value2;
                         if (pValue != nullptr) { *pValue = value; }
                         haveValue = true;
                     }
                     while ((++c & dataMask) != 0) {
-                        trieValue2 = getValue(trie->data, valueWidth, ++di);
-                        if (trieValue2 != trieValue) {
-                            if (filter == nullptr ||
-                                    maybeFilterValue(trieValue2, trie->nullValue, nullValue,
-                                                     filter, context) != value) {
-                                return c - 1;
-                            }
-                            trieValue = trieValue2;  // may or may not help
+                        if (maybeFilterValue(getValue(trie->data, valueWidth, ++di),
+                                             trie->nullValue, nullValue,
+                                             filter, context) != value) {
+                            return c - 1;
                         }
                     }
                 }
--- icu-63.2.orig/source/common/umutablecptrie.cpp
+++ icu-63.2/source/common/umutablecptrie.cpp
@@ -304,56 +304,41 @@ UChar32 MutableCodePointTrie::getRange(
     uint32_t nullValue = initialValue;
     if (filter != nullptr) { nullValue = filter(context, nullValue); }
     UChar32 c = start;
-    uint32_t trieValue, value;
+    uint32_t value;
     bool haveValue = false;
     int32_t i = c >> UCPTRIE_SHIFT_3;
     do {
         if (flags[i] == ALL_SAME) {
-            uint32_t trieValue2 = index[i];
+            uint32_t value2 = maybeFilterValue(index[i], initialValue, nullValue,
+                                               filter, context);
             if (haveValue) {
-                if (trieValue2 != trieValue) {
-                    if (filter == nullptr ||
-                            maybeFilterValue(trieValue2, initialValue, nullValue,
-                                             filter, context) != value) {
-                        return c - 1;
-                    }
-                    trieValue = trieValue2;  // may or may not help
+                if (value2 != value) {
+                    return c - 1;
                 }
             } else {
-                trieValue = trieValue2;
-                value = maybeFilterValue(trieValue2, initialValue, nullValue, filter, context);
+                value = value2;
                 if (pValue != nullptr) { *pValue = value; }
                 haveValue = true;
             }
             c = (c + UCPTRIE_SMALL_DATA_BLOCK_LENGTH) & ~UCPTRIE_SMALL_DATA_MASK;
         } else /* MIXED */ {
             int32_t di = index[i] + (c & UCPTRIE_SMALL_DATA_MASK);
-            uint32_t trieValue2 = data[di];
+            uint32_t value2 = maybeFilterValue(data[di], initialValue, nullValue,
+                                               filter, context);
             if (haveValue) {
-                if (trieValue2 != trieValue) {
-                    if (filter == nullptr ||
-                            maybeFilterValue(trieValue2, initialValue, nullValue,
-                                             filter, context) != value) {
-                        return c - 1;
-                    }
-                    trieValue = trieValue2;  // may or may not help
+                if (value2 != value) {
+                    return c - 1;
                 }
             } else {
-                trieValue = trieValue2;
-                value = maybeFilterValue(trieValue2, initialValue, nullValue, filter, context);
+                value = value2;
                 if (pValue != nullptr) { *pValue = value; }
                 haveValue = true;
             }
             while ((++c & UCPTRIE_SMALL_DATA_MASK) != 0) {
-                trieValue2 = data[++di];
-                if (trieValue2 != trieValue) {
-                    if (filter == nullptr ||
-                            maybeFilterValue(trieValue2, initialValue, nullValue,
-                                             filter, context) != value) {
-                        return c - 1;
-                    }
+                if (maybeFilterValue(data[++di], initialValue, nullValue,
+                                     filter, context) != value) {
+                    return c - 1;
                 }
-                trieValue = trieValue2;  // may or may not help
             }
         }
         ++i;
--- icu-63.2.orig/source/common/unicode/uniset.h
+++ icu-63.2/source/common/unicode/uniset.h
@@ -27,6 +27,7 @@ U_NAMESPACE_BEGIN
 
 // Forward Declarations.
 class BMPSet;
+class CharacterProperties;
 class ParsePosition;
 class RBBIRuleScanner;
 class SymbolTable;
@@ -275,23 +276,14 @@ class RuleCharacterIterator;
  * @stable ICU 2.0
  */
 class U_COMMON_API UnicodeSet U_FINAL : public UnicodeFilter {
-private:
-    /**
-     * Enough for sets with few ranges.
-     * For example, White_Space has 10 ranges, list length 21.
-     */
-    static constexpr int32_t INITIAL_CAPACITY = 25;
-    // fFlags constant
-    static constexpr uint8_t kIsBogus = 1;  // This set is bogus (i.e. not valid)
-
-    UChar32* list = stackList; // MUST be terminated with HIGH
-    int32_t capacity = INITIAL_CAPACITY; // capacity of list
-    int32_t len = 1; // length of list used; 1 <= len <= capacity
-    uint8_t fFlags = 0;         // Bit flag (see constants above)
-
-    BMPSet *bmpSet = nullptr; // The set is frozen iff either bmpSet or stringSpan is not NULL.
-    UChar32* buffer = nullptr; // internal buffer, may be NULL
-    int32_t bufferCapacity = 0; // capacity of buffer
+
+    int32_t len; // length of list used; 0 <= len <= capacity
+    int32_t capacity; // capacity of list
+    UChar32* list; // MUST be terminated with HIGH
+    BMPSet *bmpSet; // The set is frozen iff either bmpSet or stringSpan is not NULL.
+    UChar32* buffer; // internal buffer, may be NULL
+    int32_t bufferCapacity; // capacity of buffer
+    int32_t patLen;
 
     /**
      * The pattern representation of this set.  This may not be the
@@ -302,19 +294,15 @@ private:
      * indicating that toPattern() must generate a pattern
      * representation from the inversion list.
      */
-    char16_t *pat = nullptr;
-    int32_t patLen = 0;
-
-    UVector* strings = nullptr; // maintained in sorted order
-    UnicodeSetStringSpan *stringSpan = nullptr;
-
-    /**
-     * Initial list array.
-     * Avoids some heap allocations, and list is never nullptr.
-     * Increases the object size a bit.
-     */
-    UChar32 stackList[INITIAL_CAPACITY];
+    char16_t *pat;
+    UVector* strings; // maintained in sorted order
+    UnicodeSetStringSpan *stringSpan;
 
+private:
+    enum { // constants
+        kIsBogus = 1       // This set is bogus (i.e. not valid)
+    };
+    uint8_t fFlags;         // Bit flag (see constants above)
 public:
     /**
      * Determine if this object contains a valid set.
@@ -1492,6 +1480,8 @@ private:
 
     friend class USetAccess;
 
+    int32_t getStringCount() const;
+
     const UnicodeString* getString(int32_t index) const;
 
     //----------------------------------------------------------------
@@ -1538,18 +1528,13 @@ private:
     // Implementation: Utility methods
     //----------------------------------------------------------------
 
-    static int32_t nextCapacity(int32_t minCapacity);
-
-    bool ensureCapacity(int32_t newLen);
+    void ensureCapacity(int32_t newLen, UErrorCode& ec);
 
-    bool ensureBufferCapacity(int32_t newLen);
+    void ensureBufferCapacity(int32_t newLen, UErrorCode& ec);
 
     void swapBuffers(void);
 
     UBool allocateStrings(UErrorCode &status);
-    UBool hasStrings() const;
-    int32_t stringsSize() const;
-    UBool stringsContains(const UnicodeString &s) const;
 
     UnicodeString& _toPattern(UnicodeString& result,
                               UBool escapeUnprintable) const;
@@ -1629,6 +1614,7 @@ private:
                               UnicodeString& rebuiltPat,
                               UErrorCode& ec);
 
+    friend class CharacterProperties;
     static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status);
 
     /**
@@ -1660,10 +1646,7 @@ private:
     /**
      * Set the new pattern to cache.
      */
-    void setPattern(const UnicodeString& newPat) {
-        setPattern(newPat.getBuffer(), newPat.length());
-    }
-    void setPattern(const char16_t *newPat, int32_t newPatLen);
+    void setPattern(const UnicodeString& newPat);
     /**
      * Release existing cached pattern.
      */
--- icu-63.2.orig/source/common/uniset.cpp
+++ icu-63.2/source/common/uniset.cpp
@@ -14,7 +14,6 @@
 #include "unicode/parsepos.h"
 #include "unicode/symtable.h"
 #include "unicode/uniset.h"
-#include "unicode/ustring.h"
 #include "unicode/utf8.h"
 #include "unicode/utf16.h"
 #include "ruleiter.h"
@@ -54,8 +53,11 @@
 // LOW <= all valid values. ZERO for codepoints
 #define UNICODESET_LOW 0x000000
 
-/** Max list [0, 1, 2, ..., max code point, HIGH] */
-constexpr int32_t MAX_LENGTH = UNICODESET_HIGH + 1;
+// initial storage. Must be >= 0
+#define START_EXTRA 16
+
+// extra amount for growth. Must be >= 0
+#define GROW_EXTRA START_EXTRA
 
 U_NAMESPACE_BEGIN
 
@@ -135,18 +137,6 @@ static int8_t U_CALLCONV compareUnicodeS
     return a.compare(b);
 }
 
-UBool UnicodeSet::hasStrings() const {
-    return strings != nullptr && !strings->isEmpty();
-}
-
-int32_t UnicodeSet::stringsSize() const {
-    return strings == nullptr ? 0 : strings->size();
-}
-
-UBool UnicodeSet::stringsContains(const UnicodeString &s) const {
-    return strings != nullptr && strings->contains((void*) &s);
-}
-
 //----------------------------------------------------------------
 // Constructors &c
 //----------------------------------------------------------------
@@ -154,8 +144,24 @@ UBool UnicodeSet::stringsContains(const
 /**
  * Constructs an empty set.
  */
-UnicodeSet::UnicodeSet() {
-    list[0] = UNICODESET_HIGH;
+UnicodeSet::UnicodeSet() :
+    len(1), capacity(1 + START_EXTRA), list(0), bmpSet(0), buffer(0),
+    bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    allocateStrings(status);
+    if (U_FAILURE(status)) {
+        setToBogus(); // If memory allocation failed, set to bogus state.
+        return;
+    }
+    list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+    if(list!=NULL){
+        list[0] = UNICODESET_HIGH;
+    } else { // If memory allocation failed, set to bogus state.
+        setToBogus();
+        return;
+    }
     _dbgct(this);
 }
 
@@ -166,39 +172,89 @@ UnicodeSet::UnicodeSet() {
  * @param start first character, inclusive, of range
  * @param end last character, inclusive, of range
  */
-UnicodeSet::UnicodeSet(UChar32 start, UChar32 end) {
-    list[0] = UNICODESET_HIGH;
-    add(start, end);
+UnicodeSet::UnicodeSet(UChar32 start, UChar32 end) :
+    len(1), capacity(1 + START_EXTRA), list(0), bmpSet(0), buffer(0),
+    bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    allocateStrings(status);
+    if (U_FAILURE(status)) {
+        setToBogus(); // If memory allocation failed, set to bogus state.
+        return;
+    }
+    list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+    if(list!=NULL){
+        list[0] = UNICODESET_HIGH;
+        complement(start, end);
+    } else { // If memory allocation failed, set to bogus state.
+        setToBogus();
+        return;
+    }
     _dbgct(this);
 }
 
 /**
  * Constructs a set that is identical to the given UnicodeSet.
  */
-UnicodeSet::UnicodeSet(const UnicodeSet& o) : UnicodeFilter(o) {
-    *this = o;
+UnicodeSet::UnicodeSet(const UnicodeSet& o) :
+    UnicodeFilter(o),
+    len(0), capacity(o.isFrozen() ? o.len : o.len + GROW_EXTRA), list(0),
+    bmpSet(0),
+    buffer(0), bufferCapacity(0),
+    patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    allocateStrings(status);
+    if (U_FAILURE(status)) {
+        setToBogus(); // If memory allocation failed, set to bogus state.
+        return;
+    }
+    list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+    if(list!=NULL){
+        *this = o;
+    } else { // If memory allocation failed, set to bogus state.
+        setToBogus();
+        return;
+    }
     _dbgct(this);
 }
 
 // Copy-construct as thawed.
-UnicodeSet::UnicodeSet(const UnicodeSet& o, UBool /* asThawed */) : UnicodeFilter(o) {
-    if (ensureCapacity(o.len)) {
+UnicodeSet::UnicodeSet(const UnicodeSet& o, UBool /* asThawed */) :
+    UnicodeFilter(o),
+    len(0), capacity(o.len + GROW_EXTRA), list(0),
+    bmpSet(0),
+    buffer(0), bufferCapacity(0),
+    patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    allocateStrings(status);
+    if (U_FAILURE(status)) {
+        setToBogus(); // If memory allocation failed, set to bogus state.
+        return;
+    }
+    list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+    if(list!=NULL){
         // *this = o except for bmpSet and stringSpan
         len = o.len;
         uprv_memcpy(list, o.list, (size_t)len*sizeof(UChar32));
-        if (o.hasStrings()) {
-            UErrorCode status = U_ZERO_ERROR;
-            if (!allocateStrings(status) ||
-                    (strings->assign(*o.strings, cloneUnicodeString, status), U_FAILURE(status))) {
-                setToBogus();
-                return;
-            }
+        if (strings != NULL && o.strings != NULL) {
+            strings->assign(*o.strings, cloneUnicodeString, status);
+        } else { // Invalid strings.
+            setToBogus();
+            return;
         }
         if (o.pat) {
-            setPattern(o.pat, o.patLen);
+            setPattern(UnicodeString(o.pat, o.patLen));
         }
-        _dbgct(this);
+    } else { // If memory allocation failed, set to bogus state.
+        setToBogus();
+        return;
     }
+    _dbgct(this);
 }
 
 /**
@@ -206,11 +262,9 @@ UnicodeSet::UnicodeSet(const UnicodeSet&
  */
 UnicodeSet::~UnicodeSet() {
     _dbgdt(this); // first!
-    if (list != stackList) {
-        uprv_free(list);
-    }
+    uprv_free(list);
     delete bmpSet;
-    if (buffer != stackList) {
+    if (buffer) {
         uprv_free(buffer);
     }
     delete strings;
@@ -236,30 +290,32 @@ UnicodeSet& UnicodeSet::copyFrom(const U
         setToBogus();
         return *this;
     }
-    if (!ensureCapacity(o.len)) {
+    UErrorCode ec = U_ZERO_ERROR;
+    ensureCapacity(o.len, ec);
+    if (U_FAILURE(ec)) {
         // ensureCapacity will mark the UnicodeSet as Bogus if OOM failure happens.
         return *this;
     }
     len = o.len;
     uprv_memcpy(list, o.list, (size_t)len*sizeof(UChar32));
-    if (o.bmpSet != nullptr && !asThawed) {
+    if (o.bmpSet == NULL || asThawed) {
+        bmpSet = NULL;
+    } else {
         bmpSet = new BMPSet(*o.bmpSet, list, len);
         if (bmpSet == NULL) { // Check for memory allocation error.
             setToBogus();
             return *this;
         }
     }
-    if (o.hasStrings()) {
-        UErrorCode status = U_ZERO_ERROR;
-        if ((strings == nullptr && !allocateStrings(status)) ||
-                (strings->assign(*o.strings, cloneUnicodeString, status), U_FAILURE(status))) {
-            setToBogus();
-            return *this;
-        }
-    } else if (hasStrings()) {
-        strings->removeAllElements();
+    if (strings != NULL && o.strings != NULL) {
+        strings->assign(*o.strings, cloneUnicodeString, ec);
+    } else { // Invalid strings.
+        setToBogus();
+        return *this;
     }
-    if (o.stringSpan != nullptr && !asThawed) {
+    if (o.stringSpan == NULL || asThawed) {
+        stringSpan = NULL;
+    } else {
         stringSpan = new UnicodeSetStringSpan(*o.stringSpan, *strings);
         if (stringSpan == NULL) { // Check for memory allocation error.
             setToBogus();
@@ -268,7 +324,7 @@ UnicodeSet& UnicodeSet::copyFrom(const U
     }
     releasePattern();
     if (o.pat) {
-        setPattern(o.pat, o.patLen);
+        setPattern(UnicodeString(o.pat, o.patLen));
     }
     return *this;
 }
@@ -301,8 +357,7 @@ UBool UnicodeSet::operator==(const Unico
     for (int32_t i = 0; i < len; ++i) {
         if (list[i] != o.list[i]) return FALSE;
     }
-    if (hasStrings() != o.hasStrings()) { return FALSE; }
-    if (hasStrings() && *strings != *o.strings) return FALSE;
+    if (*strings != *o.strings) return FALSE;
     return TRUE;
 }
 
@@ -338,7 +393,7 @@ int32_t UnicodeSet::size(void) const {
     for (int32_t i = 0; i < count; ++i) {
         n += getRangeEnd(i) - getRangeStart(i) + 1;
     }
-    return n + stringsSize();
+    return n + strings->size();
 }
 
 /**
@@ -347,7 +402,7 @@ int32_t UnicodeSet::size(void) const {
  * @return <tt>true</tt> if this set contains no elements.
  */
 UBool UnicodeSet::isEmpty(void) const {
-    return len == 1 && !hasStrings();
+    return len == 1 && strings->size() == 0;
 }
 
 /**
@@ -447,7 +502,7 @@ UBool UnicodeSet::contains(const Unicode
     if (s.length() == 0) return FALSE;
     int32_t cp = getSingleCP(s);
     if (cp < 0) {
-        return stringsContains(s);
+        return strings->contains((void*) &s);
     } else {
         return contains((UChar32) cp);
     }
@@ -469,7 +524,8 @@ UBool UnicodeSet::containsAll(const Unic
             return FALSE;
         }
     }
-    return !c.hasStrings() || (strings != nullptr && strings->containsAll(*c.strings));
+    if (!strings->containsAll(*c.strings)) return FALSE;
+    return TRUE;
 }
 
 /**
@@ -515,7 +571,8 @@ UBool UnicodeSet::containsNone(const Uni
             return FALSE;
         }
     }
-    return strings == nullptr || !c.hasStrings() || strings->containsNone(*c.strings);
+    if (!strings->containsNone(*c.strings)) return FALSE;
+    return TRUE;
 }
 
 /**
@@ -556,7 +613,7 @@ UBool UnicodeSet::matchesIndexValue(uint
             return TRUE;
         }
     }
-    if (hasStrings()) {
+    if (strings->size() != 0) {
         for (i=0; i<strings->size(); ++i) {
             const UnicodeString& s = *(const UnicodeString*)strings->elementAt(i);
             //if (s.length() == 0) {
@@ -591,7 +648,7 @@ UMatchDegree UnicodeSet::matches(const R
             return U_MISMATCH;
         }
     } else {
-        if (hasStrings()) { // try strings first
+        if (strings->size() != 0) { // try strings first
 
             // might separate forward and backward loops later
             // for now they are combined
@@ -792,39 +849,7 @@ UnicodeSet& UnicodeSet::set(UChar32 star
  */
 UnicodeSet& UnicodeSet::add(UChar32 start, UChar32 end) {
     if (pinCodePoint(start) < pinCodePoint(end)) {
-        UChar32 limit = end + 1;
-        // Fast path for adding a new range after the last one.
-        // Odd list length: [..., lastStart, lastLimit, HIGH]
-        if ((len & 1) != 0) {
-            // If the list is empty, set lastLimit low enough to not be adjacent to 0.
-            UChar32 lastLimit = len == 1 ? -2 : list[len - 2];
-            if (lastLimit <= start && !isFrozen() && !isBogus()) {
-                if (lastLimit == start) {
-                    // Extend the last range.
-                    list[len - 2] = limit;
-                    if (limit == UNICODESET_HIGH) {
-                        --len;
-                    }
-                } else {
-                    list[len - 1] = start;
-                    if (limit < UNICODESET_HIGH) {
-                        if (ensureCapacity(len + 2)) {
-                            list[len++] = limit;
-                            list[len++] = UNICODESET_HIGH;
-                        }
-                    } else {  // limit == UNICODESET_HIGH
-                        if (ensureCapacity(len + 1)) {
-                            list[len++] = UNICODESET_HIGH;
-                        }
-                    }
-                }
-                releasePattern();
-                return *this;
-            }
-        }
-        // This is slow. Could be much faster using findCodePoint(start)
-        // and modifying the list, dealing with adjacent & overlapping ranges.
-        UChar32 range[3] = { start, limit, UNICODESET_HIGH };
+        UChar32 range[3] = { start, end+1, UNICODESET_HIGH };
         add(range, 2, 0);
     } else if (start == end) {
         add(start);
@@ -893,7 +918,9 @@ UnicodeSet& UnicodeSet::add(UChar32 c) {
         list[i] = c;
         // if we touched the HIGH mark, then add a new one
         if (c == (UNICODESET_HIGH - 1)) {
-            if (!ensureCapacity(len+1)) {
+            UErrorCode status = U_ZERO_ERROR;
+            ensureCapacity(len+1, status);
+            if (U_FAILURE(status)) {
                 // ensureCapacity will mark the object as Bogus if OOM failure happens.
                 return *this;
             }
@@ -937,13 +964,21 @@ UnicodeSet& UnicodeSet::add(UChar32 c) {
         //                             ^
         //                             list[i]
 
-        if (!ensureCapacity(len+2)) {
+        UErrorCode status = U_ZERO_ERROR;
+        ensureCapacity(len+2, status);
+        if (U_FAILURE(status)) {
             // ensureCapacity will mark the object as Bogus if OOM failure happens.
             return *this;
         }
 
-        UChar32 *p = list + i;
-        uprv_memmove(p + 2, p, (len - i) * sizeof(*p));
+        //for (int32_t k=len-1; k>=i; --k) {
+        //    list[k+2] = list[k];
+        //}
+        UChar32* src = list + len;
+        UChar32* dst = src + 2;
+        UChar32* srclimit = list + i;
+        while (src > srclimit) *(--dst) = *(--src);
+
         list[i] = c;
         list[i+1] = c+1;
         len += 2;
@@ -979,7 +1014,7 @@ UnicodeSet& UnicodeSet::add(const Unicod
     if (s.length() == 0 || isFrozen() || isBogus()) return *this;
     int32_t cp = getSingleCP(s);
     if (cp < 0) {
-        if (!stringsContains(s)) {
+        if (!strings->contains((void*) &s)) {
             _add(s);
             releasePattern();
         }
@@ -998,16 +1033,12 @@ void UnicodeSet::_add(const UnicodeStrin
     if (isFrozen() || isBogus()) {
         return;
     }
-    UErrorCode ec = U_ZERO_ERROR;
-    if (strings == nullptr && !allocateStrings(ec)) {
-        setToBogus();
-        return;
-    }
     UnicodeString* t = new UnicodeString(s);
     if (t == NULL) { // Check for memory allocation error.
         setToBogus();
         return;
     }
+    UErrorCode ec = U_ZERO_ERROR;
     strings->sortedInsert(t, compareUnicodeString, ec);
     if (U_FAILURE(ec)) {
         setToBogus();
@@ -1090,10 +1121,7 @@ UnicodeSet& UnicodeSet::removeAll(const
 }
 
 UnicodeSet& UnicodeSet::removeAllStrings() {
-    if (!isFrozen() && hasStrings()) {
-        strings->removeAllElements();
-        releasePattern();
-    }
+    strings->removeAllElements();
     return *this;
 }
 
@@ -1189,9 +1217,8 @@ UnicodeSet& UnicodeSet::remove(const Uni
     if (s.length() == 0 || isFrozen() || isBogus()) return *this;
     int32_t cp = getSingleCP(s);
     if (cp < 0) {
-        if (strings != nullptr && strings->removeElement((void*) &s)) {
-            releasePattern();
-        }
+        strings->removeElement((void*) &s);
+        releasePattern();
     } else {
         remove((UChar32)cp, (UChar32)cp);
     }
@@ -1233,17 +1260,24 @@ UnicodeSet& UnicodeSet::complement(void)
     if (isFrozen() || isBogus()) {
         return *this;
     }
+    UErrorCode status = U_ZERO_ERROR;
     if (list[0] == UNICODESET_LOW) {
-        uprv_memmove(list, list + 1, (size_t)(len-1)*sizeof(UChar32));
+        ensureBufferCapacity(len-1, status);
+        if (U_FAILURE(status)) {
+            return *this;
+        }
+        uprv_memcpy(buffer, list + 1, (size_t)(len-1)*sizeof(UChar32));
         --len;
     } else {
-        if (!ensureCapacity(len+1)) {
+        ensureBufferCapacity(len+1, status);
+        if (U_FAILURE(status)) {
             return *this;
         }
-        uprv_memmove(list + 1, list, (size_t)len*sizeof(UChar32));
-        list[0] = UNICODESET_LOW;
+        uprv_memcpy(buffer + 1, list, (size_t)len*sizeof(UChar32));
+        buffer[0] = UNICODESET_LOW;
         ++len;
     }
+    swapBuffers();
     releasePattern();
     return *this;
 }
@@ -1260,7 +1294,7 @@ UnicodeSet& UnicodeSet::complement(const
     if (s.length() == 0 || isFrozen() || isBogus()) return *this;
     int32_t cp = getSingleCP(s);
     if (cp < 0) {
-        if (stringsContains(s)) {
+        if (strings->contains((void*) &s)) {
             strings->removeElement((void*) &s);
         } else {
             _add(s);
@@ -1291,7 +1325,7 @@ UnicodeSet& UnicodeSet::addAll(const Uni
     if ( c.strings!=NULL ) {
         for (int32_t i=0; i<c.strings->size(); ++i) {
             const UnicodeString* s = (const UnicodeString*)c.strings->elementAt(i);
-            if (!stringsContains(*s)) {
+            if (!strings->contains((void*) s)) {
                 _add(*s);
             }
         }
@@ -1313,13 +1347,7 @@ UnicodeSet& UnicodeSet::retainAll(const
         return *this;
     }
     retain(c.list, c.len, 0);
-    if (hasStrings()) {
-        if (!c.hasStrings()) {
-            strings->removeAllElements();
-        } else {
-            strings->retainAll(*c.strings);
-        }
-    }
+    strings->retainAll(*c.strings);
     return *this;
 }
 
@@ -1337,9 +1365,7 @@ UnicodeSet& UnicodeSet::removeAll(const
         return *this;
     }
     retain(c.list, c.len, 2);
-    if (hasStrings() && c.hasStrings()) {
-        strings->removeAll(*c.strings);
-    }
+    strings->removeAll(*c.strings);
     return *this;
 }
 
@@ -1357,12 +1383,10 @@ UnicodeSet& UnicodeSet::complementAll(co
     }
     exclusiveOr(c.list, c.len, 0);
 
-    if (c.strings != nullptr) {
-        for (int32_t i=0; i<c.strings->size(); ++i) {
-            void* e = c.strings->elementAt(i);
-            if (strings == nullptr || !strings->removeElement(e)) {
-                _add(*(const UnicodeString*)e);
-            }
+    for (int32_t i=0; i<c.strings->size(); ++i) {
+        void* e = c.strings->elementAt(i);
+        if (!strings->removeElement(e)) {
+            _add(*(const UnicodeString*)e);
         }
     }
     return *this;
@@ -1376,14 +1400,18 @@ UnicodeSet& UnicodeSet::clear(void) {
     if (isFrozen()) {
         return *this;
     }
-    list[0] = UNICODESET_HIGH;
+    if (list != NULL) {
+        list[0] = UNICODESET_HIGH;
+    }
     len = 1;
     releasePattern();
     if (strings != NULL) {
         strings->removeAllElements();
     }
-    // Remove bogus
-    fFlags = 0;
+    if (list != NULL && strings != NULL) {
+        // Remove bogus
+        fFlags = 0;
+    }
     return *this;
 }
 
@@ -1417,6 +1445,10 @@ UChar32 UnicodeSet::getRangeEnd(int32_t
     return list[index*2 + 1] - 1;
 }
 
+int32_t UnicodeSet::getStringCount() const {
+    return strings->size();
+}
+
 const UnicodeString* UnicodeSet::getString(int32_t index) const {
     return (const UnicodeString*) strings->elementAt(index);
 }
@@ -1430,32 +1462,22 @@ UnicodeSet& UnicodeSet::compact() {
         return *this;
     }
     // Delete buffer first to defragment memory less.
-    if (buffer != stackList) {
+    if (buffer != NULL) {
         uprv_free(buffer);
         buffer = NULL;
-        bufferCapacity = 0;
     }
-    if (list == stackList) {
-        // pass
-    } else if (len <= INITIAL_CAPACITY) {
-        uprv_memcpy(stackList, list, len * sizeof(UChar32));
-        uprv_free(list);
-        list = stackList;
-        capacity = INITIAL_CAPACITY;
-    } else if ((len + 7) < capacity) {
-        // If we have more than a little unused capacity, shrink it to len.
-        UChar32* temp = (UChar32*) uprv_realloc(list, sizeof(UChar32) * len);
+    if (len < capacity) {
+        // Make the capacity equal to len or 1.
+        // We don't want to realloc of 0 size.
+        int32_t newCapacity = len + (len == 0);
+        UChar32* temp = (UChar32*) uprv_realloc(list, sizeof(UChar32) * newCapacity);
         if (temp) {
             list = temp;
-            capacity = len;
+            capacity = newCapacity;
         }
         // else what the heck happened?! We allocated less memory!
         // Oh well. We'll keep our original array.
     }
-    if (strings != nullptr && strings->isEmpty()) {
-        delete strings;
-        strings = nullptr;
-    }
     return *this;
 }
 
@@ -1466,8 +1488,10 @@ UnicodeSet& UnicodeSet::compact() {
 /**
  * Deserialize constructor.
  */
-UnicodeSet::UnicodeSet(const uint16_t data[], int32_t dataLen, ESerialization serialization,
-                       UErrorCode &ec) {
+UnicodeSet::UnicodeSet(const uint16_t data[], int32_t dataLen, ESerialization serialization, UErrorCode &ec)
+  : len(1), capacity(1+START_EXTRA), list(0), bmpSet(0), buffer(0),
+    bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0) {
 
   if(U_FAILURE(ec)) {
     setToBogus();
@@ -1482,15 +1506,24 @@ UnicodeSet::UnicodeSet(const uint16_t da
     return;
   }
 
+  allocateStrings(ec);
+  if (U_FAILURE(ec)) {
+    setToBogus();
+    return;
+  }
+
   // bmp?
   int32_t headerSize = ((data[0]&0x8000)) ?2:1;
   int32_t bmpLength = (headerSize==1)?data[0]:data[1];
 
-  int32_t newLength = (((data[0]&0x7FFF)-bmpLength)/2)+bmpLength;
+  len = (((data[0]&0x7FFF)-bmpLength)/2)+bmpLength;
 #ifdef DEBUG_SERIALIZE
-  printf("dataLen %d headerSize %d bmpLen %d len %d. data[0]=%X/%X/%X/%X\n", dataLen,headerSize,bmpLength,newLength, data[0],data[1],data[2],data[3]);
+  printf("dataLen %d headerSize %d bmpLen %d len %d. data[0]=%X/%X/%X/%X\n", dataLen,headerSize,bmpLength,len, data[0],data[1],data[2],data[3]);
 #endif
-  if(!ensureCapacity(newLength + 1)) {  // +1 for HIGH
+  capacity = len+1;
+  list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+  if(!list || U_FAILURE(ec)) {
+    setToBogus();
     return;
   }
   // copy bmp
@@ -1502,18 +1535,15 @@ UnicodeSet::UnicodeSet(const uint16_t da
 #endif
   }
   // copy smp
-  for(i=bmpLength;i<newLength;i++) {
+  for(i=bmpLength;i<len;i++) {
     list[i] = ((UChar32)data[headerSize+bmpLength+(i-bmpLength)*2+0] << 16) +
               ((UChar32)data[headerSize+bmpLength+(i-bmpLength)*2+1]);
 #ifdef DEBUG_SERIALIZE
     printf("<<32@%d+[%d] %lX\n", headerSize+bmpLength+i, i, list[i]);
 #endif
   }
-  U_ASSERT(i == newLength);
-  if (i == 0 || list[i - 1] != UNICODESET_HIGH) {
-    list[i++] = UNICODESET_HIGH;
-  }
-  len = i;
+  // terminator
+  list[len++]=UNICODESET_HIGH;
 }
 
 
@@ -1634,65 +1664,33 @@ UBool UnicodeSet::allocateStrings(UError
     return TRUE;
 }
 
-int32_t UnicodeSet::nextCapacity(int32_t minCapacity) {
-    // Grow exponentially to reduce the frequency of allocations.
-    if (minCapacity < INITIAL_CAPACITY) {
-        return minCapacity + INITIAL_CAPACITY;
-    } else if (minCapacity <= 2500) {
-        return 5 * minCapacity;
-    } else {
-        int32_t newCapacity = 2 * minCapacity;
-        if (newCapacity > MAX_LENGTH) {
-            newCapacity = MAX_LENGTH;
-        }
-        return newCapacity;
-    }
-}
-
-bool UnicodeSet::ensureCapacity(int32_t newLen) {
-    if (newLen > MAX_LENGTH) {
-        newLen = MAX_LENGTH;
-    }
+void UnicodeSet::ensureCapacity(int32_t newLen, UErrorCode& ec) {
     if (newLen <= capacity) {
-        return true;
+        return;
     }
-    int32_t newCapacity = nextCapacity(newLen);
-    UChar32* temp = (UChar32*) uprv_malloc(newCapacity * sizeof(UChar32));
+    UChar32* temp = (UChar32*) uprv_realloc(list, sizeof(UChar32) * (newLen + GROW_EXTRA));
     if (temp == NULL) {
+        ec = U_MEMORY_ALLOCATION_ERROR;
         setToBogus(); // set the object to bogus state if an OOM failure occurred.
-        return false;
-    }
-    // Copy only the actual contents.
-    uprv_memcpy(temp, list, len * sizeof(UChar32));
-    if (list != stackList) {
-        uprv_free(list);
+        return;
     }
     list = temp;
-    capacity = newCapacity;
-    return true;
+    capacity = newLen + GROW_EXTRA;
+    // else we keep the original contents on the memory failure.
 }
 
-bool UnicodeSet::ensureBufferCapacity(int32_t newLen) {
-    if (newLen > MAX_LENGTH) {
-        newLen = MAX_LENGTH;
-    }
-    if (newLen <= bufferCapacity) {
-        return true;
-    }
-    int32_t newCapacity = nextCapacity(newLen);
-    UChar32* temp = (UChar32*) uprv_malloc(newCapacity * sizeof(UChar32));
+void UnicodeSet::ensureBufferCapacity(int32_t newLen, UErrorCode& ec) {
+    if (buffer != NULL && newLen <= bufferCapacity)
+        return;
+    UChar32* temp = (UChar32*) uprv_realloc(buffer, sizeof(UChar32) * (newLen + GROW_EXTRA));
     if (temp == NULL) {
+        ec = U_MEMORY_ALLOCATION_ERROR;
         setToBogus();
-        return false;
-    }
-    // The buffer has no contents to be copied.
-    // It is always filled from scratch after this call.
-    if (buffer != stackList) {
-        uprv_free(buffer);
+        return;
     }
     buffer = temp;
-    bufferCapacity = newCapacity;
-    return true;
+    bufferCapacity = newLen + GROW_EXTRA;
+    // else we keep the original contents on the memory failure.
 }
 
 /**
@@ -1729,7 +1727,9 @@ void UnicodeSet::exclusiveOr(const UChar
     if (isFrozen() || isBogus()) {
         return;
     }
-    if (!ensureBufferCapacity(len + otherLen)) {
+    UErrorCode status = U_ZERO_ERROR;
+    ensureBufferCapacity(len + otherLen, status);
+    if (U_FAILURE(status)) {
         return;
     }
 
@@ -1777,7 +1777,9 @@ void UnicodeSet::add(const UChar32* othe
     if (isFrozen() || isBogus() || other==NULL) {
         return;
     }
-    if (!ensureBufferCapacity(len + otherLen)) {
+    UErrorCode status = U_ZERO_ERROR;
+    ensureBufferCapacity(len + otherLen, status);
+    if (U_FAILURE(status)) {
         return;
     }
 
@@ -1888,7 +1890,9 @@ void UnicodeSet::retain(const UChar32* o
     if (isFrozen() || isBogus()) {
         return;
     }
-    if (!ensureBufferCapacity(len + otherLen)) {
+    UErrorCode status = U_ZERO_ERROR;
+    ensureBufferCapacity(len + otherLen, status);
+    if (U_FAILURE(status)) {
         return;
     }
 
@@ -2134,14 +2138,12 @@ UnicodeString& UnicodeSet::_generatePatt
         }
     }
 
-    if (strings != nullptr) {
-        for (int32_t i = 0; i<strings->size(); ++i) {
-            result.append(OPEN_BRACE);
-            _appendToPat(result,
-                         *(const UnicodeString*) strings->elementAt(i),
-                         escapeUnprintable);
-            result.append(CLOSE_BRACE);
-        }
+    for (int32_t i = 0; i<strings->size(); ++i) {
+        result.append(OPEN_BRACE);
+        _appendToPat(result,
+                     *(const UnicodeString*) strings->elementAt(i),
+                     escapeUnprintable);
+        result.append(CLOSE_BRACE);
     }
     return result.append(SET_CLOSE);
 }
@@ -2160,12 +2162,13 @@ void UnicodeSet::releasePattern() {
 /**
 * Set the new pattern to cache.
 */
-void UnicodeSet::setPattern(const char16_t *newPat, int32_t newPatLen) {
+void UnicodeSet::setPattern(const UnicodeString& newPat) {
     releasePattern();
+    int32_t newPatLen = newPat.length();
     pat = (UChar *)uprv_malloc((newPatLen + 1) * sizeof(UChar));
     if (pat) {
         patLen = newPatLen;
-        u_memcpy(pat, newPat, patLen);
+        newPat.extractBetween(0, patLen, pat);
         pat[patLen] = 0;
     }
     // else we don't care if malloc failed. This was just a nice cache.
@@ -2174,15 +2177,30 @@ void UnicodeSet::setPattern(const char16
 
 UnicodeFunctor *UnicodeSet::freeze() {
     if(!isFrozen() && !isBogus()) {
-        compact();
+        // Do most of what compact() does before freezing because
+        // compact() will not work when the set is frozen.
+        // Small modification: Don't shrink if the savings would be tiny (<=GROW_EXTRA).
+
+        // Delete buffer first to defragment memory less.
+        if (buffer != NULL) {
+            uprv_free(buffer);
+            buffer = NULL;
+        }
+        if (capacity > (len + GROW_EXTRA)) {
+            // Make the capacity equal to len or 1.
+            // We don't want to realloc of 0 size.
+            capacity = len + (len == 0);
+            list = (UChar32*) uprv_realloc(list, sizeof(UChar32) * capacity);
+            if (list == NULL) { // Check for memory allocation error.
+                setToBogus();
+                return this;
+            }
+        }
 
         // Optimize contains() and span() and similar functions.
-        if (hasStrings()) {
+        if (!strings->isEmpty()) {
             stringSpan = new UnicodeSetStringSpan(*this, *strings, UnicodeSetStringSpan::ALL);
-            if (stringSpan == nullptr) {
-                setToBogus();
-                return this;
-            } else if (!stringSpan->needsStringSpanUTF16()) {
+            if (stringSpan != NULL && !stringSpan->needsStringSpanUTF16()) {
                 // All strings are irrelevant for span() etc. because
                 // all of each string's code points are contained in this set.
                 // Do not check needsStringSpanUTF8() because UTF-8 has at most as
@@ -2215,7 +2233,7 @@ int32_t UnicodeSet::span(const UChar *s,
     }
     if(stringSpan!=NULL) {
         return stringSpan->span(s, length, spanCondition);
-    } else if(hasStrings()) {
+    } else if(!strings->isEmpty()) {
         uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
                             UnicodeSetStringSpan::FWD_UTF16_NOT_CONTAINED :
                             UnicodeSetStringSpan::FWD_UTF16_CONTAINED;
@@ -2252,7 +2270,7 @@ int32_t UnicodeSet::spanBack(const UChar
     }
     if(stringSpan!=NULL) {
         return stringSpan->spanBack(s, length, spanCondition);
-    } else if(hasStrings()) {
+    } else if(!strings->isEmpty()) {
         uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
                             UnicodeSetStringSpan::BACK_UTF16_NOT_CONTAINED :
                             UnicodeSetStringSpan::BACK_UTF16_CONTAINED;
@@ -2290,7 +2308,7 @@ int32_t UnicodeSet::spanUTF8(const char
     }
     if(stringSpan!=NULL) {
         return stringSpan->spanUTF8((const uint8_t *)s, length, spanCondition);
-    } else if(hasStrings()) {
+    } else if(!strings->isEmpty()) {
         uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
                             UnicodeSetStringSpan::FWD_UTF8_NOT_CONTAINED :
                             UnicodeSetStringSpan::FWD_UTF8_CONTAINED;
@@ -2328,7 +2346,7 @@ int32_t UnicodeSet::spanBackUTF8(const c
     }
     if(stringSpan!=NULL) {
         return stringSpan->spanBackUTF8((const uint8_t *)s, length, spanCondition);
-    } else if(hasStrings()) {
+    } else if(!strings->isEmpty()) {
         uint32_t which= spanCondition==USET_SPAN_NOT_CONTAINED ?
                             UnicodeSetStringSpan::BACK_UTF8_NOT_CONTAINED :
                             UnicodeSetStringSpan::BACK_UTF8_CONTAINED;
--- icu-63.2.orig/source/common/uniset_closure.cpp
+++ icu-63.2/source/common/uniset_closure.cpp
@@ -31,6 +31,10 @@
 #include "util.h"
 #include "uvector.h"
 
+// initial storage. Must be >= 0
+// *** same as in uniset.cpp ! ***
+#define START_EXTRA 16
+
 U_NAMESPACE_BEGIN
 
 // TODO memory debugging provided inside uniset.cpp
@@ -45,16 +49,42 @@ U_NAMESPACE_BEGIN
 UnicodeSet::UnicodeSet(const UnicodeString& pattern,
                        uint32_t options,
                        const SymbolTable* symbols,
-                       UErrorCode& status) {
-    applyPattern(pattern, options, symbols, status);
+                       UErrorCode& status) :
+    len(0), capacity(START_EXTRA), list(0), bmpSet(0), buffer(0),
+    bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    if(U_SUCCESS(status)){
+        list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+        /* test for NULL */
+        if(list == NULL) {
+            status = U_MEMORY_ALLOCATION_ERROR;  
+        }else{
+            allocateStrings(status);
+            applyPattern(pattern, options, symbols, status);
+        }
+    }
     _dbgct(this);
 }
 
 UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos,
                        uint32_t options,
                        const SymbolTable* symbols,
-                       UErrorCode& status) {
-    applyPattern(pattern, pos, options, symbols, status);
+                       UErrorCode& status) :
+    len(0), capacity(START_EXTRA), list(0), bmpSet(0), buffer(0),
+    bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    if(U_SUCCESS(status)){
+        list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+        /* test for NULL */
+        if(list == NULL) {
+            status = U_MEMORY_ALLOCATION_ERROR;   
+        }else{
+            allocateStrings(status);
+            applyPattern(pattern, pos, options, symbols, status);
+        }
+    }
     _dbgct(this);
 }
 
@@ -169,7 +199,7 @@ UnicodeSet& UnicodeSet::closeOver(int32_
             // start with input set to guarantee inclusion
             // USET_CASE: remove strings because the strings will actually be reduced (folded);
             //            therefore, start with no strings and add only those needed
-            if ((attribute & USET_CASE_INSENSITIVE) && foldSet.hasStrings()) {
+            if (attribute & USET_CASE_INSENSITIVE) {
                 foldSet.strings->removeAllElements();
             }
 
@@ -204,7 +234,7 @@ UnicodeSet& UnicodeSet::closeOver(int32_
                     }
                 }
             }
-            if (hasStrings()) {
+            if (strings != NULL && strings->size() > 0) {
                 if (attribute & USET_CASE_INSENSITIVE) {
                     for (int32_t j=0; j<strings->size(); ++j) {
                         str = *(const UnicodeString *) strings->elementAt(j);
--- icu-63.2.orig/source/common/uniset_props.cpp
+++ icu-63.2/source/common/uniset_props.cpp
@@ -47,6 +47,10 @@
 
 U_NAMESPACE_USE
 
+// initial storage. Must be >= 0
+// *** same as in uniset.cpp ! ***
+#define START_EXTRA 16
+
 // Define UChar constants using hex for EBCDIC compatibility
 // Used #define to reduce private static exports and memory access time.
 #define SET_OPEN        ((UChar)0x005B) /*[*/
@@ -181,8 +185,21 @@ isPOSIXClose(const UnicodeString &patter
  * @param pattern a string specifying what characters are in the set
  */
 UnicodeSet::UnicodeSet(const UnicodeString& pattern,
-                       UErrorCode& status) {
-    applyPattern(pattern, status);
+                       UErrorCode& status) :
+    len(0), capacity(START_EXTRA), list(0), bmpSet(0), buffer(0),
+    bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL),
+    fFlags(0)
+{
+    if(U_SUCCESS(status)){
+        list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity);
+        /* test for NULL */
+        if(list == NULL) {
+            status = U_MEMORY_ALLOCATION_ERROR;  
+        }else{
+            allocateStrings(status);
+            applyPattern(pattern, status);
+        }
+    }
     _dbgct(this);
 }
 
@@ -696,11 +713,6 @@ static UBool numericValueFilter(UChar32
     return u_getNumericValue(ch) == *(double*)context;
 }
 
-static UBool generalCategoryMaskFilter(UChar32 ch, void* context) {
-    int32_t value = *(int32_t*)context;
-    return (U_GET_GC_MASK((UChar32) ch) & value) != 0;
-}
-
 static UBool versionFilter(UChar32 ch, void* context) {
     static const UVersionInfo none = { 0, 0, 0, 0 };
     UVersionInfo v;
@@ -709,16 +721,6 @@ static UBool versionFilter(UChar32 ch, v
     return uprv_memcmp(&v, &none, sizeof(v)) > 0 && uprv_memcmp(&v, version, sizeof(v)) <= 0;
 }
 
-typedef struct {
-    UProperty prop;
-    int32_t value;
-} IntPropertyContext;
-
-static UBool intPropertyFilter(UChar32 ch, void* context) {
-    IntPropertyContext* c = (IntPropertyContext*)context;
-    return u_getIntPropertyValue((UChar32) ch, c->prop) == c->value;
-}
-
 static UBool scriptExtensionsFilter(UChar32 ch, void* context) {
     return uscript_hasScript(ch, *(UScriptCode*)context);
 }
@@ -779,6 +781,43 @@ void UnicodeSet::applyFilter(UnicodeSet:
 
 namespace {
 
+/** Maps map values to 1 if the mask contains their value'th bit, all others to 0. */
+uint32_t U_CALLCONV generalCategoryMaskFilter(const void *context, uint32_t value) {
+    uint32_t mask = *(const uint32_t *)context;
+    value = U_MASK(value) & mask;
+    if (value != 0) { value = 1; }
+    return value;
+}
+
+/** Maps one map value to 1, all others to 0. */
+uint32_t U_CALLCONV intValueFilter(const void *context, uint32_t value) {
+    uint32_t v = *(const uint32_t *)context;
+    return value == v ? 1 : 0;
+}
+
+}  // namespace
+
+void UnicodeSet::applyIntPropertyValue(const UCPMap *map,
+                                       UCPMapValueFilter *filter, const void *context,
+                                       UErrorCode &errorCode) {
+    if (U_FAILURE(errorCode)) { return; }
+    clear();
+    UChar32 start = 0, end;
+    uint32_t value;
+    while ((end = ucpmap_getRange(map, start, UCPMAP_RANGE_NORMAL, 0,
+                                  filter, context, &value)) >= 0) {
+        if (value != 0) {
+            add(start, end);
+        }
+        start = end + 1;
+    }
+    if (isBogus()) {
+        errorCode = U_MEMORY_ALLOCATION_ERROR;
+    }
+}
+
+namespace {
+
 static UBool mungeCharName(char* dst, const char* src, int32_t dstCapacity) {
     /* Note: we use ' ' in compiler code page */
     int32_t j = 0;
@@ -806,10 +845,11 @@ static UBool mungeCharName(char* dst, co
 
 UnicodeSet&
 UnicodeSet::applyIntPropertyValue(UProperty prop, int32_t value, UErrorCode& ec) {
-    if (U_FAILURE(ec) || isFrozen()) { return *this; }
+    if (U_FAILURE(ec)) { return *this; }
+    // All of the following check isFrozen() before modifying this set.
     if (prop == UCHAR_GENERAL_CATEGORY_MASK) {
-        const UnicodeSet* inclusions = CharacterProperties::getInclusionsForProperty(prop, ec);
-        applyFilter(generalCategoryMaskFilter, &value, inclusions, ec);
+        const UCPMap *map = u_getIntPropertyMap(UCHAR_GENERAL_CATEGORY, &ec);
+        applyIntPropertyValue(map, generalCategoryMaskFilter, &value, ec);
     } else if (prop == UCHAR_SCRIPT_EXTENSIONS) {
         const UnicodeSet* inclusions = CharacterProperties::getInclusionsForProperty(prop, ec);
         UScriptCode script = (UScriptCode)value;
@@ -826,11 +866,14 @@ UnicodeSet::applyIntPropertyValue(UPrope
             clear();
         }
     } else if (UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT) {
-        const UnicodeSet* inclusions = CharacterProperties::getInclusionsForProperty(prop, ec);
-        IntPropertyContext c = {prop, value};
-        applyFilter(intPropertyFilter, &c, inclusions, ec);
+        const UCPMap *map = u_getIntPropertyMap(prop, &ec);
+        applyIntPropertyValue(map, intValueFilter, &value, ec);
     } else {
+        // This code used to always call getInclusions(property source)
+        // which sets an error for an unsupported property.
         ec = U_ILLEGAL_ARGUMENT_ERROR;
+        // Otherwise we would just clear() this set because
+        // getIntPropertyValue(c, prop) returns 0 for all code points.
     }
     return *this;
 }
--- icu-63.2.orig/source/common/uprops.h
+++ icu-63.2/source/common/uprops.h
@@ -462,6 +462,7 @@ class UnicodeSet;
 class CharacterProperties {
 public:
     CharacterProperties() = delete;
+    static void U_CALLCONV initInclusion(UPropertySource src, UErrorCode &errorCode);
     static const UnicodeSet *getInclusionsForProperty(UProperty prop, UErrorCode &errorCode);
 };
 
--- icu-63.2.orig/source/common/uset.cpp
+++ icu-63.2/source/common/uset.cpp
@@ -249,7 +249,7 @@ class USetAccess /* not : public UObject
 public:
     /* Try to have the compiler inline these*/
     inline static int32_t getStringCount(const UnicodeSet& set) {
-        return set.stringsSize();
+        return set.getStringCount();
     }
     inline static const UnicodeString* getString(const UnicodeSet& set,
                                                  int32_t i) {
--- icu-63.2.orig/source/common/usetiter.cpp
+++ icu-63.2/source/common/usetiter.cpp
@@ -116,7 +116,7 @@ void UnicodeSetIterator::reset() {
         stringCount = 0;
     } else {
         endRange = set->getRangeCount() - 1;
-        stringCount = set->stringsSize();
+        stringCount = set->strings->size();
     }
     range = 0;
     endElement = -1;
--- icu-63.2.orig/source/test/intltest/convtest.cpp
+++ icu-63.2/source/test/intltest/convtest.cpp
@@ -606,7 +606,12 @@ ConversionTest::TestGetUnicodeSet2() {
                 // First try to see if we have different sets because ucnv_getUnicodeSet()
                 // added strings: The above conversion method does not tell us what strings might be convertible.
                 // Remove strings from the set and compare again.
-                set.removeAllStrings();
+                // Unfortunately, there are no good, direct set methods for finding out whether there are strings
+                // in the set, nor for enumerating or removing just them.
+                // Intersect all code points with the set. The intersection will not contain strings.
+                UnicodeSet temp(0, 0x10ffff);
+                temp.retainAll(set);
+                set=temp;
             }
             if(set!=expected) {
                 UnicodeSet diffSet;