xdiff: delete redundant array xdfile_t.ha
When 0 <= i < xdfile_t.nreff the following is true: xdfile_t.ha[i] == xdfile_t.recs[xdfile_t.rindex[i]] This makes the code about 5% slower. The fields rindex and ha are specific to the classic diff (myers and minimal). I plan on creating a struct for classic diff, but there's a lot of cleanup that needs to be done before that can happen and leaving ha in would make those cleanups harder to follow. A subsequent commit will delete the chastore cha from xdfile_t. That later commit will investigate deleting ha and cha independently and together. Signed-off-by: Ezekiel Newren <ezekielnewren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ezekiel Newren committed
Sep 26, 2025 at 22:41 UTC
5c294dceb23633a8bcced946ce3f65a06038cf52
3 files changed
+16
-21
xdiff/xdiffi.c
+14
-10
@@ -22,6 +22,11 @@
22
23
#include "xinclude.h"
24
25
+static unsigned long get_hash(xdfile_t *xdf, long index)
26
+{
27
+ return xdf->recs[xdf->rindex[index]]->ha;
28
+}
29
+
30
#define XDL_MAX_COST_MIN 256
31
#define XDL_HEUR_MIN_COST 256
32
#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1)
@@ -42,8 +47,8 @@ typedef struct s_xdpsplit {
47
* using this algorithm, so a little bit of heuristic is needed to cut the
48
* search and to return a suboptimal point.
49
*/
45
-static long xdl_split(unsigned long const *ha1, long off1, long lim1,
46
- unsigned long const *ha2, long off2, long lim2,
50
+static long xdl_split(xdfile_t *xdf1, long off1, long lim1,
51
+ xdfile_t *xdf2, long off2, long lim2,
52
long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
53
xdalgoenv_t *xenv) {
54
long dmin = off1 - lim2, dmax = lim1 - off2;
@@ -87,7 +92,7 @@ static long xdl_split(unsigned long const *ha1, long off1, long lim1,
92
i1 = kvdf[d + 1];
93
prev1 = i1;
94
i2 = i1 - d;
90
- for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
95
+ for (; i1 < lim1 && i2 < lim2 && get_hash(xdf1, i1) == get_hash(xdf2, i2); i1++, i2++);
96
if (i1 - prev1 > xenv->snake_cnt)
97
got_snake = 1;
98
kvdf[d] = i1;
@@ -124,7 +129,7 @@ static long xdl_split(unsigned long const *ha1, long off1, long lim1,
129
i1 = kvdb[d + 1] - 1;
130
prev1 = i1;
131
i2 = i1 - d;
127
- for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
132
+ for (; i1 > off1 && i2 > off2 && get_hash(xdf1, i1 - 1) == get_hash(xdf2, i2 - 1); i1--, i2--);
133
if (prev1 - i1 > xenv->snake_cnt)
134
got_snake = 1;
135
kvdb[d] = i1;
@@ -159,7 +164,7 @@ static long xdl_split(unsigned long const *ha1, long off1, long lim1,
164
if (v > XDL_K_HEUR * ec && v > best &&
165
off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
166
off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
162
- for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
167
+ for (k = 1; get_hash(xdf1, i1 - k) == get_hash(xdf2, i2 - k); k++)
168
if (k == xenv->snake_cnt) {
169
best = v;
170
spl->i1 = i1;
@@ -183,7 +188,7 @@ static long xdl_split(unsigned long const *ha1, long off1, long lim1,
188
if (v > XDL_K_HEUR * ec && v > best &&
189
off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
190
off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
186
- for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
191
+ for (k = 0; get_hash(xdf1, i1 + k) == get_hash(xdf2, i2 + k); k++)
192
if (k == xenv->snake_cnt - 1) {
193
best = v;
194
spl->i1 = i1;
@@ -260,13 +265,12 @@ static long xdl_split(unsigned long const *ha1, long off1, long lim1,
265
int xdl_recs_cmp(xdfile_t *xdf1, long off1, long lim1,
266
xdfile_t *xdf2, long off2, long lim2,
267
long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
263
- unsigned long const *ha1 = xdf1->ha, *ha2 = xdf2->ha;
268
269
/*
270
* Shrink the box by walking through each diagonal snake (SW and NE).
271
*/
268
- for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
269
- for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
272
+ for (; off1 < lim1 && off2 < lim2 && get_hash(xdf1, off1) == get_hash(xdf2, off2); off1++, off2++);
273
+ for (; off1 < lim1 && off2 < lim2 && get_hash(xdf1, lim1 - 1) == get_hash(xdf2, lim2 - 1); lim1--, lim2--);
274
275
/*
276
* If one dimension is empty, then all records on the other one must
@@ -285,7 +289,7 @@ int xdl_recs_cmp(xdfile_t *xdf1, long off1, long lim1,
289
/*
290
* Divide ...
291
*/
288
- if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
292
+ if (xdl_split(xdf1, off1, lim1, xdf2, off2, lim2, kvdf, kvdb,
293
need_min, &spl, xenv) < 0) {
294
295
return -1;
xdiff/xprepare.c
+2
-10
@@ -133,7 +133,6 @@ static void xdl_free_ctx(xdfile_t *xdf)
133
{
134
xdl_free(xdf->rindex);
135
xdl_free(xdf->rchg - 1);
136
- xdl_free(xdf->ha);
136
xdl_free(xdf->recs);
137
xdl_cha_free(&xdf->rcha);
138
}
@@ -146,7 +145,6 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
145
char const *blk, *cur, *top, *prev;
146
xrecord_t *crec;
147
149
- xdf->ha = NULL;
148
xdf->rindex = NULL;
149
xdf->rchg = NULL;
150
xdf->recs = NULL;
@@ -181,8 +179,6 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
179
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
180
if (!XDL_ALLOC_ARRAY(xdf->rindex, xdf->nrec + 1))
181
goto abort;
184
- if (!XDL_ALLOC_ARRAY(xdf->ha, xdf->nrec + 1))
185
- goto abort;
182
}
183
184
xdf->rchg += 1;
@@ -300,9 +296,7 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
296
i <= xdf1->dend; i++, recs++) {
297
if (dis1[i] == 1 ||
298
(dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
303
- xdf1->rindex[nreff] = i;
304
- xdf1->ha[nreff] = (*recs)->ha;
305
- nreff++;
299
+ xdf1->rindex[nreff++] = i;
300
} else
301
xdf1->rchg[i] = 1;
302
}
@@ -312,9 +306,7 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd
306
i <= xdf2->dend; i++, recs++) {
307
if (dis2[i] == 1 ||
308
(dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
315
- xdf2->rindex[nreff] = i;
316
- xdf2->ha[nreff] = (*recs)->ha;
317
- nreff++;
309
+ xdf2->rindex[nreff++] = i;
310
} else
311
xdf2->rchg[i] = 1;
312
}
xdiff/xtypes.h
-1
@@ -52,7 +52,6 @@ typedef struct s_xdfile {
52
char *rchg;
53
long *rindex;
54
long nreff;
55
- unsigned long *ha;
55
} xdfile_t;
56
57
typedef struct s_xdfenv {