diff --git a/ChangeLog b/ChangeLog index 16d77fc006..62c95dd108 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Jul 20 18:39:18 2010 Nobuyoshi Nakada + + * include/ruby/ruby.h (RSTRING_GETMEM): new macro to get ptr and + len at once. + + * string.c (rb_str_cmp, str_eql, rb_str_eql): trivial improvements. + Tue Jul 20 18:23:04 2010 Nobuyoshi Nakada * ext/fiddle/closure.c (closure_data_type), diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 7fcbf33bda..936493c028 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -647,10 +647,12 @@ struct RString { #define RSTRING_NOEMBED FL_USER1 #define RSTRING_EMBED_LEN_MASK (FL_USER2|FL_USER3|FL_USER4|FL_USER5|FL_USER6) #define RSTRING_EMBED_LEN_SHIFT (FL_USHIFT+2) +#define RSTRING_EMBED_LEN(str) \ + (long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \ + (RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT)) #define RSTRING_LEN(str) \ (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \ - (long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \ - (RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT)) : \ + RSTRING_EMBED_LEN(str) : \ RSTRING(str)->as.heap.len) #define RSTRING_PTR(str) \ (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \ @@ -658,11 +660,13 @@ struct RString { RSTRING(str)->as.heap.ptr) #define RSTRING_END(str) \ (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \ - (RSTRING(str)->as.ary + \ - ((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \ - (RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT))) : \ + (RSTRING(str)->as.ary + RSTRING_EMBED_LEN(str)) : \ (RSTRING(str)->as.heap.ptr + RSTRING(str)->as.heap.len)) #define RSTRING_LENINT(str) rb_long2int(RSTRING_LEN(str)) +#define RSTRING_GETMEM(str, ptrvar, lenvar) \ + (!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \ + ((ptrvar) = RSTRING(str)->as.ary, (lenvar) = RSTRING_EMBED_LEN(str)) : \ + ((ptrvar) = RSTRING(str)->as.heap.ptr, (lenvar) = RSTRING(str)->as.heap.len)) #define RARRAY_EMBED_LEN_MAX 3 struct RArray { diff --git a/string.c b/string.c index 1b06370bb7..6610576de3 100644 --- a/string.c +++ b/string.c @@ -2100,13 +2100,15 @@ rb_str_comparable(VALUE str1, VALUE str2) int rb_str_cmp(VALUE str1, VALUE str2) { - long len; + long len1, len2; + const char *ptr1, *ptr2; int retval; - len = lesser(RSTRING_LEN(str1), RSTRING_LEN(str2)); - retval = memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len); - if (retval == 0) { - if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) { + if (str1 == str2) return 0; + RSTRING_GETMEM(str1, ptr1, len1); + RSTRING_GETMEM(str2, ptr2, len2); + if (ptr1 == ptr2 || (retval = memcmp(ptr1, ptr2, lesser(len1, len2))) == 0) { + if (len1 == len2) { if (!rb_str_comparable(str1, str2)) { if (ENCODING_GET(str1) > ENCODING_GET(str2)) return 1; @@ -2114,7 +2116,7 @@ rb_str_cmp(VALUE str1, VALUE str2) } return 0; } - if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return 1; + if (len1 > len2) return 1; return -1; } if (retval > 0) return 1; @@ -2126,10 +2128,13 @@ static VALUE str_eql(const VALUE str1, const VALUE str2) { const long len = RSTRING_LEN(str1); + const char *ptr1, *ptr2; if (len != RSTRING_LEN(str2)) return Qfalse; if (!rb_str_comparable(str1, str2)) return Qfalse; - if (memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len) == 0) + if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2))) + return Qtrue; + if (memcmp(ptr1, ptr2, len) == 0) return Qtrue; return Qfalse; } @@ -2165,6 +2170,7 @@ rb_str_equal(VALUE str1, VALUE str2) static VALUE rb_str_eql(VALUE str1, VALUE str2) { + if (str1 == str2) return Qtrue; if (TYPE(str2) != T_STRING) return Qfalse; return str_eql(str1, str2); }