Title: [277667] trunk/Source/WebCore
Revision
277667
Author
cdu...@apple.com
Date
2021-05-18 11:59:29 -0700 (Tue, 18 May 2021)

Log Message

Use UTF-8 internally from Strings inside SQLiteStatement
https://bugs.webkit.org/show_bug.cgi?id=225890

Reviewed by Darin Adler.

Use UTF-8 internally from Strings inside SQLiteStatement. Our SQLite databases use UTF-8
internally for text encoding (since we open the database with sqlite3_open_v2() and not
sqlite3_open16()).

Previously, we were providing UTF-16 strings to SQLite, which meant it had to convert it
internally to UTF-8. Also, whenever we were requesting a string from SQLite, it had to
convert it from UTF-8 to UTF-16 before returning it to us. Our String constructed from
returned from SQLiteStatement would also be 16-bit encoded, even if all ASCII so we were
potentially wasting memory too.

We now provide UTF-8 to SQLite by using StringView::utf8(). We also request UTF-8 strings
from SQLite and rely on String::fromUTF8() to convert it to a WTF::String. For all ASCII
characters, this means we'll end up with a 8-bit String and save memory.

* platform/sql/SQLiteStatement.cpp:
(WebCore::SQLiteStatement::bindText):
(WebCore::SQLiteStatement::getColumnName):
(WebCore::SQLiteStatement::getColumnText):
* platform/sql/SQLiteStatement.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (277666 => 277667)


--- trunk/Source/WebCore/ChangeLog	2021-05-18 18:48:48 UTC (rev 277666)
+++ trunk/Source/WebCore/ChangeLog	2021-05-18 18:59:29 UTC (rev 277667)
@@ -1,3 +1,30 @@
+2021-05-18  Chris Dumez  <cdu...@apple.com>
+
+        Use UTF-8 internally from Strings inside SQLiteStatement
+        https://bugs.webkit.org/show_bug.cgi?id=225890
+
+        Reviewed by Darin Adler.
+
+        Use UTF-8 internally from Strings inside SQLiteStatement. Our SQLite databases use UTF-8
+        internally for text encoding (since we open the database with sqlite3_open_v2() and not
+        sqlite3_open16()).
+
+        Previously, we were providing UTF-16 strings to SQLite, which meant it had to convert it
+        internally to UTF-8. Also, whenever we were requesting a string from SQLite, it had to
+        convert it from UTF-8 to UTF-16 before returning it to us. Our String constructed from
+        returned from SQLiteStatement would also be 16-bit encoded, even if all ASCII so we were
+        potentially wasting memory too.
+
+        We now provide UTF-8 to SQLite by using StringView::utf8(). We also request UTF-8 strings
+        from SQLite and rely on String::fromUTF8() to convert it to a WTF::String. For all ASCII
+        characters, this means we'll end up with a 8-bit String and save memory.
+
+        * platform/sql/SQLiteStatement.cpp:
+        (WebCore::SQLiteStatement::bindText):
+        (WebCore::SQLiteStatement::getColumnName):
+        (WebCore::SQLiteStatement::getColumnText):
+        * platform/sql/SQLiteStatement.h:
+
 2021-05-18  Said Abou-Hallawa  <s...@apple.com>
 
         Allow logging minimal info about uploading media files

Modified: trunk/Source/WebCore/platform/network/curl/CookieJarDB.cpp (277666 => 277667)


--- trunk/Source/WebCore/platform/network/curl/CookieJarDB.cpp	2021-05-18 18:48:48 UTC (rev 277666)
+++ trunk/Source/WebCore/platform/network/curl/CookieJarDB.cpp	2021-05-18 18:59:29 UTC (rev 277667)
@@ -363,7 +363,7 @@
         statement.bindNull(2);
     } else {
         statement.bindText(1, registrableDomain.string());
-        statement.bindText(2, String("*.") + registrableDomain.string());
+        statement.bindText(2, makeString("*.", registrableDomain.string()));
     }
 
     return statement.step() == SQLITE_ROW;
@@ -406,7 +406,7 @@
     if (CookieUtil::isIPAddress(requestHost) || !requestHost.contains('.') || registrableDomain.isEmpty())
         pstmt->bindNull(6);
     else
-        pstmt->bindText(6, String("*.") + registrableDomain.string());
+        pstmt->bindText(6, makeString("*.", registrableDomain.string()));
 
     Vector<Cookie> results;
 

Modified: trunk/Source/WebCore/platform/sql/SQLiteStatement.cpp (277666 => 277667)


--- trunk/Source/WebCore/platform/sql/SQLiteStatement.cpp	2021-05-18 18:48:48 UTC (rev 277666)
+++ trunk/Source/WebCore/platform/sql/SQLiteStatement.cpp	2021-05-18 18:59:29 UTC (rev 277667)
@@ -110,22 +110,17 @@
     return bindBlob(index, characters, text.length() * sizeof(UChar));
 }
 
-int SQLiteStatement::bindText(int index, const String& text)
+int SQLiteStatement::bindText(int index, StringView text)
 {
     ASSERT(index > 0);
     ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
 
-    // String::characters() returns 0 for the empty string, which SQLite
-    // treats as a null, so we supply a non-null pointer for that case.
-    auto upconvertedCharacters = StringView(text).upconvertedCharacters();
-    UChar anyCharacter = 0;
-    const UChar* characters;
-    if (text.isEmpty() && !text.isNull())
-        characters = &anyCharacter;
-    else
-        characters = upconvertedCharacters;
+    // Fast path when the input text is all ASCII.
+    if (text.is8Bit() && text.isAllASCII())
+        return sqlite3_bind_text(m_statement, index, text.length() ? reinterpret_cast<const char*>(text.characters8()) : "", text.length(), SQLITE_TRANSIENT);
 
-    return sqlite3_bind_text16(m_statement, index, characters, sizeof(UChar) * text.length(), SQLITE_TRANSIENT);
+    auto utf8Text = text.utf8();
+    return sqlite3_bind_text(m_statement, index, utf8Text.data(), utf8Text.length(), SQLITE_TRANSIENT);
 }
 
 int SQLiteStatement::bindInt(int index, int integer)
@@ -192,7 +187,7 @@
         return String();
     if (columnCount() <= col)
         return String();
-    return String(reinterpret_cast<const UChar*>(sqlite3_column_name16(m_statement, col)));
+    return String::fromUTF8(sqlite3_column_name(m_statement, col));
 }
 
 SQLValue SQLiteStatement::getColumnValue(int col)
@@ -207,18 +202,16 @@
     // "(mostly) ignored"
     sqlite3_value* value = sqlite3_column_value(m_statement, col);
     switch (sqlite3_value_type(value)) {
-        case SQLITE_INTEGER:    // SQLValue and JS don't represent integers, so use FLOAT -case
-        case SQLITE_FLOAT:
-            return sqlite3_value_double(value);
-        case SQLITE_BLOB:       // SQLValue and JS don't represent blobs, so use TEXT -case
-        case SQLITE_TEXT: {
-            const UChar* string = reinterpret_cast<const UChar*>(sqlite3_value_text16(value));
-            return StringImpl::create8BitIfPossible(string);
-        }
-        case SQLITE_NULL:
-            return nullptr;
-        default:
-            break;
+    case SQLITE_INTEGER: // SQLValue and JS don't represent integers, so use FLOAT -case
+    case SQLITE_FLOAT:
+        return sqlite3_value_double(value);
+    case SQLITE_BLOB: // SQLValue and JS don't represent blobs, so use TEXT -case
+    case SQLITE_TEXT:
+        return String::fromUTF8(sqlite3_value_text(value), sqlite3_value_bytes(value));
+    case SQLITE_NULL:
+        return nullptr;
+    default:
+        break;
     }
 
     ASSERT_NOT_REACHED();
@@ -232,7 +225,7 @@
         return String();
     if (columnCount() <= col)
         return String();
-    return String(reinterpret_cast<const UChar*>(sqlite3_column_text16(m_statement, col)), sqlite3_column_bytes16(m_statement, col) / sizeof(UChar));
+    return String::fromUTF8(sqlite3_column_text(m_statement, col), sqlite3_column_bytes(m_statement, col));
 }
     
 double SQLiteStatement::getColumnDouble(int col)

Modified: trunk/Source/WebCore/platform/sql/SQLiteStatement.h (277666 => 277667)


--- trunk/Source/WebCore/platform/sql/SQLiteStatement.h	2021-05-18 18:48:48 UTC (rev 277666)
+++ trunk/Source/WebCore/platform/sql/SQLiteStatement.h	2021-05-18 18:59:29 UTC (rev 277667)
@@ -40,7 +40,7 @@
     
     WEBCORE_EXPORT int bindBlob(int index, const void* blob, int size);
     WEBCORE_EXPORT int bindBlob(int index, const String&);
-    WEBCORE_EXPORT int bindText(int index, const String&);
+    WEBCORE_EXPORT int bindText(int index, StringView);
     WEBCORE_EXPORT int bindInt(int index, int);
     WEBCORE_EXPORT int bindInt64(int index, int64_t);
     WEBCORE_EXPORT int bindDouble(int index, double);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to