Show More
@@ -2169,11 +2169,36 b' def optimize(x, small):' | |||
|
2169 | 2169 | return w, (op, tb, ta) |
|
2170 | 2170 | return w, (op, ta, tb) |
|
2171 | 2171 | elif op == 'or': |
|
2172 | ws, ts = zip(*[optimize(y, False) for y in x[1:]]) | |
|
2172 | # fast path for machine-generated expression, that is likely to have | |
|
2173 | # lots of trivial revisions: 'a + b + c()' to '_list(a b) + c()' | |
|
2174 | ws, ts, ss = [], [], [] | |
|
2175 | def flushss(): | |
|
2176 | if not ss: | |
|
2177 | return | |
|
2178 | if len(ss) == 1: | |
|
2179 | w, t = ss[0] | |
|
2180 | else: | |
|
2181 | s = '\0'.join(t[1] for w, t in ss) | |
|
2182 | y = ('func', ('symbol', '_list'), ('string', s)) | |
|
2183 | w, t = optimize(y, False) | |
|
2184 | ws.append(w) | |
|
2185 | ts.append(t) | |
|
2186 | del ss[:] | |
|
2187 | for y in x[1:]: | |
|
2188 | w, t = optimize(y, False) | |
|
2189 | if t[0] == 'string' or t[0] == 'symbol': | |
|
2190 | ss.append((w, t)) | |
|
2191 | continue | |
|
2192 | flushss() | |
|
2193 | ws.append(w) | |
|
2194 | ts.append(t) | |
|
2195 | flushss() | |
|
2196 | if len(ts) == 1: | |
|
2197 | return ws[0], ts[0] # 'or' operation is fully optimized out | |
|
2173 | 2198 | # we can't reorder trees by weight because it would change the order. |
|
2174 | 2199 | # ("sort(a + b)" == "sort(b + a)", but "a + b" != "b + a") |
|
2175 | 2200 | # ts = tuple(t for w, t in sorted(zip(ws, ts), key=lambda wt: wt[0])) |
|
2176 | return max(ws), (op,) + ts | |
|
2201 | return max(ws), (op,) + tuple(ts) | |
|
2177 | 2202 | elif op == 'not': |
|
2178 | 2203 | # Optimize not public() to _notpublic() because we have a fast version |
|
2179 | 2204 | if x[1] == ('func', ('symbol', 'public'), None): |
@@ -132,11 +132,7 b' trivial' | |||
|
132 | 132 | ('symbol', '1') |
|
133 | 133 | ('symbol', '2')) |
|
134 | 134 | * set: |
|
135 | <addset | |
|
136 | <baseset [0]>, | |
|
137 | <addset | |
|
138 | <baseset [1]>, | |
|
139 | <baseset [2]>>> | |
|
135 | <baseset [0, 1, 2]> | |
|
140 | 136 | 0 |
|
141 | 137 | 1 |
|
142 | 138 | 2 |
@@ -272,9 +268,7 b' quoting needed' | |||
|
272 | 268 | * set: |
|
273 | 269 | <addset |
|
274 | 270 | <baseset [1]>, |
|
275 | <addset | |
|
276 | <baseset [2]>, | |
|
277 | <baseset [3]>>> | |
|
271 | <baseset [2, 3]>> | |
|
278 | 272 | 1 |
|
279 | 273 | 2 |
|
280 | 274 | 3 |
@@ -909,6 +903,114 b' test that `or` operation skips duplicate' | |||
|
909 | 903 | 4 |
|
910 | 904 | 5 |
|
911 | 905 | |
|
906 | test optimization of trivial `or` operation | |
|
907 | ||
|
908 | $ try --optimize '0|(1)|"2"|-2|tip|null' | |
|
909 | (or | |
|
910 | ('symbol', '0') | |
|
911 | (group | |
|
912 | ('symbol', '1')) | |
|
913 | ('string', '2') | |
|
914 | (negate | |
|
915 | ('symbol', '2')) | |
|
916 | ('symbol', 'tip') | |
|
917 | ('symbol', 'null')) | |
|
918 | * optimized: | |
|
919 | (func | |
|
920 | ('symbol', '_list') | |
|
921 | ('string', '0\x001\x002\x00-2\x00tip\x00null')) | |
|
922 | * set: | |
|
923 | <baseset [0, 1, 2, 8, 9, -1]> | |
|
924 | 0 | |
|
925 | 1 | |
|
926 | 2 | |
|
927 | 8 | |
|
928 | 9 | |
|
929 | -1 | |
|
930 | ||
|
931 | $ try --optimize '0|1|2:3' | |
|
932 | (or | |
|
933 | ('symbol', '0') | |
|
934 | ('symbol', '1') | |
|
935 | (range | |
|
936 | ('symbol', '2') | |
|
937 | ('symbol', '3'))) | |
|
938 | * optimized: | |
|
939 | (or | |
|
940 | (func | |
|
941 | ('symbol', '_list') | |
|
942 | ('string', '0\x001')) | |
|
943 | (range | |
|
944 | ('symbol', '2') | |
|
945 | ('symbol', '3'))) | |
|
946 | * set: | |
|
947 | <addset | |
|
948 | <baseset [0, 1]>, | |
|
949 | <spanset+ 2:3>> | |
|
950 | 0 | |
|
951 | 1 | |
|
952 | 2 | |
|
953 | 3 | |
|
954 | ||
|
955 | $ try --optimize '0:1|2|3:4|5|6' | |
|
956 | (or | |
|
957 | (range | |
|
958 | ('symbol', '0') | |
|
959 | ('symbol', '1')) | |
|
960 | ('symbol', '2') | |
|
961 | (range | |
|
962 | ('symbol', '3') | |
|
963 | ('symbol', '4')) | |
|
964 | ('symbol', '5') | |
|
965 | ('symbol', '6')) | |
|
966 | * optimized: | |
|
967 | (or | |
|
968 | (range | |
|
969 | ('symbol', '0') | |
|
970 | ('symbol', '1')) | |
|
971 | ('symbol', '2') | |
|
972 | (range | |
|
973 | ('symbol', '3') | |
|
974 | ('symbol', '4')) | |
|
975 | (func | |
|
976 | ('symbol', '_list') | |
|
977 | ('string', '5\x006'))) | |
|
978 | * set: | |
|
979 | <addset | |
|
980 | <addset | |
|
981 | <spanset+ 0:1>, | |
|
982 | <baseset [2]>>, | |
|
983 | <addset | |
|
984 | <spanset+ 3:4>, | |
|
985 | <baseset [5, 6]>>> | |
|
986 | 0 | |
|
987 | 1 | |
|
988 | 2 | |
|
989 | 3 | |
|
990 | 4 | |
|
991 | 5 | |
|
992 | 6 | |
|
993 | ||
|
994 | test that `_list` should be narrowed by provided `subset` | |
|
995 | ||
|
996 | $ log '0:2 and (null|1|2|3)' | |
|
997 | 1 | |
|
998 | 2 | |
|
999 | ||
|
1000 | test that `_list` should remove duplicates | |
|
1001 | ||
|
1002 | $ log '0|1|2|1|2|-1|tip' | |
|
1003 | 0 | |
|
1004 | 1 | |
|
1005 | 2 | |
|
1006 | 9 | |
|
1007 | ||
|
1008 | test unknown revision in `_list` | |
|
1009 | ||
|
1010 | $ log '0|unknown' | |
|
1011 | abort: unknown revision 'unknown'! | |
|
1012 | [255] | |
|
1013 | ||
|
912 | 1014 | test that chained `or` operations make balanced addsets |
|
913 | 1015 | |
|
914 | 1016 | $ try '0:1|1:2|2:3|3:4|4:5' |
@@ -1367,9 +1469,7 b' test infinite recursion' | |||
|
1367 | 1469 | * set: |
|
1368 | 1470 | <addset |
|
1369 | 1471 | <baseset [3]>, |
|
1370 | <addset | |
|
1371 | <baseset [1]>, | |
|
1372 | <baseset [2]>>> | |
|
1472 | <baseset [1, 2]>> | |
|
1373 | 1473 | 3 |
|
1374 | 1474 | 1 |
|
1375 | 1475 | 2 |
General Comments 0
You need to be logged in to leave comments.
Login now