Bug 185337 - Support instantiating a String with a StaticStringImpl{&, *}
Summary: Support instantiating a String with a StaticStringImpl{&, *}
Status: ASSIGNED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Daniel Bates
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-05-04 18:49 PDT by Daniel Bates
Modified: 2019-02-03 23:12 PST (History)
5 users (show)

See Also:


Attachments
Patch (2.15 KB, patch)
2018-05-04 18:53 PDT, Daniel Bates
dbates: review-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Bates 2018-05-04 18:49:15 PDT
We should support instantiating a String with a StaticStringImpl{&, *} so that we can construct constexpr StaticStringImpls and pass them to functions the take a String without needing to cast it to non-const StaticStringImpl beforehand. It is safe for String to take a const StaticStringImpl and internally cast to a non-const StringImpl because StaticStringImpls are guaranteed to be immutable. This immutability is guaranteed both at runtime (by [1]) and by convention of only instantiating a StaticStringImpl in a constexpr expression.

[1] <https://trac.webkit.org/browser/trunk/Source/WTF/wtf/text/StringImpl.h?rev=231268#L330>
Comment 1 Daniel Bates 2018-05-04 18:53:11 PDT
Created attachment 339622 [details]
Patch
Comment 2 Yusuke Suzuki 2018-05-04 19:13:01 PDT
Comment on attachment 339622 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=339622&action=review

> Source/WTF/ChangeLog:11
> +        convention of only instantiating a StaticStringImpl in a constexpr expression.

Is this condition maintained for the refcount of StaticStringImpl? StaticStringImpl sets s_refCountFlagIsStaticString = 0x1.
Since our StringImpl increments/decrements the refcount by 2, setting 0x1 initial ensures that the refcount never becomes zero even if multiple threads ref/deref it.
Even though, the refcount itself is mutated.
Comment 3 Daniel Bates 2018-05-04 20:02:07 PDT
(In reply to Yusuke Suzuki from comment #2)
> Comment on attachment 339622 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=339622&action=review
> 
> > Source/WTF/ChangeLog:11
> > +        convention of only instantiating a StaticStringImpl in a constexpr expression.
> 
> Is this condition maintained for the refcount of StaticStringImpl?
> StaticStringImpl sets s_refCountFlagIsStaticString = 0x1.
> Since our StringImpl increments/decrements the refcount by 2, setting 0x1
> initial ensures that the refcount never becomes zero even if multiple
> threads ref/deref it.
> Even though, the refcount itself is mutated.

:( This patch is wrong.
Comment 4 Daniel Bates 2018-05-04 20:08:19 PDT
(In reply to Yusuke Suzuki from comment #2)
> Comment on attachment 339622 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=339622&action=review
> 
> > Source/WTF/ChangeLog:11
> > +        convention of only instantiating a StaticStringImpl in a constexpr expression.
> 
> Is this condition maintained for the refcount of StaticStringImpl?
> StaticStringImpl sets s_refCountFlagIsStaticString = 0x1.
> Since our StringImpl increments/decrements the refcount by 2, setting 0x1
> initial ensures that the refcount never becomes zero even if multiple
> threads ref/deref it.

How is it thread-safe to increment and decrement the ref count for a StringImpl? I mean, its ref count is an unsigned and we do not use we do not use locks in StringImpl::ref/deref(). In particular, how is StringImpl::deref() thread-safe?
Comment 5 Yusuke Suzuki 2018-05-04 20:17:52 PDT
Comment on attachment 339622 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=339622&action=review

>>>> Source/WTF/ChangeLog:11
>>>> +        convention of only instantiating a StaticStringImpl in a constexpr expression.
>>> 
>>> Is this condition maintained for the refcount of StaticStringImpl? StaticStringImpl sets s_refCountFlagIsStaticString = 0x1.
>>> Since our StringImpl increments/decrements the refcount by 2, setting 0x1 initial ensures that the refcount never becomes zero even if multiple threads ref/deref it.
>>> Even though, the refcount itself is mutated.
>> 
>> :( This patch is wrong.
> 
> How is it thread-safe to increment and decrement the ref count for a StringImpl? I mean, its ref count is an unsigned and we do not use we do not use locks in StringImpl::ref/deref(). In particular, how is StringImpl::deref() thread-safe?

Basic StringImpl is not thread-safe. When moving it around threads, we call `isolateCopy()`.
The exception is *static* StringImpl (like, StaticStringImpl. The StringImpl with `isStatic()` flag). In this case, the ref count is set to 0x1.
Since there is not thread-safety in StringImpl, the effect of StringImpl::ref/deref() would be lost. But it is OK since Static StringImpl is never destroyed. Threads increment/decrement refcount by 2. (ref += 2 / ref -= 2).
But refcount's first bit is always 0x1 in static StringImpl. So while the value of refCount() of static StringImpl becomes bogus, the refcount never becomes zero, and static StringImpl is never destroyed.