Merge pull request #32719 from nekomatata/dictionary-optimization

Optimized Dictionary hash() and duplicate() methods
This commit is contained in:
Rémi Verschelde 2019-10-26 23:08:27 +02:00 committed by GitHub
commit c994eaa852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 14 deletions

View File

@ -192,13 +192,9 @@ uint32_t Dictionary::hash() const {
uint32_t h = hash_djb2_one_32(Variant::DICTIONARY); uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
List<Variant> keys; for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
get_key_list(&keys); h = hash_djb2_one_32(E.key().hash(), h);
h = hash_djb2_one_32(E.value().hash(), h);
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
h = hash_djb2_one_32(E->get().hash(), h);
h = hash_djb2_one_32(operator[](E->get()).hash(), h);
} }
return h; return h;
@ -207,10 +203,11 @@ uint32_t Dictionary::hash() const {
Array Dictionary::keys() const { Array Dictionary::keys() const {
Array varr; Array varr;
varr.resize(size());
if (_p->variant_map.empty()) if (_p->variant_map.empty())
return varr; return varr;
varr.resize(size());
int i = 0; int i = 0;
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) { for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
varr[i] = E.key(); varr[i] = E.key();
@ -223,10 +220,11 @@ Array Dictionary::keys() const {
Array Dictionary::values() const { Array Dictionary::values() const {
Array varr; Array varr;
varr.resize(size());
if (_p->variant_map.empty()) if (_p->variant_map.empty())
return varr; return varr;
varr.resize(size());
int i = 0; int i = 0;
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) { for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
varr[i] = E.get(); varr[i] = E.get();
@ -255,11 +253,8 @@ Dictionary Dictionary::duplicate(bool p_deep) const {
Dictionary n; Dictionary n;
List<Variant> keys; for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
get_key_list(&keys); n[E.key()] = p_deep ? E.value().duplicate(true) : E.value();
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
n[E->get()] = p_deep ? operator[](E->get()).duplicate(p_deep) : operator[](E->get());
} }
return n; return n;