aboutsummaryrefslogtreecommitdiffstats
path: root/src/v8/0001-Add-hashing-and-comparison-methods-to-v8-String.patch
blob: 9f4abbc5e54a3cad9569db91dfd4a38fb2d1ca18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
From be15626304572957f432160bb0fa35af5e6663b2 Mon Sep 17 00:00:00 2001
From: Aaron Kennedy <aaron.kennedy@nokia.com>
Date: Mon, 23 May 2011 15:47:20 +1000
Subject: [PATCH 1/7] Add hashing and comparison methods to v8::String

This allows us to more rapidly search for a v8::String inside
a hash of QStrings.
---
 include/v8.h   |   17 +++++++++++++++++
 src/api.cc     |   15 +++++++++++++++
 src/objects.cc |   29 +++++++++++++++++++++++++++++
 src/objects.h  |    2 ++
 4 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/include/v8.h b/include/v8.h
index d15d024..ac3c062 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -994,6 +994,23 @@ class String : public Primitive {
   V8EXPORT int Utf8Length() const;
 
   /**
+   * Returns the hash of this string.
+   */
+  V8EXPORT uint32_t Hash() const;
+
+  /**
+   * Compute a hash value for the passed UTF16 string
+   * data.
+   */
+  V8EXPORT static uint32_t ComputeHash(uint16_t *string, int length);
+
+  /**
+   * Returns true if this string is equal to the external
+   * string data provided.
+   */
+  V8EXPORT bool Equals(uint16_t *string, int length);
+
+  /**
    * Write the contents of the string to an external buffer.
    * If no arguments are given, expects the buffer to be large
    * enough to hold the entire string and NULL terminator. Copies
diff --git a/src/api.cc b/src/api.cc
index a2373cd..09b6b93 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -3284,6 +3284,21 @@ int String::Utf8Length() const {
   return str->Utf8Length();
 }
 
+uint32_t String::Hash() const {
+  i::Handle<i::String> str = Utils::OpenHandle(this);
+  if (IsDeadCheck(str->GetIsolate(), "v8::String::Hash()")) return 0;
+  return str->Hash();
+}
+
+uint32_t String::ComputeHash(uint16_t *string, int length) {
+  return i::HashSequentialString<i::uc16>(string, length) >> i::String::kHashShift;
+}
+
+bool String::Equals(uint16_t *string, int length) {
+  i::Handle<i::String> str = Utils::OpenHandle(this);
+  if (IsDeadCheck(str->GetIsolate(), "v8::String::Equals()")) return 0;
+  return str->SlowEqualsExternal(string, length);
+}
 
 int String::WriteUtf8(char* buffer,
                       int capacity,
diff --git a/src/objects.cc b/src/objects.cc
index df61956..bbdb37b 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -5346,6 +5346,35 @@ static inline bool CompareStringContentsPartial(Isolate* isolate,
   }
 }
 
+bool String::SlowEqualsExternal(uc16 *string, int length) {
+  int len = this->length();
+  if (len != length) return false;
+  if (len == 0) return true;
+
+  // We know the strings are both non-empty. Compare the first chars
+  // before we try to flatten the strings.
+  if (this->Get(0) != string[0]) return false;
+
+  String* lhs = this->TryFlattenGetString();
+
+  Isolate* isolate = GetIsolate();
+  if (lhs->IsFlat()) {
+    if (lhs->IsAsciiRepresentation()) {
+      Vector<const char> vec1 = lhs->ToAsciiVector();
+      VectorIterator<char> buf1(vec1);
+      VectorIterator<uc16> ib(string, length);
+      return CompareStringContents(&buf1, &ib);
+    } else {
+      Vector<const uc16> vec1 = lhs->ToUC16Vector();
+      Vector<const uc16> vec2(string, length);
+      return CompareRawStringContents(vec1, vec2);
+    }
+  } else {
+    isolate->objects_string_compare_buffer_a()->Reset(0, lhs);
+    VectorIterator<uc16> ib(string, length);
+    return CompareStringContents(isolate->objects_string_compare_buffer_a(), &ib);
+  }
+}
 
 bool String::SlowEquals(String* other) {
   // Fast check: negative check with lengths.
diff --git a/src/objects.h b/src/objects.h
index e966b3d..cd11c88 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -5359,6 +5359,8 @@ class String: public HeapObject {
   bool IsAsciiEqualTo(Vector<const char> str);
   bool IsTwoByteEqualTo(Vector<const uc16> str);
 
+  bool SlowEqualsExternal(uc16 *string, int length);
+
   // Return a UTF8 representation of the string.  The string is null
   // terminated but may optionally contain nulls.  Length is returned
   // in length_output if length_output is not a null pointer  The string
-- 
1.7.2.3