Time has moved on and we now use constexpr for most of these - do we still have a use for static in that case? What even is the distinction between "constexpr char[]" and "static constexpr char[]"...?
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CALekkJd6b1r1DZvpz1cJbwxEFbGnBd5xO%3DKoZtee3Y2ANmdfJQ%40mail.gmail.com.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAAHOzFDUPzz5idQP1PoJaaF7Xhh2vxj4gLnVumT0hcK3_m1vgg%40mail.gmail.com.
Using a constexpr StringPiece probably requires a relocation. I would stick to constexpr char[] over StringPiece.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAAHOzFAgsCKZtJQ38JTR8W3%2BUMi_pt0w1tL1ifyKWKreB9ww4A%40mail.gmail.com.
Yes, relocations are a huge problem, due to the runtime memory overhead, which is paid per-process in some systems.
We can certainly track the number of relocations, and presumably also the number of CoW pages needed to hold them, but I don't think we could realistically "limit" them since there are common patterns & language features (e.g. vtables) that require them - I think we'd need to focus on specific patterns/use-cases, e.g. requiring "static constexpr const kMyLovelyLiteral[] = ...." for literals, rather than the various other forms that currently exist
On Fri, 4 Nov 2022 at 17:06, Peter Kasting <pkas...@chromium.org> wrote:On Fri, Nov 4, 2022 at 9:05 AM Wez <w...@chromium.org> wrote:Yes, relocations are a huge problem, due to the runtime memory overhead, which is paid per-process in some systems.Hmm. Is there a way to track these and try and reduce them, as we do with static initializers?PK
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CALekkJes88FgoC6_8a0ACSnTt0joPYkTGi9Zf%2B5Tfy2Nrva2Mw%40mail.gmail.com.
On Fri, 4 Nov 2022 at 12:17, Wez <w...@chromium.org> wrote:We can certainly track the number of relocations, and presumably also the number of CoW pages needed to hold them, but I don't think we could realistically "limit" them since there are common patterns & language features (e.g. vtables) that require them - I think we'd need to focus on specific patterns/use-cases, e.g. requiring "static constexpr const kMyLovelyLiteral[] = ...." for literals, rather than the various other forms that currently existAgreed - I looked into our relocations years ago to try to improve the situation on Android (where we originally could not share these pages at all) and at the time >50% of them were for vtables, which is probably still true. Avoiding them for vtables is very difficult, but IMO that just means we *should* avoid specific patterns that could easily be replaced with something that doesn't need relocating, so that we only have to pay for the ones that are harder to avoid.
On Fri, 4 Nov 2022 at 18:24, Torne (Richard Coles) <to...@chromium.org> wrote:On Fri, 4 Nov 2022 at 12:17, Wez <w...@chromium.org> wrote:We can certainly track the number of relocations, and presumably also the number of CoW pages needed to hold them, but I don't think we could realistically "limit" them since there are common patterns & language features (e.g. vtables) that require them - I think we'd need to focus on specific patterns/use-cases, e.g. requiring "static constexpr const kMyLovelyLiteral[] = ...." for literals, rather than the various other forms that currently existAgreed - I looked into our relocations years ago to try to improve the situation on Android (where we originally could not share these pages at all) and at the time >50% of them were for vtables, which is probably still true. Avoiding them for vtables is very difficult, but IMO that just means we *should* avoid specific patterns that could easily be replaced with something that doesn't need relocating, so that we only have to pay for the ones that are harder to avoid.The toolchain does now support relative-vtables, which should work so long as you're able to build all of the C++ you're linking with them enabled, of course. :)
On Fri, 4 Nov 2022 at 13:34, Wez <w...@chromium.org> wrote:On Fri, 4 Nov 2022 at 18:24, Torne (Richard Coles) <to...@chromium.org> wrote:On Fri, 4 Nov 2022 at 12:17, Wez <w...@chromium.org> wrote:We can certainly track the number of relocations, and presumably also the number of CoW pages needed to hold them, but I don't think we could realistically "limit" them since there are common patterns & language features (e.g. vtables) that require them - I think we'd need to focus on specific patterns/use-cases, e.g. requiring "static constexpr const kMyLovelyLiteral[] = ...." for literals, rather than the various other forms that currently existAgreed - I looked into our relocations years ago to try to improve the situation on Android (where we originally could not share these pages at all) and at the time >50% of them were for vtables, which is probably still true. Avoiding them for vtables is very difficult, but IMO that just means we *should* avoid specific patterns that could easily be replaced with something that doesn't need relocating, so that we only have to pay for the ones that are harder to avoid.The toolchain does now support relative-vtables, which should work so long as you're able to build all of the C++ you're linking with them enabled, of course. :)That's *super* interesting and I'd love to know if anyone has already started looking at trying this on Android. The Android NDK libraries should all be a pure C ABI for ABI stability, so there's no *obvious* reason why we couldn't use a relative-vtable ABI for our C++ and see what the performance/memory tradeoffs look like.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAEV-rjfyt3a9iu_Y6FS95SDkGZ%2BQFgQD0-xHnhxJ--W2opcERw%40mail.gmail.com.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CALekkJes88FgoC6_8a0ACSnTt0joPYkTGi9Zf%2B5Tfy2Nrva2Mw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAL0AKM-XOeLcE8QMxVPk1w0GMUC7YrJRaJX4mckojY%3DwFHLaQg%40mail.gmail.com.
I think the answer to whether we should add checks/guidance for strings depends on whether there's a compelling alternative.For strings < N bytes (maybe 16?), I think const char[] is a compelling alternative. Computing its length over and over will be hard to notice, and we'd be better off saving the space.For larger strings, we could probably write our own global_string<int size> template that could use char[] under-the-hood, but I'm not sure if we could do it without a macro.E.g.:template<int SIZE>
struct GlobalString {
char data[SIZE];
operator const char*() const { return data; }
};GlobalString<7> global_str{ "global"};Can we get rid of the explicit <7> without a macro?
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CABiQX1WhPGPFRivR52A665AjxM1hY78sUiMsUWoOk_rxa-PACQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAHtyhaSnESv4O0xr6Cr0iuxmcthVWJsF0_DMpjuMAFQ8MGR95w%40mail.gmail.com.
template<std::size_t N> struct DoubleString { char p[N*2-1]{}; constexpr DoubleString(char const(&pp)[N]) { std::ranges::copy(pp, p); std::ranges::copy(pp, p + N - 1); }; }; template<DoubleString A> constexpr auto operator"" _x2() { return A.p; }
I think the answer to whether we should add checks/guidance for strings depends on whether there's a compelling alternative.For strings < N bytes (maybe 16?), I think const char[] is a compelling alternative. Computing its length over and over will be hard to notice, and we'd be better off saving the space.
For larger strings, we could probably write our own global_string<int size> template that could use char[] under-the-hood, but I'm not sure if we could do it without a macro.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CABiQX1WhPGPFRivR52A665AjxM1hY78sUiMsUWoOk_rxa-PACQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAHk1GDwgVXggNQ_v6a%3DxgugmZYgPXZO3Yg2hSB628V2TDNHyAA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAJTZ7LLGdMKbBOWaTmoe-C_HdqB9bP73VYWubm4cv5hG5j0Qjg%40mail.gmail.com.
Ah! Thanks Mark. FWIW, I took that from this old thread where return A[x] was sufficient to trigger a difference before.
This confirms what I thought: constexpr and const are equivalent for POD types (constexpr just says "please pre-compute this" but a POD is always pre-computed).So we still need static (and const is fine, constexpr is overkill).
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAHk1GDy_Nd3BZaVmRzVWjbsqjshLgttw%3DS0_zxHjjfFRQ8dk9w%40mail.gmail.com.
So... what's the TLDR of this whole thread?* Use constexpr over const because it's a nice practice to be in and occasionally enforces that you didn't actually do something dumb
* No need for "constexpr const char[]" because in this case constexpr implies const (note, that is not necessarily true for other uses of constexpr)
* No need for "inline constexpr char[]" because in this case constexpr implies inline (note, that is not necessarily true for other uses of constexpr)
* Use "static" in classes and inside functions?
* If you did use "static", it is also OK to use a StringPiece, because we won't generate a relocation? What about for file-/global-scope objects?
Don't get me wrong, I'm a big fan of constexpr. But I think something like `constexpr int kFoo = 1234;` is overkill and I view `constexpr char kBar[] = "bar";` the same 🤷♂️. I'll never ask for it to be changed in a review but I don't see a need for it. Do constexpr any complex initialization for sure.On Wed, Nov 9, 2022 at 2:37 PM Peter Kasting <pkas...@chromium.org> wrote:So... what's the TLDR of this whole thread?* Use constexpr over const because it's a nice practice to be in and occasionally enforces that you didn't actually do something dumbMeh (per above).* No need for "constexpr const char[]" because in this case constexpr implies const (note, that is not necessarily true for other uses of constexpr)Agreed (when would `constexpr const` ever mean something other than `constexpr`?)* No need for "inline constexpr char[]" because in this case constexpr implies inline (note, that is not necessarily true for other uses of constexpr)For class scope: `static constexpr char kFoo[] = "foo"`So yes, no inline needed I think?* Use "static" in classes and inside functions?"static" does better reflect the intent but since clang seems to optimize all the same, we should stop caring?
Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects a and b with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they have distinct addresses.⁵
5) Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an object at all if the program cannot observe the difference (4.6).
An instance of each object with automatic storage duration (6.7.3) is associated with each entry into its block. Such an object exists and retains its last-stored value during the execution of the block and while the block is suspended (by a call of a function or receipt of a signal).
Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (6.7).
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAHk1GDzJV5VaBDvFKCcbGzY-W2DS%2BgWy2eHGHH-Zdp2B8Wiu5g%40mail.gmail.com.
Not to detract from the main point (that static still has a benefit), but I don't think there's a uniqueness guarantee without static? If you call the same function twice in a row from the same callstack and thread, wouldn't the local variable get allocated at the same address on the stack since it's relative to the stack pointer?
And more generally, I don't think uniqueness would be guaranteed in some way (as that would require some machinery to ensure an address from before isn't re-used, which doesn't make sense to have.)
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAHk1GDz7hFbqiJGNwTxYUvbjFrmFKetfQF6WnynbnnWf-K1Zwg%40mail.gmail.com.