зеркало из https://github.com/github/ruby.git
add rb_hash_new_with_size()
Sometimes, size of a hash can be calcluated a priori. By providing such info to the constructor we can avoid unnecessary internal re- allocations. This can boost for instance creation of hash literals. [Bug #13861] Signed-off-by: Urabe, Shyouhei <shyouhei@ruby-lang.org> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59744 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
e568455826
Коммит
0eb7359cc7
11
hash.c
11
hash.c
|
@ -432,6 +432,15 @@ rb_hash_new(void)
|
||||||
return hash_alloc(rb_cHash);
|
return hash_alloc(rb_cHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_hash_new_with_size(st_index_t size)
|
||||||
|
{
|
||||||
|
VALUE ret = rb_hash_new();
|
||||||
|
if (size)
|
||||||
|
RHASH(ret)->ntbl = st_init_table_with_size(&objhash, size);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
hash_dup(VALUE hash, VALUE klass, VALUE flags)
|
hash_dup(VALUE hash, VALUE klass, VALUE flags)
|
||||||
{
|
{
|
||||||
|
@ -1927,7 +1936,7 @@ rb_hash_transform_values(VALUE hash)
|
||||||
VALUE result;
|
VALUE result;
|
||||||
|
|
||||||
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
||||||
result = rb_hash_new();
|
result = rb_hash_new_with_size(RHASH_SIZE(hash));
|
||||||
if (!RHASH_EMPTY_P(hash)) {
|
if (!RHASH_EMPTY_P(hash)) {
|
||||||
rb_hash_foreach(hash, transform_values_i, result);
|
rb_hash_foreach(hash, transform_values_i, result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -496,7 +496,7 @@ newhash
|
||||||
{
|
{
|
||||||
RUBY_DTRACE_CREATE_HOOK(HASH, num);
|
RUBY_DTRACE_CREATE_HOOK(HASH, num);
|
||||||
|
|
||||||
val = rb_hash_new();
|
val = rb_hash_new_with_size(num / 2);
|
||||||
|
|
||||||
if (num) {
|
if (num) {
|
||||||
rb_hash_bulk_insert(num, STACK_ADDR_FROM_TOP(num), val);
|
rb_hash_bulk_insert(num, STACK_ADDR_FROM_TOP(num), val);
|
||||||
|
|
|
@ -1242,6 +1242,7 @@ void ruby_sized_xfree(void *x, size_t size);
|
||||||
|
|
||||||
/* hash.c */
|
/* hash.c */
|
||||||
struct st_table *rb_hash_tbl_raw(VALUE hash);
|
struct st_table *rb_hash_tbl_raw(VALUE hash);
|
||||||
|
VALUE rb_hash_new_with_size(st_index_t size);
|
||||||
VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
||||||
VALUE rb_hash_default_value(VALUE hash, VALUE key);
|
VALUE rb_hash_default_value(VALUE hash, VALUE key);
|
||||||
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc);
|
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc);
|
||||||
|
|
2
vm.c
2
vm.c
|
@ -2694,7 +2694,7 @@ m_core_hash_from_ary(VALUE self, VALUE ary)
|
||||||
static VALUE
|
static VALUE
|
||||||
core_hash_from_ary(VALUE ary)
|
core_hash_from_ary(VALUE ary)
|
||||||
{
|
{
|
||||||
VALUE hash = rb_hash_new();
|
VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary) / 2);
|
||||||
|
|
||||||
RUBY_DTRACE_CREATE_HOOK(HASH, (Check_Type(ary, T_ARRAY), RARRAY_LEN(ary)));
|
RUBY_DTRACE_CREATE_HOOK(HASH, (Check_Type(ary, T_ARRAY), RARRAY_LEN(ary)));
|
||||||
return core_hash_merge_ary(hash, ary);
|
return core_hash_merge_ary(hash, ary);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче