Here’s the string comparison function in runtime/str.c
.
CAMLprim value caml_string_compare(value s1, value s2)
{
mlsize_t len1, len2;
int res;
if (s1 == s2) return Val_int(0);
len1 = caml_string_length(s1);
len2 = caml_string_length(s2);
res = memcmp(String_val(s1), String_val(s2), len1 <= len2 ? len1 : len2);
if (res < 0) return Val_int(-1);
if (res > 0) return Val_int(1);
if (len1 < len2) return Val_int(-1);
if (len1 > len2) return Val_int(1);
return Val_int(0);
}
Why is the comparison of the two lengths done after the memcmp
call instead of beforehand? How does the performance of the two steps compare?