Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: runtime/lib/function.cc

Issue 2983823002: Improve hashCode for closures (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | runtime/lib/function.dart » ('j') | runtime/vm/dart_entry.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/compiler.h" 7 #include "vm/compiler.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/exceptions.h" 9 #include "vm/exceptions.h"
10 #include "vm/native_entry.h" 10 #include "vm/native_entry.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 if ((receiver_a.raw() == receiver_b.raw()) && 61 if ((receiver_a.raw() == receiver_b.raw()) &&
62 (func_a.name() == func_b.name()) && 62 (func_a.name() == func_b.name()) &&
63 (func_a.Owner() == func_b.Owner())) { 63 (func_a.Owner() == func_b.Owner())) {
64 return Bool::True().raw(); 64 return Bool::True().raw();
65 } 65 }
66 } 66 }
67 } 67 }
68 return Bool::False().raw(); 68 return Bool::False().raw();
69 } 69 }
70 70
71 DEFINE_NATIVE_ENTRY(Closure_hashCode, 1) { 71 DEFINE_NATIVE_ENTRY(Closure_calculateHashCode, 1) {
rmacnak 2017/07/19 01:00:24 computeHash (Compare String and Type)
alexmarkov 2017/07/19 17:54:19 Done.
72 const Closure& receiver = 72 const Closure& receiver =
73 Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); 73 Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
74 const Function& func = Function::Handle(zone, receiver.function()); 74 const Function& func = Function::Handle(zone, receiver.function());
75 return func.GetClosureHashCode(); 75 intptr_t result = func.GetClosureHashCode();
76 if (func.IsImplicitInstanceClosureFunction()) {
77 // Calculate identityHashCode of cached closure receiver and
78 // combine it with function's hash code.
79 const Context& context = Context::Handle(zone, receiver.context());
80 const Object& recv = Object::Handle(zone, context.At(0));
81 ASSERT(recv.IsInstance());
82 const Object& recvHash =
83 Object::Handle(zone, Instance::Cast(recv).IdentityHashCode());
84 if (recvHash.IsInteger()) {
85 result = result ^ static_cast<intptr_t>(
rmacnak 2017/07/19 01:00:24 Please use CombineHashes and FinalizeHash
alexmarkov 2017/07/19 17:54:19 Done.
86 Integer::Cast(recvHash).AsTruncatedInt64Value());
87 }
88 }
89 // Finalize hash value like for strings so that it fits into a smi.
90 result += result << 3;
91 result ^= result >> 11;
92 result += result << 15;
93 result &= ((static_cast<intptr_t>(1) << String::kHashBits) - 1);
94 return Smi::New(result);
76 } 95 }
77 96
78 DEFINE_NATIVE_ENTRY(Closure_clone, 1) { 97 DEFINE_NATIVE_ENTRY(Closure_clone, 1) {
79 const Closure& receiver = 98 const Closure& receiver =
80 Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); 99 Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
81 const TypeArguments& instantiator_type_arguments = 100 const TypeArguments& instantiator_type_arguments =
82 TypeArguments::Handle(zone, receiver.instantiator_type_arguments()); 101 TypeArguments::Handle(zone, receiver.instantiator_type_arguments());
83 const TypeArguments& function_type_arguments = 102 const TypeArguments& function_type_arguments =
84 TypeArguments::Handle(zone, receiver.function_type_arguments()); 103 TypeArguments::Handle(zone, receiver.function_type_arguments());
85 const Function& function = Function::Handle(zone, receiver.function()); 104 const Function& function = Function::Handle(zone, receiver.function());
86 const Context& context = Context::Handle(zone, receiver.context()); 105 const Context& context = Context::Handle(zone, receiver.context());
87 Context& cloned_context = 106 Context& cloned_context =
88 Context::Handle(zone, Context::New(context.num_variables())); 107 Context::Handle(zone, Context::New(context.num_variables()));
89 cloned_context.set_parent(Context::Handle(zone, context.parent())); 108 cloned_context.set_parent(Context::Handle(zone, context.parent()));
90 Object& instance = Object::Handle(zone); 109 Object& instance = Object::Handle(zone);
91 for (int i = 0; i < context.num_variables(); i++) { 110 for (int i = 0; i < context.num_variables(); i++) {
92 instance = context.At(i); 111 instance = context.At(i);
93 cloned_context.SetAt(i, instance); 112 cloned_context.SetAt(i, instance);
94 } 113 }
95 return Closure::New(instantiator_type_arguments, function_type_arguments, 114 return Closure::New(instantiator_type_arguments, function_type_arguments,
96 function, cloned_context); 115 function, cloned_context);
97 } 116 }
98 117
99 } // namespace dart 118 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/function.dart » ('j') | runtime/vm/dart_entry.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698