* range.c (range_include): specialize single character string

case (e.g. (?a ..?z).include(?x)) for performance.
  [ruby-core:15481]

* string.c (rb_str_upto): specialize single character case.

* string.c (rb_str_hash): omit coderange scan for performance.

* object.c (rb_check_to_integer): check Fixnum first.

* object.c (rb_to_integer): ditto.

* string.c (rb_str_equal): inline memcmp to avoid unnecessary
  rb_str_comparable(). 

* parse.y (rb_intern2): use US-ASCII encoding.

* parse.y (rb_intern_str): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15433 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2008-02-11 17:46:52 +00:00
Родитель 9d968bc221
Коммит 0472db84c5
6 изменённых файлов: 85 добавлений и 21 удалений

Просмотреть файл

@ -1,3 +1,24 @@
Tue Feb 12 02:42:27 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* range.c (range_include): specialize single character string
case (e.g. (?a ..?z).include(?x)) for performance.
[ruby-core:15481]
* string.c (rb_str_upto): specialize single character case.
* string.c (rb_str_hash): omit coderange scan for performance.
* object.c (rb_check_to_integer): check Fixnum first.
* object.c (rb_to_integer): ditto.
* string.c (rb_str_equal): inline memcmp to avoid unnecessary
rb_str_comparable().
* parse.y (rb_intern2): use US-ASCII encoding.
* parse.y (rb_intern_str): ditto.
Mon Feb 11 17:21:18 2008 Kouhei Sutou <kou@cozmixng.org> Mon Feb 11 17:21:18 2008 Kouhei Sutou <kou@cozmixng.org>
* lib/rss/rss.rb (RSS::VERSION), test/rss/test_version.rb: * lib/rss/rss.rb (RSS::VERSION), test/rss/test_version.rb:

Просмотреть файл

@ -1940,7 +1940,10 @@ rb_check_convert_type(VALUE val, int type, const char *tname, const char *method
static VALUE static VALUE
rb_to_integer(VALUE val, const char *method) rb_to_integer(VALUE val, const char *method)
{ {
VALUE v = convert_type(val, "Integer", method, Qtrue); VALUE v;
if (FIXNUM_P(val)) return val;
v = convert_type(val, "Integer", method, Qtrue);
if (!rb_obj_is_kind_of(v, rb_cInteger)) { if (!rb_obj_is_kind_of(v, rb_cInteger)) {
char *cname = rb_obj_classname(val); char *cname = rb_obj_classname(val);
rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)", rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
@ -1952,7 +1955,10 @@ rb_to_integer(VALUE val, const char *method)
VALUE VALUE
rb_check_to_integer(VALUE val, const char *method) rb_check_to_integer(VALUE val, const char *method)
{ {
VALUE v = convert_type(val, "Integer", method, Qfalse); VALUE v;
if (FIXNUM_P(val)) return val;
v = convert_type(val, "Integer", method, Qfalse);
if (!rb_obj_is_kind_of(v, rb_cInteger)) { if (!rb_obj_is_kind_of(v, rb_cInteger)) {
return Qnil; return Qnil;
} }

16
parse.y
Просмотреть файл

@ -271,7 +271,7 @@ struct parser_params {
#define STR_NEW0() rb_enc_str_new(0,0,rb_usascii_encoding()) #define STR_NEW0() rb_enc_str_new(0,0,rb_usascii_encoding())
#define STR_NEW2(p) rb_enc_str_new((p),strlen(p),parser->enc) #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),parser->enc)
#define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),parser->enc) #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),parser->enc)
#define STR_ENC(m) ((m)?parser->enc:rb_ascii8bit_encoding()) #define STR_ENC(m) ((m)?parser->enc:rb_usascii_encoding())
#define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT) #define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT)
#define TOK_INTERN(mb) rb_intern3(tok(), toklen(), STR_ENC(mb)) #define TOK_INTERN(mb) rb_intern3(tok(), toklen(), STR_ENC(mb))
@ -9043,7 +9043,7 @@ rb_intern3(const char *name, long len, rb_encoding *enc)
ID ID
rb_intern2(const char *name, long len) rb_intern2(const char *name, long len)
{ {
return rb_intern3(name, len, rb_ascii8bit_encoding()); return rb_intern3(name, len, rb_usascii_encoding());
} }
#undef rb_intern #undef rb_intern
@ -9056,14 +9056,16 @@ rb_intern(const char *name)
ID ID
rb_intern_str(VALUE str) rb_intern_str(VALUE str)
{ {
int idx = 0; rb_encoding *enc;
ID id; ID id;
if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
idx = rb_enc_get_index(str); enc = rb_usascii_encoding();
} }
id = rb_intern3(RSTRING_PTR(str), RSTRING_LEN(str), else {
rb_enc_from_index(idx)); enc = rb_enc_get(str);
}
id = rb_intern3(RSTRING_PTR(str), RSTRING_LEN(str), enc);
RB_GC_GUARD(str); RB_GC_GUARD(str);
return id; return id;
} }

22
range.c
Просмотреть файл

@ -10,6 +10,7 @@
**********************************************************************/ **********************************************************************/
#include "ruby/ruby.h" #include "ruby/ruby.h"
#include "ruby/encoding.h"
VALUE rb_cRange; VALUE rb_cRange;
static ID id_cmp, id_succ, id_beg, id_end, id_excl; static ID id_cmp, id_succ, id_beg, id_end, id_excl;
@ -748,6 +749,27 @@ range_include(VALUE range, VALUE val)
} }
return Qfalse; return Qfalse;
} }
else if (TYPE(beg) == T_STRING && TYPE(end) == T_STRING &&
RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
rb_encoding *enc = rb_enc_check(beg, end);
if (NIL_P(val)) return Qfalse;
if (TYPE(val) == T_STRING) {
if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
return Qfalse;
else {
char b = RSTRING_PTR(beg)[0];
char e = RSTRING_PTR(end)[0];
char v = RSTRING_PTR(val)[0];
if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
if (b <= v && v < e) return Qtrue;
if (!EXCL(range) && v == e) return Qtrue;
return Qfalse;
}
}
}
}
/* TODO: ruby_frame->this_func = rb_intern("include?"); */ /* TODO: ruby_frame->this_func = rb_intern("include?"); */
return rb_call_super(1, &val); return rb_call_super(1, &val);
} }

Просмотреть файл

@ -1509,11 +1509,7 @@ rb_memhash(const void *ptr, long len)
int int
rb_str_hash(VALUE str) rb_str_hash(VALUE str)
{ {
int e = ENCODING_GET(str); return hash((const void *)RSTRING_PTR(str), RSTRING_LEN(str), 0);
if (e && is_ascii_string(str)) {
e = 0;
}
return hash((const void *)RSTRING_PTR(str), RSTRING_LEN(str), e);
} }
/* /*
@ -1593,6 +1589,8 @@ rb_str_cmp(VALUE str1, VALUE str2)
VALUE VALUE
rb_str_equal(VALUE str1, VALUE str2) rb_str_equal(VALUE str1, VALUE str2)
{ {
int len;
if (str1 == str2) return Qtrue; if (str1 == str2) return Qtrue;
if (TYPE(str2) != T_STRING) { if (TYPE(str2) != T_STRING) {
if (!rb_respond_to(str2, rb_intern("to_str"))) { if (!rb_respond_to(str2, rb_intern("to_str"))) {
@ -1601,8 +1599,8 @@ rb_str_equal(VALUE str1, VALUE str2)
return rb_equal(str2, str1); return rb_equal(str2, str1);
} }
if (!rb_str_comparable(str1, str2)) return Qfalse; if (!rb_str_comparable(str1, str2)) return Qfalse;
if (RSTRING_LEN(str1) == RSTRING_LEN(str2) && if (RSTRING_LEN(str1) == (len = RSTRING_LEN(str2)) &&
rb_str_cmp(str1, str2) == 0) { memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len) == 0) {
return Qtrue; return Qtrue;
} }
return Qfalse; return Qfalse;
@ -2295,14 +2293,30 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
VALUE current, after_end; VALUE current, after_end;
ID succ; ID succ;
int n, excl; int n, excl;
rb_encoding *enc;
rb_scan_args(argc, argv, "11", &end, &exclusive); rb_scan_args(argc, argv, "11", &end, &exclusive);
rb_enc_check(beg, end);
excl = RTEST(exclusive); excl = RTEST(exclusive);
succ = rb_intern("succ"); succ = rb_intern("succ");
StringValue(end); StringValue(end);
enc = rb_enc_check(beg, end);
if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 &&
is_ascii_string(beg) && is_ascii_string(end)) {
char c = RSTRING_PTR(beg)[0];
char e = RSTRING_PTR(end)[0];
if (c > e || (excl && c == e)) return beg;
for (;;) {
rb_yield(rb_enc_str_new(&c, 1, enc));
if (!excl && c == e) break;
c++;
if (excl && c == e) break;
}
return beg;
}
n = rb_str_cmp(beg, end); n = rb_str_cmp(beg, end);
if (n > 0 || (excl && n == 0)) return beg; if (n > 0 || (excl && n == 0)) return beg;
after_end = rb_funcall(end, succ, 0, 0); after_end = rb_funcall(end, succ, 0, 0);
current = beg; current = beg;
while (!rb_str_equal(current, after_end)) { while (!rb_str_equal(current, after_end)) {
@ -2311,7 +2325,6 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
current = rb_funcall(current, succ, 0, 0); current = rb_funcall(current, succ, 0, 0);
StringValue(current); StringValue(current);
if (excl && rb_str_equal(current, end)) break; if (excl && rb_str_equal(current, end)) break;
StringValue(current);
if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0) if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
break; break;
} }

Просмотреть файл

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0" #define RUBY_VERSION "1.9.0"
#define RUBY_RELEASE_DATE "2008-02-11" #define RUBY_RELEASE_DATE "2008-02-12"
#define RUBY_VERSION_CODE 190 #define RUBY_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20080211 #define RUBY_RELEASE_CODE 20080212
#define RUBY_PATCHLEVEL 0 #define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0 #define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 2 #define RUBY_RELEASE_MONTH 2
#define RUBY_RELEASE_DAY 11 #define RUBY_RELEASE_DAY 12
#ifdef RUBY_EXTERN #ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_version[];