Changes between Initial Version and Version 1 of EfficientStrings


Ignore:
Timestamp:
Aug 14, 2012 10:06:52 PM (12 years ago)
Author:
benjamin@webkit.org
Comment:

Some advice on the use of strings

Legend:

Unmodified
Added
Removed
Modified
  • EfficientStrings

    v1 v1  
     1=== Construction ===
     2==== Null and empty ====
     3Passing a null string is very efficient. Whenever possible, use a null String instead of an empty String.
     4
     5==== From literal ====
     6Each String type has a efficient constructor to initialize the string from a literal:
     7 * '''String foo = ASCIILiteral("bar"))''';
     8 * String foo("bar", String::ConstructFromLiteral);
     9 * AtomicString foo("bar", AtomicString::ConstructFromLiteral);
     10
     11The safest option is ''ASCIILiteral("Foo")''. It produces the same code size as String("Foo") while being faster.
     12
     13The difference between the version is if the length of the string is included or not. Having the size given in the constructor makes the constructor faster. Having the size also makes the code bigger, which is a problem when the code is executed infrequently.
     14
     15==== Not creating a string ====
     16Many operations can be more efficient with a literal. Do not create a String when it is not needed.
     17
     18E.g.:
     19 * foo.startsWith("bar")
     20 * foo.startsWith(ASCIILiteral("bar"))
     21 * foo.startsWith(String("bar"))
     22The first version is the fastest.
     23
     24
     25=== Concatenation ===
     26
     27There are two efficient way to concatenate strings: StringBuilder and StringOperators. Anything else is pretty much less efficient when doing more than one operations.
     28
     29E.g.:
     30
     31{{{
     32str = text;
     33str.append("a"); // == str.append(String("a");
     34str.append(foo);
     35str += bar;
     36}}}
     37Should be (StringOperators):
     38{{{
     39str = text; + "a" + foo + bar;
     40}}}
     41
     42----
     43
     44E.g:
     45
     46{{{
     47str = "foo";
     48for (size_t i = 0; i < foobars; ++i) {
     49   str += "bar";
     50}}}
     51should be:
     52
     53{{{
     54StringBuilder builder;
     55builder.append("foo");
     56for (size_t i = 0; i < foobars; ++i) {
     57   builder.append("bar");
     58str = builder.toString();
     59}}}