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

Unified Diff: vm/dart_api_impl.cc

Issue 10386060: Speed up the Dart_IsBlah functions by checking the class index. This (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: vm/dart_api_impl.cc
===================================================================
--- vm/dart_api_impl.cc (revision 7467)
+++ vm/dart_api_impl.cc (working copy)
@@ -42,19 +42,19 @@
}
}
-#define RETURN_TYPE_ERROR(isolate, dart_handle, Type) \
- do { \
- const Object& tmp = \
- Object::Handle(isolate, Api::UnwrapHandle((dart_handle))); \
- if (tmp.IsNull()) { \
- return Api::NewError("%s expects argument '%s' to be non-null.", \
- CURRENT_FUNC, #dart_handle); \
- } else if (tmp.IsError()) { \
- return dart_handle; \
- } else { \
- return Api::NewError("%s expects argument '%s' to be of type %s.", \
- CURRENT_FUNC, #dart_handle, #Type); \
- } \
+#define RETURN_TYPE_ERROR(isolate, dart_handle, Type) \
+ do { \
+ const Object& tmp = \
+ Object::Handle(isolate, Api::UnwrapHandle((dart_handle))); \
+ if (tmp.IsNull()) { \
+ return Api::NewError("%s expects argument '%s' to be non-null.", \
+ CURRENT_FUNC, #dart_handle); \
+ } else if (tmp.IsError()) { \
+ return dart_handle; \
+ } else { \
+ return Api::NewError("%s expects argument '%s' to be of type %s.", \
+ CURRENT_FUNC, #dart_handle, #Type); \
+ } \
} while (0)
@@ -120,15 +120,15 @@
return *(reinterpret_cast<RawObject**>(object));
}
-#define DEFINE_UNWRAP(Type) \
- const Type& Api::Unwrap##Type##Handle(Isolate* iso, \
- Dart_Handle dart_handle) { \
- const Object& tmp = Object::Handle(iso, Api::UnwrapHandle(dart_handle)); \
- Type& typed_handle = Type::Handle(iso); \
- if (tmp.Is##Type()) { \
- typed_handle ^= tmp.raw(); \
- } \
- return typed_handle; \
+#define DEFINE_UNWRAP(Type) \
+ const Type& Api::Unwrap##Type##Handle(Isolate* iso, \
+ Dart_Handle dart_handle) { \
+ const Object& tmp = Object::Handle(iso, Api::UnwrapHandle(dart_handle)); \
+ Type& typed_handle = Type::Handle(iso); \
+ if (tmp.Is##Type()) { \
+ typed_handle ^= tmp.raw(); \
+ } \
+ return typed_handle; \
}
CLASS_LIST_NO_OBJECT(DEFINE_UNWRAP)
#undef DEFINE_UNWRAP
@@ -259,10 +259,9 @@
DART_EXPORT bool Dart_IsError(Dart_Handle handle) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
- return obj.IsError();
+ ASSERT(kError + 5 == kInstance); // There are 4 subtypes of Error.
Ivan Posva 2012/05/09 23:28:16 This is brittle in case we delete one of the error
turnidge 2012/05/10 17:49:38 I have moved all of these predicates to raw_object
+ intptr_t class_index = Api::ClassIndex(handle);
+ return (class_index >= kError && class_index < kInstance);
}
@@ -374,9 +373,9 @@
DART_EXPORT void _Dart_ReportErrorHandle(const char* file,
- int line,
- const char* handle,
- const char* message) {
+ int line,
+ const char* handle,
+ const char* message) {
fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n",
file, line, handle, message);
OS::Abort();
@@ -1010,10 +1009,8 @@
DART_EXPORT bool Dart_IsNull(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsNull();
+ ASSERT(kNullClassIndex + 1 == kDynamicClassIndex); // No subtypes.
+ return Api::ClassIndex(object) == kNullClassIndex;
}
@@ -1083,10 +1080,9 @@
// TODO(iposva): The argument should be an instance.
DART_EXPORT bool Dart_IsNumber(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsNumber();
+ ASSERT(kNumber + 6 == kString); // There are 5 subtypes of Number.
Ivan Posva 2012/05/09 23:28:16 ditto.
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index >= kNumber && class_index < kString);
}
@@ -1094,24 +1090,19 @@
DART_EXPORT bool Dart_IsInteger(Dart_Handle object) {
- // Fast path for Smis.
- if (Api::IsSmi(object)) {
- return true;
- }
-
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsInteger();
+ ASSERT(kInteger + 4 == kDouble); // There are 3 subtypes of Integer.
Ivan Posva 2012/05/09 23:28:16 ditto.
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index >= kInteger && class_index < kDouble);
}
DART_EXPORT Dart_Handle Dart_IntegerFitsIntoInt64(Dart_Handle integer,
bool* fits) {
- // Fast path for Smis.
+ // Fast path for Smis and Mints.
Isolate* isolate = Isolate::Current();
CHECK_ISOLATE(isolate);
- if (Api::IsSmi(integer)) {
+ intptr_t class_index = Api::ClassIndex(integer);
+ if (class_index == kSmi || class_index == kMint) {
*fits = true;
return Api::Success(isolate);
}
@@ -1121,17 +1112,13 @@
if (int_obj.IsNull()) {
RETURN_TYPE_ERROR(isolate, integer, Integer);
}
- if (int_obj.IsSmi() || int_obj.IsMint()) {
- *fits = true;
- } else {
- ASSERT(int_obj.IsBigint());
+ ASSERT(int_obj.IsBigint());
#if defined(DEBUG)
- Bigint& bigint = Bigint::Handle(isolate);
- bigint ^= int_obj.raw();
- ASSERT(!BigintOperations::FitsIntoMint(bigint));
+ Bigint& bigint = Bigint::Handle(isolate);
+ bigint ^= int_obj.raw();
+ ASSERT(!BigintOperations::FitsIntoMint(bigint));
#endif
- *fits = false;
- }
+ *fits = false;
return Api::Success(isolate);
}
@@ -1298,10 +1285,8 @@
DART_EXPORT bool Dart_IsBoolean(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsBool();
+ ASSERT(kBool + 1 == kArray); // No subtypes.
turnidge 2012/05/10 17:49:38 I dropped the asserts for types with no subtypes.
+ return Api::ClassIndex(object) == kBool;
}
@@ -1329,10 +1314,8 @@
DART_EXPORT bool Dart_IsDouble(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsDouble();
+ ASSERT(kDouble + 1 == kString); // No subtypes.
+ return Api::ClassIndex(object) == kDouble;
}
@@ -1360,27 +1343,25 @@
DART_EXPORT bool Dart_IsString(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsString();
+ ASSERT(kString + 7 == kBool); // There are 6 subtypes of String.
Ivan Posva 2012/05/09 23:28:16 ditto.
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index >= kString && class_index < kBool);
}
DART_EXPORT bool Dart_IsString8(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsOneByteString() || obj.IsExternalOneByteString();
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index == kOneByteString ||
+ class_index == kExternalOneByteString);
}
DART_EXPORT bool Dart_IsString16(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return (obj.IsOneByteString() || obj.IsExternalOneByteString() ||
- obj.IsTwoByteString() || obj.IsExternalTwoByteString());
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index == kOneByteString ||
+ class_index == kTwoByteString ||
+ class_index == kExternalOneByteString ||
+ class_index == kExternalTwoByteString);
}
@@ -1430,13 +1411,10 @@
DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const String& str = Api::UnwrapStringHandle(isolate, object);
- if (str.IsNull()) {
- return false;
- }
- return str.IsExternal();
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index == kExternalOneByteString ||
+ class_index == kExternalTwoByteString ||
+ class_index == kExternalFourByteString);
}
@@ -1663,11 +1641,17 @@
DART_EXPORT bool Dart_IsList(Dart_Handle object) {
+ ASSERT(kArray + 2 == kGrowableObjectArray); // There is 1 subtype of Array.
Ivan Posva 2012/05/09 23:28:16 ditto.
+ ASSERT(kGrowableObjectArray + 1 == kByteArray); // No subtypes.
+ intptr_t class_index = Api::ClassIndex(object);
+ if (class_index >= kArray && class_index < kByteArray) {
+ return true;
+ }
+
Isolate* isolate = Isolate::Current();
DARTSCOPE(isolate);
const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return (obj.IsArray() || obj.IsGrowableObjectArray() ||
- (GetListInstance(isolate, obj) != Instance::null()));
+ return GetListInstance(isolate, obj) != Instance::null();
}
@@ -2012,10 +1996,9 @@
DART_EXPORT bool Dart_IsByteArray(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsByteArray();
+ ASSERT(kByteArray + 21 == kClosure); // There are 20 subtypes of ByteArray
Ivan Posva 2012/05/09 23:28:16 ditto.
+ intptr_t class_index = Api::ClassIndex(object);
+ return (class_index >= kByteArray && class_index <= kClosure);
}
@@ -2243,6 +2226,8 @@
DART_EXPORT bool Dart_IsClosure(Dart_Handle object) {
+ // We can't use a fast class index check here because there are many
+ // different signature classes for closures.
Isolate* isolate = Isolate::Current();
DARTSCOPE(isolate);
const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
@@ -3126,10 +3111,8 @@
DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) {
- Isolate* isolate = Isolate::Current();
- DARTSCOPE(isolate);
- const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
- return obj.IsLibrary();
+ ASSERT(kLibrary + 1 == kLibraryPrefix); // No subtypes.
+ return Api::ClassIndex(object) == kLibrary;
}

Powered by Google App Engine
This is Rietveld 408576698