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