Coverage Report

Created: 2024-09-08 06:23

/src/git/xdiff/xprepare.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  LibXDiff by Davide Libenzi ( File Differential Library )
3
 *  Copyright (C) 2003  Davide Libenzi
4
 *
5
 *  This library is free software; you can redistribute it and/or
6
 *  modify it under the terms of the GNU Lesser General Public
7
 *  License as published by the Free Software Foundation; either
8
 *  version 2.1 of the License, or (at your option) any later version.
9
 *
10
 *  This library is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 *  Lesser General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Lesser General Public
16
 *  License along with this library; if not, see
17
 *  <http://www.gnu.org/licenses/>.
18
 *
19
 *  Davide Libenzi <davidel@xmailserver.org>
20
 *
21
 */
22
23
#include "xinclude.h"
24
25
26
0
#define XDL_KPDIS_RUN 4
27
0
#define XDL_MAX_EQLIMIT 1024
28
0
#define XDL_SIMSCAN_WINDOW 100
29
0
#define XDL_GUESS_NLINES1 256
30
0
#define XDL_GUESS_NLINES2 20
31
32
33
typedef struct s_xdlclass {
34
  struct s_xdlclass *next;
35
  unsigned long ha;
36
  char const *line;
37
  long size;
38
  long idx;
39
  long len1, len2;
40
} xdlclass_t;
41
42
typedef struct s_xdlclassifier {
43
  unsigned int hbits;
44
  long hsize;
45
  xdlclass_t **rchash;
46
  chastore_t ncha;
47
  xdlclass_t **rcrecs;
48
  long alloc;
49
  long count;
50
  long flags;
51
} xdlclassifier_t;
52
53
54
55
56
static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
57
static void xdl_free_classifier(xdlclassifier_t *cf);
58
static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
59
             unsigned int hbits, xrecord_t *rec);
60
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
61
         xdlclassifier_t *cf, xdfile_t *xdf);
62
static void xdl_free_ctx(xdfile_t *xdf);
63
static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
64
static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
65
static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
66
static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
67
68
69
70
71
0
static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
72
0
  cf->flags = flags;
73
74
0
  cf->hbits = xdl_hashbits((unsigned int) size);
75
0
  cf->hsize = 1 << cf->hbits;
76
77
0
  if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
78
79
0
    return -1;
80
0
  }
81
0
  if (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) {
82
83
0
    xdl_cha_free(&cf->ncha);
84
0
    return -1;
85
0
  }
86
87
0
  cf->alloc = size;
88
0
  if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
89
90
0
    xdl_free(cf->rchash);
91
0
    xdl_cha_free(&cf->ncha);
92
0
    return -1;
93
0
  }
94
95
0
  cf->count = 0;
96
97
0
  return 0;
98
0
}
99
100
101
0
static void xdl_free_classifier(xdlclassifier_t *cf) {
102
103
0
  xdl_free(cf->rcrecs);
104
0
  xdl_free(cf->rchash);
105
0
  xdl_cha_free(&cf->ncha);
106
0
}
107
108
109
static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
110
0
             unsigned int hbits, xrecord_t *rec) {
111
0
  long hi;
112
0
  char const *line;
113
0
  xdlclass_t *rcrec;
114
115
0
  line = rec->ptr;
116
0
  hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
117
0
  for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
118
0
    if (rcrec->ha == rec->ha &&
119
0
        xdl_recmatch(rcrec->line, rcrec->size,
120
0
          rec->ptr, rec->size, cf->flags))
121
0
      break;
122
123
0
  if (!rcrec) {
124
0
    if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
125
126
0
      return -1;
127
0
    }
128
0
    rcrec->idx = cf->count++;
129
0
    if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc))
130
0
        return -1;
131
0
    cf->rcrecs[rcrec->idx] = rcrec;
132
0
    rcrec->line = line;
133
0
    rcrec->size = rec->size;
134
0
    rcrec->ha = rec->ha;
135
0
    rcrec->len1 = rcrec->len2 = 0;
136
0
    rcrec->next = cf->rchash[hi];
137
0
    cf->rchash[hi] = rcrec;
138
0
  }
139
140
0
  (pass == 1) ? rcrec->len1++ : rcrec->len2++;
141
142
0
  rec->ha = (unsigned long) rcrec->idx;
143
144
0
  hi = (long) XDL_HASHLONG(rec->ha, hbits);
145
0
  rec->next = rhash[hi];
146
0
  rhash[hi] = rec;
147
148
0
  return 0;
149
0
}
150
151
152
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
153
0
         xdlclassifier_t *cf, xdfile_t *xdf) {
154
0
  unsigned int hbits;
155
0
  long nrec, hsize, bsize;
156
0
  unsigned long hav;
157
0
  char const *blk, *cur, *top, *prev;
158
0
  xrecord_t *crec;
159
0
  xrecord_t **recs;
160
0
  xrecord_t **rhash;
161
0
  unsigned long *ha;
162
0
  char *rchg;
163
0
  long *rindex;
164
165
0
  ha = NULL;
166
0
  rindex = NULL;
167
0
  rchg = NULL;
168
0
  rhash = NULL;
169
0
  recs = NULL;
170
171
0
  if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
172
0
    goto abort;
173
0
  if (!XDL_ALLOC_ARRAY(recs, narec))
174
0
    goto abort;
175
176
0
  hbits = xdl_hashbits((unsigned int) narec);
177
0
  hsize = 1 << hbits;
178
0
  if (!XDL_CALLOC_ARRAY(rhash, hsize))
179
0
    goto abort;
180
181
0
  nrec = 0;
182
0
  if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
183
0
    for (top = blk + bsize; cur < top; ) {
184
0
      prev = cur;
185
0
      hav = xdl_hash_record(&cur, top, xpp->flags);
186
0
      if (XDL_ALLOC_GROW(recs, nrec + 1, narec))
187
0
        goto abort;
188
0
      if (!(crec = xdl_cha_alloc(&xdf->rcha)))
189
0
        goto abort;
190
0
      crec->ptr = prev;
191
0
      crec->size = (long) (cur - prev);
192
0
      crec->ha = hav;
193
0
      recs[nrec++] = crec;
194
0
      if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
195
0
        goto abort;
196
0
    }
197
0
  }
198
199
0
  if (!XDL_CALLOC_ARRAY(rchg, nrec + 2))
200
0
    goto abort;
201
202
0
  if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
203
0
      (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
204
0
    if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
205
0
      goto abort;
206
0
    if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
207
0
      goto abort;
208
0
  }
209
210
0
  xdf->nrec = nrec;
211
0
  xdf->recs = recs;
212
0
  xdf->hbits = hbits;
213
0
  xdf->rhash = rhash;
214
0
  xdf->rchg = rchg + 1;
215
0
  xdf->rindex = rindex;
216
0
  xdf->nreff = 0;
217
0
  xdf->ha = ha;
218
0
  xdf->dstart = 0;
219
0
  xdf->dend = nrec - 1;
220
221
0
  return 0;
222
223
0
abort:
224
0
  xdl_free(ha);
225
0
  xdl_free(rindex);
226
0
  xdl_free(rchg);
227
0
  xdl_free(rhash);
228
0
  xdl_free(recs);
229
0
  xdl_cha_free(&xdf->rcha);
230
0
  return -1;
231
0
}
232
233
234
0
static void xdl_free_ctx(xdfile_t *xdf) {
235
236
0
  xdl_free(xdf->rhash);
237
0
  xdl_free(xdf->rindex);
238
0
  xdl_free(xdf->rchg - 1);
239
0
  xdl_free(xdf->ha);
240
0
  xdl_free(xdf->recs);
241
0
  xdl_cha_free(&xdf->rcha);
242
0
}
243
244
245
int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
246
0
        xdfenv_t *xe) {
247
0
  long enl1, enl2, sample;
248
0
  xdlclassifier_t cf;
249
250
0
  memset(&cf, 0, sizeof(cf));
251
252
  /*
253
   * For histogram diff, we can afford a smaller sample size and
254
   * thus a poorer estimate of the number of lines, as the hash
255
   * table (rhash) won't be filled up/grown. The number of lines
256
   * (nrecs) will be updated correctly anyway by
257
   * xdl_prepare_ctx().
258
   */
259
0
  sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
260
0
      ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
261
262
0
  enl1 = xdl_guess_lines(mf1, sample) + 1;
263
0
  enl2 = xdl_guess_lines(mf2, sample) + 1;
264
265
0
  if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
266
0
    return -1;
267
268
0
  if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
269
270
0
    xdl_free_classifier(&cf);
271
0
    return -1;
272
0
  }
273
0
  if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
274
275
0
    xdl_free_ctx(&xe->xdf1);
276
0
    xdl_free_classifier(&cf);
277
0
    return -1;
278
0
  }
279
280
0
  if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
281
0
      (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
282
0
      xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
283
284
0
    xdl_free_ctx(&xe->xdf2);
285
0
    xdl_free_ctx(&xe->xdf1);
286
0
    xdl_free_classifier(&cf);
287
0
    return -1;
288
0
  }
289
290
0
  xdl_free_classifier(&cf);
291
292
0
  return 0;
293
0
}
294
295
296
0
void xdl_free_env(xdfenv_t *xe) {
297
298
0
  xdl_free_ctx(&xe->xdf2);
299
0
  xdl_free_ctx(&xe->xdf1);
300
0
}
301
302
303
0
static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
304
0
  long r, rdis0, rpdis0, rdis1, rpdis1;
305
306
  /*
307
   * Limits the window the is examined during the similar-lines
308
   * scan. The loops below stops when dis[i - r] == 1 (line that
309
   * has no match), but there are corner cases where the loop
310
   * proceed all the way to the extremities by causing huge
311
   * performance penalties in case of big files.
312
   */
313
0
  if (i - s > XDL_SIMSCAN_WINDOW)
314
0
    s = i - XDL_SIMSCAN_WINDOW;
315
0
  if (e - i > XDL_SIMSCAN_WINDOW)
316
0
    e = i + XDL_SIMSCAN_WINDOW;
317
318
  /*
319
   * Scans the lines before 'i' to find a run of lines that either
320
   * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
321
   * Note that we always call this function with dis[i] > 1, so the
322
   * current line (i) is already a multimatch line.
323
   */
324
0
  for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
325
0
    if (!dis[i - r])
326
0
      rdis0++;
327
0
    else if (dis[i - r] == 2)
328
0
      rpdis0++;
329
0
    else
330
0
      break;
331
0
  }
332
  /*
333
   * If the run before the line 'i' found only multimatch lines, we
334
   * return 0 and hence we don't make the current line (i) discarded.
335
   * We want to discard multimatch lines only when they appear in the
336
   * middle of runs with nomatch lines (dis[j] == 0).
337
   */
338
0
  if (rdis0 == 0)
339
0
    return 0;
340
0
  for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
341
0
    if (!dis[i + r])
342
0
      rdis1++;
343
0
    else if (dis[i + r] == 2)
344
0
      rpdis1++;
345
0
    else
346
0
      break;
347
0
  }
348
  /*
349
   * If the run after the line 'i' found only multimatch lines, we
350
   * return 0 and hence we don't make the current line (i) discarded.
351
   */
352
0
  if (rdis1 == 0)
353
0
    return 0;
354
0
  rdis1 += rdis0;
355
0
  rpdis1 += rpdis0;
356
357
0
  return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
358
0
}
359
360
361
/*
362
 * Try to reduce the problem complexity, discard records that have no
363
 * matches on the other file. Also, lines that have multiple matches
364
 * might be potentially discarded if they happear in a run of discardable.
365
 */
366
0
static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
367
0
  long i, nm, nreff, mlim;
368
0
  xrecord_t **recs;
369
0
  xdlclass_t *rcrec;
370
0
  char *dis, *dis1, *dis2;
371
372
0
  if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2))
373
0
    return -1;
374
0
  dis1 = dis;
375
0
  dis2 = dis1 + xdf1->nrec + 1;
376
377
0
  if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
378
0
    mlim = XDL_MAX_EQLIMIT;
379
0
  for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
380
0
    rcrec = cf->rcrecs[(*recs)->ha];
381
0
    nm = rcrec ? rcrec->len2 : 0;
382
0
    dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
383
0
  }
384
385
0
  if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
386
0
    mlim = XDL_MAX_EQLIMIT;
387
0
  for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
388
0
    rcrec = cf->rcrecs[(*recs)->ha];
389
0
    nm = rcrec ? rcrec->len1 : 0;
390
0
    dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
391
0
  }
392
393
0
  for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
394
0
       i <= xdf1->dend; i++, recs++) {
395
0
    if (dis1[i] == 1 ||
396
0
        (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
397
0
      xdf1->rindex[nreff] = i;
398
0
      xdf1->ha[nreff] = (*recs)->ha;
399
0
      nreff++;
400
0
    } else
401
0
      xdf1->rchg[i] = 1;
402
0
  }
403
0
  xdf1->nreff = nreff;
404
405
0
  for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
406
0
       i <= xdf2->dend; i++, recs++) {
407
0
    if (dis2[i] == 1 ||
408
0
        (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
409
0
      xdf2->rindex[nreff] = i;
410
0
      xdf2->ha[nreff] = (*recs)->ha;
411
0
      nreff++;
412
0
    } else
413
0
      xdf2->rchg[i] = 1;
414
0
  }
415
0
  xdf2->nreff = nreff;
416
417
0
  xdl_free(dis);
418
419
0
  return 0;
420
0
}
421
422
423
/*
424
 * Early trim initial and terminal matching records.
425
 */
426
0
static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
427
0
  long i, lim;
428
0
  xrecord_t **recs1, **recs2;
429
430
0
  recs1 = xdf1->recs;
431
0
  recs2 = xdf2->recs;
432
0
  for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
433
0
       i++, recs1++, recs2++)
434
0
    if ((*recs1)->ha != (*recs2)->ha)
435
0
      break;
436
437
0
  xdf1->dstart = xdf2->dstart = i;
438
439
0
  recs1 = xdf1->recs + xdf1->nrec - 1;
440
0
  recs2 = xdf2->recs + xdf2->nrec - 1;
441
0
  for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
442
0
    if ((*recs1)->ha != (*recs2)->ha)
443
0
      break;
444
445
0
  xdf1->dend = xdf1->nrec - i - 1;
446
0
  xdf2->dend = xdf2->nrec - i - 1;
447
448
0
  return 0;
449
0
}
450
451
452
0
static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
453
454
0
  if (xdl_trim_ends(xdf1, xdf2) < 0 ||
455
0
      xdl_cleanup_records(cf, xdf1, xdf2) < 0) {
456
457
0
    return -1;
458
0
  }
459
460
0
  return 0;
461
0
}