Bug 177892 - JSC: use default_construct_at
Summary: JSC: use default_construct_at
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: JF Bastien
URL:
Keywords:
Depends on: 177473
Blocks:
  Show dependency treegraph
 
Reported: 2017-10-04 12:55 PDT by JF Bastien
Modified: 2018-03-28 23:40 PDT (History)
9 users (show)

See Also:


Attachments
patch (4.44 KB, patch)
2017-10-04 12:56 PDT, JF Bastien
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description JF Bastien 2017-10-04 12:55:06 PDT
I added default_construct_at to StdLibExtra in #177473 because some code was otherwise hard to read. I wanted to see how it would feel in other parts of the code, and whether it was a widespread thing that we'd use, so I grep'd for a few places in JSC where I could use it, and did so!

I'm thinking about proposing this for C++20, so getting a feel for it seems useful, and in this case causes very little churn.
Comment 1 JF Bastien 2017-10-04 12:56:43 PDT
Created attachment 322707 [details]
patch

Does it look nicer? More understandable?
Comment 2 JF Bastien 2017-10-04 12:57:36 PDT
Forgot to say: an easy way to grep for likely uses is:

  git grep "new (" ./Source/JavaScriptCore/ | grep -v NotNull | grep "()"
Comment 3 Saam Barati 2017-10-04 13:01:50 PDT
I vote for default_construct_at => construct_at

construct_at should just take variadic parameters it does perfect forwarding on. "Default" flows naturally out of that, e.g, no arguments passed.
Comment 4 Alexey Proskuryakov 2017-10-04 16:29:59 PDT
Comment on attachment 322707 [details]
patch

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

> Source/JavaScriptCore/ChangeLog:8
> +        It makes code cleaner.

I know how to read placement new, but I would definitely need to look up what default_construct_at or construct_at were.

In general, it seems undesirable to use 1:1 replacements for language constructs, as that adds barriers to entry.

> Source/JavaScriptCore/yarr/YarrInterpreter.cpp:91
> -        return new (allocatorPool->alloc(size)) DisjunctionContext();
> +        return &default_construct_at(static_cast<DisjunctionContext*>(allocatorPool->alloc(size)));

This in particular seems harder to read than before.
Comment 5 Saam Barati 2017-10-04 16:38:14 PDT
(In reply to Alexey Proskuryakov from comment #4)
> Comment on attachment 322707 [details]
> patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=322707&action=review
> 
> > Source/JavaScriptCore/ChangeLog:8
> > +        It makes code cleaner.
> 
> I know how to read placement new, but I would definitely need to look up
> what default_construct_at or construct_at were.
> 
> In general, it seems undesirable to use 1:1 replacements for language
> constructs, as that adds barriers to entry.
> 
> > Source/JavaScriptCore/yarr/YarrInterpreter.cpp:91
> > -        return new (allocatorPool->alloc(size)) DisjunctionContext();
> > +        return &default_construct_at(static_cast<DisjunctionContext*>(allocatorPool->alloc(size)));
> 
> This in particular seems harder to read than before.

I agree. I much prefer placement new.
Comment 6 JF Bastien 2017-10-19 21:22:27 PDT
Agreed with Saam's suggestion, can forward things.

Code such as this is definitely harder to write as well as read:
  new (&container.get()[i]) std::remove_reference_t<decltype(*container.get())>();

That's the original inspiration for the initial proposal.

That being said it's one place, so I don't really care.
Comment 7 Daniel Bates 2018-03-28 23:40:50 PDT
Comment on attachment 322707 [details]
patch

Clearing r and cq flags to get this patch out of these queues.