Project

General

Profile

0001-refactor-a-data-structure-for-CMP_OPTIMIZABLE.patch

mame (Yusuke Endoh), 03/14/2016 01:28 PM

View differences:

array.c
2364 2364

  
2365 2365
struct ary_sort_data {
2366 2366
    VALUE ary;
2367
    int opt_methods;
2368
    int opt_inited;
2367
    struct cmp_opt_data cmp_opt;
2369 2368
};
2370 2369

  
2371 2370
static VALUE
......
2399 2398
    VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
2400 2399
    int n;
2401 2400

  
2402
    if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Fixnum)) {
2401
    if (FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data->cmp_opt, Fixnum)) {
2403 2402
	if ((long)a > (long)b) return 1;
2404 2403
	if ((long)a < (long)b) return -1;
2405 2404
	return 0;
2406 2405
    }
2407
    if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) {
2406
    if (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data->cmp_opt, String)) {
2408 2407
	return rb_str_cmp(a, b);
2409 2408
    }
2410 2409

  
......
2448 2447

  
2449 2448
	RBASIC_CLEAR_CLASS(tmp);
2450 2449
	data.ary = tmp;
2451
	data.opt_methods = 0;
2452
	data.opt_inited = 0;
2450
	data.cmp_opt.opt_methods = 0;
2451
	data.cmp_opt.opt_inited = 0;
2453 2452
	RARRAY_PTR_USE(tmp, ptr, {
2454 2453
	    ruby_qsort(ptr, len, sizeof(VALUE),
2455 2454
		       rb_block_given_p()?sort_1:sort_2, &data);
enum.c
1421 1421

  
1422 1422
struct min_t {
1423 1423
    VALUE min;
1424
    int opt_methods;
1425
    int opt_inited;
1424
    struct cmp_opt_data cmp_opt;
1426 1425
};
1427 1426

  
1428 1427
static VALUE
......
1436 1435
	memo->min = i;
1437 1436
    }
1438 1437
    else {
1439
	if (OPTIMIZED_CMP(i, memo->min, memo) < 0) {
1438
	if (OPTIMIZED_CMP(i, memo->min, memo->cmp_opt) < 0) {
1440 1439
	    memo->min = i;
1441 1440
	}
1442 1441
    }
......
1501 1500
       return nmin_run(obj, num, 0, 0);
1502 1501

  
1503 1502
    m->min = Qundef;
1504
    m->opt_methods = 0;
1505
    m->opt_inited = 0;
1503
    m->cmp_opt.opt_methods = 0;
1504
    m->cmp_opt.opt_inited = 0;
1506 1505
    if (rb_block_given_p()) {
1507 1506
	rb_block_call(obj, id_each, 0, 0, min_ii, memo);
1508 1507
    }
......
1516 1515

  
1517 1516
struct max_t {
1518 1517
    VALUE max;
1519
    int opt_methods;
1520
    int opt_inited;
1518
    struct cmp_opt_data cmp_opt;
1521 1519
};
1522 1520

  
1523 1521
static VALUE
......
1531 1529
	memo->max = i;
1532 1530
    }
1533 1531
    else {
1534
	if (OPTIMIZED_CMP(i, memo->max, memo) > 0) {
1532
	if (OPTIMIZED_CMP(i, memo->max, memo->cmp_opt) > 0) {
1535 1533
	    memo->max = i;
1536 1534
	}
1537 1535
    }
......
1595 1593
       return nmin_run(obj, num, 0, 1);
1596 1594

  
1597 1595
    m->max = Qundef;
1598
    m->opt_methods = 0;
1599
    m->opt_inited = 0;
1596
    m->cmp_opt.opt_methods = 0;
1597
    m->cmp_opt.opt_inited = 0;
1600 1598
    if (rb_block_given_p()) {
1601 1599
	rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
1602 1600
    }
......
1612 1610
    VALUE min;
1613 1611
    VALUE max;
1614 1612
    VALUE last;
1615
    int opt_methods;
1616
    int opt_inited;
1613
    struct cmp_opt_data cmp_opt;
1617 1614
};
1618 1615

  
1619 1616
static void
......
1626 1623
	memo->max = j;
1627 1624
    }
1628 1625
    else {
1629
	n = OPTIMIZED_CMP(i, memo->min, memo);
1626
	n = OPTIMIZED_CMP(i, memo->min, memo->cmp_opt);
1630 1627
	if (n < 0) {
1631 1628
	    memo->min = i;
1632 1629
	}
1633
	n = OPTIMIZED_CMP(j, memo->max, memo);
1630
	n = OPTIMIZED_CMP(j, memo->max, memo->cmp_opt);
1634 1631
	if (n > 0) {
1635 1632
	    memo->max = j;
1636 1633
	}
......
1653 1650
    j = memo->last;
1654 1651
    memo->last = Qundef;
1655 1652

  
1656
    n = OPTIMIZED_CMP(j, i, memo);
1653
    n = OPTIMIZED_CMP(j, i, memo->cmp_opt);
1657 1654
    if (n == 0)
1658 1655
        i = j;
1659 1656
    else if (n < 0) {
......
1743 1740

  
1744 1741
    m->min = Qundef;
1745 1742
    m->last = Qundef;
1746
    m->opt_methods = 0;
1747
    m->opt_inited = 0;
1743
    m->cmp_opt.opt_methods = 0;
1744
    m->cmp_opt.opt_inited = 0;
1748 1745
    if (rb_block_given_p()) {
1749 1746
	rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
1750 1747
	if (m->last != Qundef)
internal.h
685 685
    cmp_optimizable_count
686 686
};
687 687

  
688
struct cmp_opt_data {
689
    int opt_methods;
690
    int opt_inited;
691
};
692

  
688 693
#define CMP_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(cmp_opt_,type))
689 694
#define CMP_OPTIMIZABLE(data, type) \
690
    (((data)->opt_inited & CMP_OPTIMIZABLE_BIT(type)) ? \
691
     ((data)->opt_methods & CMP_OPTIMIZABLE_BIT(type)) : \
692
     (((data)->opt_inited |= CMP_OPTIMIZABLE_BIT(type)), \
695
    (((data).opt_inited & CMP_OPTIMIZABLE_BIT(type)) ? \
696
     ((data).opt_methods & CMP_OPTIMIZABLE_BIT(type)) : \
697
     (((data).opt_inited |= CMP_OPTIMIZABLE_BIT(type)), \
693 698
      rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
694
      ((data)->opt_methods |= CMP_OPTIMIZABLE_BIT(type))))
699
      ((data).opt_methods |= CMP_OPTIMIZABLE_BIT(type))))
695 700

  
696 701
/* ment is in method.h */
697 702

  
698
-