Skip to content

Conversation

xenu
Copy link
Member

@xenu xenu commented Aug 25, 2025

Local, non-static constant arrays may get re-initialised at runtime (by pushing their contents on the stack) every time they are reached. This somewhat surprising behaviour is caused by the fact that the C standard requires (almost) every object to have a unique address.

In theory, a compiler may eliminate the re-initialisation, if it can prove that the address of an array is never examined. However:

  1. This often isn't possible to prove. The compiler has no way to know what will happen to an array pointer once it leaves the compilation unit.

  2. Unlike Clang, GCC and Visual C++ don't even attempt this optimisation at all. GCC has an open ticket for this:
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

Making those arrays static avoids the problem, because static arrays are guaranteed to be initialised only once, prior the to the program startup.

  • This set of changes does not require a perldelta entry.

@leonerd
Copy link
Contributor

leonerd commented Aug 25, 2025

I see some callsites where sizeof has been changed to strlen - is that going to have a runtime cost on every evaluation now?

@xenu
Copy link
Member Author

xenu commented Aug 25, 2025

Compilers are smart enough to eliminate strlen for string constants. Here are outputs of GCC, Clang and Visual C++ (all with -O2): https://gcc.godbolt.org/z/1nr1snhrd

I think we already rely on this in a few places, ditto for memcpy.

You-know-who will probably complain that Visual C++ with -O1 can't. This is of course a self-inflicted problem, because we could easily switch to -O2 or at least -O1 -Oi. But you-know-who doesn't want to do that, and I don't have the energy to argue with him.

@xenu xenu force-pushed the xenu/const-char branch from 29059a5 to 5aff122 Compare August 25, 2025 21:16
@xenu
Copy link
Member Author

xenu commented Aug 25, 2025

Force-pushed with a tiny change, I forgot one const in hangul_prefix declaration.

@khwilliamson
Copy link
Contributor

I think we should have a section in perlhacktips of accumulated wisdom, maybe "Hints". This would be a good first entry.

@tonycoz
Copy link
Contributor

tonycoz commented Aug 26, 2025

Looks good otherwise.

Local, non-static constant arrays may get re-initialised at runtime (by
pushing their contents on the stack) every time they are reached. This
somewhat surprising behaviour is caused by the fact that the C standard
requires (almost) every object to have a unique address.

In theory, a compiler may eliminate the re-initialisation, if it can
prove that the address of an array is never examined. However:

1. This often isn't possible to prove. The compiler has no way to know
what will happen to an array pointer once it leaves the compilation
unit.

2. Unlike Clang, GCC and Visual C++ don't even attempt this optimisation
at all. GCC has an open ticket for this:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

Making those arrays static avoids the problem, because static arrays are
guaranteed to be initialised only once, prior the to the program
startup.
@xenu xenu force-pushed the xenu/const-char branch from 5aff122 to c866ac7 Compare August 26, 2025 00:38
@xenu xenu merged commit 93fbcb7 into Perl:blead Aug 26, 2025
33 checks passed
@xenu xenu deleted the xenu/const-char branch August 26, 2025 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants