String constants don't fold in the middle of an expression

Original Reporter info from Mantis: runewalsh
  • Reporter name:

Description:

'litA' + 'litB' + 'litC' + 'litD' + CreateDynamicString() merges literals into one constant 'litAlitBlitClitD'.
While:
CreateDynamicString() + 'litA' + 'litB' + 'litC' + 'litD' concatenates everything at runtime.

Program attached shows that this can result in some overhead.
Add to it the overhead of 3×sizeof(pointer) for the header of each non-folded string.

Originally I built a long HTML preface this way, which was mostly static, but had dynamic parts, one of which is of course encountered almost immediately:
const LE = LineEnding;
chunk :=
  '&LtPos;html lang=' + GetBaseLang() + ... + LE +
  ... + LE +
  '&LtPos;style>' + LE +
  '.grid > div {' + LE +
  ' white-space: pre-wrap;' + LE +
  ' overflow-wrap: break-word;' + LE +
  ' hyphens: auto;' + LE +
  ' line-height: 1.5;' + LE +
  ... many lines more ...

so I cut around 20 Kb of executable size of 600 by manually wrapping groups of compile-time constants into parentheses.

Additional information:

Subtly related to 35827 :D

Mantis conversion info:

  • Mantis ID: 38267
  • Fixed in version: 3.3.1
  • Fixed in revision: 47933 (#97abf6b4), 47977 (#7de94e7a)
  • Monitored by: » @CuriousKit (J. Gareth Moreton)