のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

AtomicString

AtomicString VS String

WTF::AtomicString is a class that has four differences from the normal WTF::String class:

(1)It more expensive to create a new atomic string than a non-atomic string; doing so requires a lookup in a per-thread atomic string hash table.

(2)It very inexpensive to compare one atomic string with another. The cost is just a pointer comparison. The actual string length and data don't need to be compared, because on any one thread no AtomicString can be equal to any other AtomicString.

(3)If a particular string already exists in the atomic string table, allocating another string that is equal to it does not cost any additional memory.
 The atomic string is shared and the cost is looking it up in the per-thread atomic string hash table and incrementing its reference count.

(4)There are special considerations if you want to use an atomic string on a thread other than the one it was created on since each thread has its own atomic string hash table.


We use AtomicString to make string comparisons fast and to save memory when many equal strings are likely to be allocated. For example, we use AtomicString for HTML attribute names so we can compare them quickly, and for both HTML attribute names and values since it’s common to have many identical ones and we save memory.

We shouldn't use AtomicString if the string we're about to create doesn't get shared across multiple AtomicStrings. For example, if we had used AtomicString for the strings inside Text nodes, then we may end up filling up the atomic string table with all these really long strings that don't typically appear more than once. It also slows down the hash map look up for all other atomic strings.

(this topic is a summary of the thread "[webkit-dev] When should I use AtomicString vs String?")