Bug 239427 - Use AtomString as early as possible when string will eventually get atomized
Summary: Use AtomString as early as possible when string will eventually get atomized
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-04-16 20:37 PDT by Chris Dumez
Modified: 2022-04-18 14:13 PDT (History)
22 users (show)

See Also:


Attachments
Patch (53.92 KB, patch)
2022-04-16 20:41 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2022-04-16 20:37:31 PDT
Use AtomString as early as possible when string will eventually get atomized.
Comment 1 Chris Dumez 2022-04-16 20:41:38 PDT
Created attachment 457763 [details]
Patch
Comment 2 Darin Adler 2022-04-18 09:54:33 PDT
Comment on attachment 457763 [details]
Patch

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

From bindings we could have AtomString&& instead of const AtomString& for cases where we are taking ownership and want to save some reference count churn. Not sure how important this is to performance, but it is an optimization possibility.

> Source/WebCore/svg/SVGLengthValue.cpp:276
> +    StringBuilder builder;
> +    builder.append(m_valueInSpecifiedUnits, lengthTypeToString(m_lengthType));
> +    return builder.toAtomString();

We should create makeAtomString for cases like this one. It’s a bit wasteful to use StringBuilder when we know everything up front, and it’s not too hard to write a makeAtomString.
Comment 3 Chris Dumez 2022-04-18 10:01:49 PDT
(In reply to Darin Adler from comment #2)
> Comment on attachment 457763 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=457763&action=review
> 
> From bindings we could have AtomString&& instead of const AtomString& for
> cases where we are taking ownership and want to save some reference count
> churn. Not sure how important this is to performance, but it is an
> optimization possibility.

Yes, Sam mentioned the same thing on Slack. I'll likely look into this in the near future.

> > Source/WebCore/svg/SVGLengthValue.cpp:276
> > +    StringBuilder builder;
> > +    builder.append(m_valueInSpecifiedUnits, lengthTypeToString(m_lengthType));
> > +    return builder.toAtomString();
> 
> We should create makeAtomString for cases like this one. It’s a bit wasteful
> to use StringBuilder when we know everything up front, and it’s not too hard
> to write a makeAtomString.

Let me see how hard it can to write.
Comment 4 Chris Dumez 2022-04-18 10:14:48 PDT
(In reply to Chris Dumez from comment #3)
> (In reply to Darin Adler from comment #2)
> > Comment on attachment 457763 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=457763&action=review
> > 
> > From bindings we could have AtomString&& instead of const AtomString& for
> > cases where we are taking ownership and want to save some reference count
> > churn. Not sure how important this is to performance, but it is an
> > optimization possibility.
> 
> Yes, Sam mentioned the same thing on Slack. I'll likely look into this in
> the near future.
> 
> > > Source/WebCore/svg/SVGLengthValue.cpp:276
> > > +    StringBuilder builder;
> > > +    builder.append(m_valueInSpecifiedUnits, lengthTypeToString(m_lengthType));
> > > +    return builder.toAtomString();
> > 
> > We should create makeAtomString for cases like this one. It’s a bit wasteful
> > to use StringBuilder when we know everything up front, and it’s not too hard
> > to write a makeAtomString.
> 
> Let me see how hard it can to write.

I'll follow-up for this.
Comment 5 Darin Adler 2022-04-18 10:30:22 PDT
(In reply to Chris Dumez from comment #4)
> I'll follow-up for this.

Basically makeAtomString is really only an optimization for short strings.

Once we know the string is short enough we can use an on-stack buffer, build the entire string on the stack, look up the existing StringImpl and allocate a new one only if it’s not already in the AtomString table.

For a longer string, since we have to allocate on the heap anyway, I don’t think we can do much better than AtomString(makeString(xxx)), allocating a buffer (a StringImpl, in fact) and then deallocating it if it turns out to be identical to one in the table.
Comment 6 EWS 2022-04-18 10:57:21 PDT
Committed r292960 (249725@main): <https://commits.webkit.org/249725@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 457763 [details].
Comment 7 Radar WebKit Bug Importer 2022-04-18 10:58:13 PDT
<rdar://problem/91903893>