Do the extremely obvious optimization of checking if two pieces of data

are at the same location before bothering to compare them
This commit is contained in:
Bruce Hill 2024-09-05 10:31:35 -04:00
parent d3c4f613ac
commit 8d41b2b1fb
6 changed files with 21 additions and 3 deletions

View File

@ -21,10 +21,14 @@ public Text_t CString$as_text(const void *c_string, bool colorize, const TypeInf
return Text$concat(colorize ? Text("\x1b[34mCString\x1b[m(") : Text("CString("), Text$quoted(text, colorize), Text(")"));
}
public int CString$compare(const char **x, const char **y)
public int32_t CString$compare(const char **x, const char **y)
{
if (x == y)
return 0;
if (!*x != !*y)
return (!*y) - (!*x);
return strcmp(*x, *y);
}

View File

@ -122,6 +122,8 @@ public uint64_t generic_hash(const void *obj, const TypeInfo *type)
public int32_t generic_compare(const void *x, const void *y, const TypeInfo *type)
{
if (x == y) return 0;
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer$compare(x, y, type);
case TextInfo: return Text$compare(x, y);
@ -141,6 +143,8 @@ public int32_t generic_compare(const void *x, const void *y, const TypeInfo *typ
public bool generic_equal(const void *x, const void *y, const TypeInfo *type)
{
if (x == y) return true;
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer$equal(x, y, type);
case TextInfo: return Text$equal(x, y);

View File

@ -40,13 +40,13 @@ public Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type) {
public int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type) {
(void)type;
if (__builtin_expect(((x->small | y->small) & 1) == 0, 0))
return mpz_cmp(*x->big, *y->big);
return x->big == y->big ? 0 : mpz_cmp(*x->big, *y->big);
return (x->small > y->small) - (x->small < y->small);
}
public int32_t Int$compare_value(const Int_t x, const Int_t y) {
if (__builtin_expect(((x.small | y.small) & 1) == 0, 0))
return mpz_cmp(*x.big, *y.big);
return x.big == y.big ? 0 : mpz_cmp(*x.big, *y.big);
return (x.small > y.small) - (x.small < y.small);
}

View File

@ -18,6 +18,7 @@
static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo *type)
{
(void)type;
if (x == y) return 0;
int32_t diff = Int$compare(&x->first, &y->first, &$Int);
if (diff != 0) return diff;
diff = Int$compare(&x->last, &y->last, &$Int);
@ -28,6 +29,7 @@ static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo
static bool Range$equal(const Range_t *x, const Range_t *y, const TypeInfo *type)
{
(void)type;
if (x == y) return true;
return Int$equal(&x->first, &y->first, &$Int) && Int$equal(&x->last, &y->last, &$Int) && Int$equal(&x->step, &y->step, &$Int);
}

View File

@ -395,6 +395,8 @@ public table_t Table$sorted(table_t t, const TypeInfo *type)
public bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type)
{
if (x == y) return true;
assert(type->tag == TableInfo);
if (Table$length(*x) != Table$length(*y))
return false;
@ -407,6 +409,8 @@ public bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type
public int32_t Table$compare(const table_t *x, const table_t *y, const TypeInfo *type)
{
if (x == y) return 0;
assert(type->tag == TableInfo);
auto table = type->TableInfo;
if (x->entries.length == 0)

View File

@ -864,6 +864,8 @@ int32_t get_grapheme(Text_t text, int64_t index)
public int32_t Text$compare(const Text_t *a, const Text_t *b)
{
if (a == b) return 0;
int64_t len = MAX(a->length, b->length);
iteration_state_t a_state = {0, 0}, b_state = {0, 0};
for (int64_t i = 0; i < len; i++) {
@ -897,6 +899,8 @@ public int32_t Text$compare(const Text_t *a, const Text_t *b)
public bool Text$equal(const Text_t *a, const Text_t *b)
{
if (a == b) return true;
if (a->length != b->length || (a->hash != 0 && b->hash != 0 && a->hash != b->hash))
return false;
int64_t len = a->length;