Skip to content

Enable CowData/String in constexpr environment#108788

Draft
YYF233333 wants to merge 9 commits intogodotengine:masterfrom
YYF233333:comptime_string
Draft

Enable CowData/String in constexpr environment#108788
YYF233333 wants to merge 9 commits intogodotengine:masterfrom
YYF233333:comptime_string

Conversation

@YYF233333
Copy link
Copy Markdown
Contributor

@YYF233333 YYF233333 commented Jul 20, 2025

TLDR: This PR allows you to write constexpr String and constexpr StringName.

Overview

Enable the usage of CowData/String/StringName with string literals at compile time.

This is achieved by constructing a static char32_t buffer with the same layout CowData get at runtime. refcount is assigned a big enough value (currently UINT32_MAX) so the buffer will never be freed. With a constinit buffer like this we can create various pointer-like types (String/StringName).

Implementation

  • CowBuffer: a static char32_t buffer with refcount and size attached at front. Some tricks are used to ensure it can be evaluated in constexpr environment. (I don't know if I accidentally introduce any forbidden STL stuff, please tell me if you find anything inappropriate.)
  • ComptimeString: convenient wrapper. Instead of writing static constinit _buf = CowBuffer("str"); constexpr String s = CowData<char32_t>(buf);, we can just use ComptimeString<"str">().value
  • Change many operators of CowData/String/StringName to constexpr. Memory alloc related codes are bypassed using std::is_constant_evaluated().
  • ComptimeStringName: similar to ComptimeString, but also handle StringName::_Data registration.

Usage

  • GDCLASS/GDVIRTUAL: Replace static String, static StringName, "string literals" with their comptime conterparts.
  • SNAME: Replace lambda with ComptimeStringName.
  • StringName("literals", true): Replace with ComptimeStringName.

Benchmark

Old

Binary Size (production=yes target=template_release use_llvm=yes)

Benchmarks (5 runs average)

Result (quite chaotic and not very consistent)

raw data:
ma.jsonl.txt
pr.jsonl.txt

                                                      pr              ma       diff    percent
Export Gltf                                       107.50          108.24   -0000.74     -00.68%
Import Fbx                                        505.12          507.40   -0002.28     -00.45%
Import Gltf                                        55.62           55.90   -0000.28     -00.50%
Import Ogg Audio                                    6.64            6.70   -0000.06     -00.89%
Import Webp Images                                806.18          805.06   +0001.12     +00.14%
Append Array                                      166.88          158.68   +0008.20     +05.17%
Bsearch                                           101.55          103.66   -0002.11     -02.04%
Fill                                               61.78           49.38   +0012.40     +25.12%
Reverse                                            25.83           25.79   +0000.05     +00.19%
Function Callable                                 317.34          313.24   +0004.10     +01.31%
Lambda Inline Callable                            373.30          386.24   -0012.94     -03.35%
Lambda Variable Callable                           90.77           89.52   +0001.25     +01.40%
Load                                               35.73           36.37   -0000.65     -01.78%
Load With Password                                 38.15           40.57   -0002.42     -05.96%
Save                                               33.81           33.46   +0000.35     +01.04%
Save With Password                                 39.15           39.02   +0000.13     +00.34%
Generate 1g Random Bytes 1k At A Time            1238.80         1247.00   -0008.20     -00.66%
Generate 1m Random Bytes 10 At A Time            2192.20         2255.20   -0063.00     -02.79%
Create                                             17.19           16.92   +0000.27     +01.60%
12 Threads Full Contention                        863.94          556.98   +0306.96     +55.11%
12 Threads Half Contention                        441.80          293.30   +0148.50     +50.63%
12 Threads Little Contention                      108.88           79.62   +0029.26     +36.76%
12 Threads Slope Contention                       468.76          328.58   +0140.18     +42.66%
2 Threads Full Contention                          62.46           55.99   +0006.47     +11.56%
2 Threads Half Contention                          43.62           41.34   +0002.28     +05.51%
2 Threads Little Contention                        28.31           27.73   +0000.58     +02.08%
4 Threads Full Contention                         184.86          163.86   +0021.00     +12.82%
4 Threads Half Contention                         107.50           93.51   +0013.99     +14.96%
4 Threads Little Contention                        42.16           38.34   +0003.81     +09.95%
4 Threads Slope Contention                        139.96          122.92   +0017.04     +13.86%
8 Threads Full Contention                         464.30          340.78   +0123.52     +36.25%
8 Threads Half Contention                         248.26          188.90   +0059.36     +31.42%
8 Threads Little Contention                        69.11           57.71   +0011.41     +19.77%
8 Threads Slope Contention                        290.88          221.20   +0069.68     +31.50%
Single                                             24.82           25.07   -0000.24     -00.97%
Global Scope Randf                                109.64          109.62   +0000.02     +00.02%
Global Scope Randf Range                          361.52          359.50   +0002.02     +00.56%
Global Scope Randfn                               852.24          855.16   -0002.92     -00.34%
Global Scope Randi                                102.90          102.68   +0000.22     +00.21%
Global Scope Randi Range                          140.54          140.06   +0000.48     +00.34%
Global Scope Randomize                            779.64          778.38   +0001.26     +00.16%
Randf                                             269.94          270.44   -0000.50     -00.18%
Randf Range                                       296.42          295.66   +0000.76     +00.26%
Randfn                                            806.48          799.56   +0006.92     +00.87%
Randi                                              84.76           86.48   -0001.73     -02.00%
Randi Range                                       113.02          112.94   +0000.08     +00.07%
Randomize                                         801.48          795.76   +0005.72     +00.72%
Emission Params 0                                 213.92          216.66   -0002.74     -01.26%
Emission Params 1                                 217.76          218.76   -0001.00     -00.46%
Emission Params 10                                252.44          254.04   -0001.60     -00.63%
Deep Tree                                         132.80          132.90   -0000.10     -00.08%
Duplicate                                        1078.00         1129.20   -0051.20     -04.53%
Fragmentation                                    1202.60         1211.80   -0009.20     -00.76%
Wide Tree                                         126.22          125.12   +0001.10     +00.88%
Fill Loop                                         241.70          242.74   -0001.04     -00.43%
Fill Method                                       105.34          105.80   -0000.46     -00.43%
Packed Color Array                                142.88          145.04   -0002.16     -01.49%
Packed Float 32 Array                             108.02          113.14   -0005.12     -04.53%
Packed Float 64 Array                             112.54          111.14   +0001.40     +01.26%
Packed Int 32 Array                                90.97           90.50   +0000.48     +00.53%
Packed Int 64 Array                                96.77           94.21   +0002.55     +02.71%
Packed String Array                               524.32          531.40   -0007.08     -01.33%
Packed Vector 2 Array                             122.28          122.18   +0000.10     +00.08%
Packed Vector 3 Array                             139.24          138.00   +0001.24     +00.90%
Typed Color Array                                 217.24          214.68   +0002.56     +01.19%
Typed Float Array                                 183.68          180.68   +0003.00     +01.66%
Typed Int Array                                   175.06          178.96   -0003.90     -02.18%
Typed String Array                                625.42          611.32   +0014.10     +02.31%
Typed Vector 2 Array                              209.70          200.24   +0009.46     +04.72%
Typed Vector 3 Array                              214.50          214.16   +0000.34     +00.16%
Untyped Color Array                               253.72          257.50   -0003.78     -01.47%
Untyped Float Array                               212.50          211.98   +0000.52     +00.25%
Untyped Int Array                                 214.48          212.42   +0002.06     +00.97%
Untyped String Array                              660.20          658.04   +0002.16     +00.33%
Untyped Vector 2 Array                            242.44          241.40   +0001.04     +00.43%
Untyped Vector 3 Array                            249.92          248.04   +0001.88     +00.76%
Binary Trees 13                                   527.26          529.46   -0002.20     -00.42%
Binary Trees 15                                  2441.00         2470.20   -0029.20     -01.18%
For Loop Add                                        8.11            7.85   +0000.25     +03.21%
For Loop Call                                      62.61           60.69   +0001.92     +03.16%
Lambda Call                                        46.62           45.11   +0001.51     +03.35%
Mandelbrot Set                                   2430.80         2298.20   +0132.60     +05.77%
Merkle Trees 13                                  1635.80         1637.60   -0001.80     -00.11%
Merkle Trees 15                                  7879.20         7941.80   -0062.60     -00.79%
Nbody 1 000 000                                  5383.20         5161.60   +0221.60     +04.29%
Nbody 500 000                                    2700.60         2589.40   +0111.20     +04.29%
Spectral Norm 100                                  50.07           49.70   +0000.37     +00.75%
Spectral Norm 1000                               4604.60         4645.80   -0041.20     -00.89%
Spectral Norm 500                                1170.60         1160.80   +0009.80     +00.84%
Md 5 Buffer Empty                                 206.84          206.24   +0000.60     +00.29%
Md 5 Buffer Non Empty                             674.36          678.36   -0004.00     -00.59%
Md 5 Text Empty                                   172.76          173.10   -0000.34     -00.20%
Md 5 Text Non Empty                               643.18          641.78   +0001.40     +00.22%
Sha 1 Buffer Empty                                190.72          189.10   +0001.62     +00.86%
Sha 1 Buffer Non Empty                            590.30          595.74   -0005.44     -00.91%
Sha 1 Text Empty                                  161.20          153.54   +0007.66     +04.99%
Sha 1 Text Non Empty                              563.54          552.86   +0010.68     +01.93%
Sha 256 Buffer Empty                              264.68          267.08   -0002.40     -00.90%
Sha 256 Buffer Non Empty                          894.52          900.94   -0006.42     -00.71%
Sha 256 Text Empty                                235.52          238.78   -0003.26     -01.37%
Sha 256 Text Non Empty                            866.80          887.90   -0021.10     -02.38%
Complex Variable Concatenate                     1522.40         1549.60   -0027.20     -01.76%
Complex Variable Method                          2647.60         2839.40   -0191.80     -06.75%
Complex Variable Percent                         2644.60         2663.60   -0019.00     -00.71%
No Op Constant Method                             140.18          139.24   +0000.94     +00.68%
Simple Constant Concatenate                         4.61            4.58   +0000.03     +00.68%
Simple Constant Method                            554.14          554.48   -0000.34     -00.06%
Simple Constant Method Constant Dict              325.68          330.22   -0004.54     -01.37%
Simple Constant Percent                             4.64            4.61   +0000.03     +00.56%
Simple Variable Concatenate                       156.78          152.62   +0004.16     +02.73%
Simple Variable Method                            556.10          556.86   -0000.76     -00.14%
Simple Variable Percent                           421.62          426.90   -0005.28     -01.24%
Begins With                                        11.15           11.08   +0000.07     +00.65%
Bigrams                                           722.12          938.22   -0216.10     -23.03%
Capitalize                                        968.02         1018.50   -0050.48     -04.96%
Casecmp To                                          8.82            8.78   +0000.04     +00.48%
Contains                                            9.56            9.55   +0000.01     +00.11%
Contains Gdscript In                                4.64            4.62   +0000.02     +00.42%
Count                                              55.79           52.50   +0003.30     +06.28%
Countn                                            246.92          249.50   -0002.58     -01.03%
Ends With                                          11.02           10.89   +0000.13     +01.21%
Find                                               35.22           35.16   +0000.07     +00.19%
Findn                                              65.64           64.80   +0000.83     +01.29%
Get Slice                                          70.78           71.15   -0000.36     -00.51%
Get Slice Count                                    21.44           21.60   -0000.16     -00.74%
Humanize Size                                     701.94          710.34   -0008.40     -01.18%
Insert                                             62.57           61.45   +0001.12     +01.82%
Is Valid Filename                                  23.11           22.92   +0000.19     +00.81%
Lpad                                              138.40          137.68   +0000.72     +00.52%
Naturalnocasecmp To                                12.27           12.52   -0000.24     -01.95%
Nocasecmp To                                       37.30           37.44   -0000.13     -00.35%
Pad Decimals                                      439.60          438.46   +0001.14     +00.26%
Pad Decimals Pre Constructed                       72.41           72.88   -0000.47     -00.65%
Pad Zeros                                         251.22          251.72   -0000.50     -00.20%
Pad Zeros Pre Constructed                         164.02          166.06   -0002.04     -01.23%
Rfind                                              40.54           42.24   -0001.70     -04.03%
Rfindn                                            183.64          179.74   +0003.90     +02.17%
Rpad                                              126.02          127.42   -0001.40     -01.10%
Rsplit                                            401.38          402.78   -0001.40     -00.35%
Similarity                                         22.96           22.68   +0000.28     +01.22%
Simplify Path                                    1189.40         1392.20   -0202.80     -14.57%
Split                                             383.92          379.06   +0004.86     +01.28%
Split Floats                                      320.14          317.98   +0002.16     +00.68%
Substr                                             60.69           59.76   +0000.93     +01.56%
To Camel Case                                     627.48          660.94   -0033.46     -05.06%
To Lower                                          195.54          213.44   -0017.90     -08.39%
To Pascal Case                                    909.52          949.96   -0040.44     -04.26%
To Snake Case                                     869.56          887.76   -0018.20     -02.05%
To Utf 16 Buffer                                  147.04          146.64   +0000.40     +00.27%
To Utf 32 Buffer                                   98.14           99.23   -0001.09     -01.10%
To Utf 8 Buffer                                   142.52          144.56   -0002.04     -01.41%
To Wchar Buffer                                   145.28          148.20   -0002.92     -01.97%
Uri Decode                                        489.94          500.06   -0010.12     -02.02%
Uri Encode                                        419.00          444.62   -0025.62     -05.76%
Validate Filename                                 304.42          307.32   -0002.90     -00.94%
Validate Node Name                                 72.48           73.65   -0001.17     -01.59%
Xml Escape                                        427.68          439.72   -0012.04     -02.74%
Xml Unescape                                       76.40           75.42   +0000.98     +01.29%
Both Clean And Simplify                          5957.80         6026.00   -0068.20     -01.13%
Only Clean                                       5953.20         5939.00   +0014.20     +00.24%
Only Simplify                                      14.68           16.06   -0001.38     -08.57%
Quickest                                           13.34           17.42   -0004.07     -23.38%
Delaunay 2d                                       119.98          114.24   +0005.74     +05.02%
Delaunay 3d                                       611.70          599.80   +0011.90     +01.98%
Parse 20 Complex Expressions Cold                 291.12          289.98   +0001.14     +00.39%
Parse 20 Complex Expressions With Cached Parse          286.76          286.24   +0000.52     +00.18%
Parse 20 Complex Expressions With Cached Parse Then Execute          292.96          291.74   +0001.22     +00.42%
Cellular                                         1152.80         1168.20   -0015.40     -01.32%
Perlin                                            406.08          405.02   +0001.06     +00.26%
Simplex                                           408.02          411.92   -0003.90     -00.95%
Simplex Smooth                                    500.98          509.06   -0008.08     -01.59%
Value                                             403.38          401.82   +0001.56     +00.39%
Value Cubic                                       710.92          710.00   +0000.92     +00.13%
Triangulate                                        94.96           94.78   +0000.18     +00.19%
Astar 5000 Times 5000 Points                      424.40          422.18   +0002.22     +00.53%
Astar 1000 Times 1000 Points                      564.24          563.44   +0000.80     +00.14%
1000 Moving Agents                                  0.02            0.02   -0000.00     -03.70%
Navigation 10 000 Random Paths                     10.90           11.66   -0000.76     -06.56%
Add Children With Same Name                        50.78           49.72   +0001.07     +02.14%
Add Children Without Name                          56.67           52.97   +0003.70     +06.98%
Delete Children In Order                           10.99           10.96   +0000.03     +00.24%
Delete Children Random Order                       13.13           13.21   -0000.08     -00.61%
Delete Children Reverse Order                       6.68            6.44   +0000.24     +03.76%
Get Node                                           30.69           30.06   +0000.62     +02.07%
Move Children                                     709.00          746.72   -0037.72     -05.05%
Activate 1024 Viewports                           227.02          222.78   +0004.24     +01.90%
Activate 256 Viewports                            120.64          120.58   +0000.06     +00.05%
Activate 64 Viewports                               6.80            6.72   +0000.08     +01.16%
Create 1024 Viewports                             128.40          122.40   +0006.00     +04.90%
Create 256 Viewports                               50.93           50.49   +0000.44     +00.88%
Create 64 Viewports                                 3.51            3.26   +0000.25     +07.74%
Sum of all tests                                97692.78        97476.29   +0216.49     +00.22%

New:

Benchmarks (5 runs average)

Result

raw data:
ma2.jsonl.txt
pr2.jsonl.txt

                                                     pr2             ma2       diff    percent
Export Gltf                                       103.62          104.24   -0000.62     -00.59%
Import Fbx                                        513.02          512.04   +0000.98     +00.19%
Import Gltf                                        55.36           52.80   +0002.55     +04.84%
Import Ogg Audio                                    9.54            6.55   +0002.98     +45.50%
Import Webp Images                                817.80          817.82   -0000.02     -00.00%
Append Array                                      162.14          165.64   -0003.50     -02.11%
Bsearch                                           101.20          101.04   +0000.16     +00.16%
Fill                                               59.06           47.44   +0011.62     +24.50%
Reverse                                            25.94           25.75   +0000.19     +00.73%
Function Callable                                 306.38          313.04   -0006.66     -02.13%
Lambda Inline Callable                            373.94          390.02   -0016.08     -04.12%
Lambda Variable Callable                           89.27           87.42   +0001.85     +02.12%
Load                                               34.84           35.24   -0000.39     -01.12%
Load With Password                                 39.41           38.13   +0001.28     +03.35%
Save                                               33.66           68.80   -0035.14     -51.07%
Save With Password                                 38.37           37.72   +0000.65     +01.73%
Generate 1g Random Bytes 1k At A Time            1243.60         1266.20   -0022.60     -01.78%
Generate 1m Random Bytes 10 At A Time            2187.60         2241.40   -0053.80     -02.40%
Create                                             17.03           17.29   -0000.26     -01.53%
12 Threads Full Contention                        821.22          807.16   +0014.06     +01.74%
12 Threads Half Contention                        427.02          423.14   +0003.88     +00.92%
12 Threads Little Contention                      103.67          104.50   -0000.83     -00.79%
12 Threads Slope Contention                       456.74          455.90   +0000.84     +00.18%
2 Threads Full Contention                          61.80           62.97   -0001.17     -01.86%
2 Threads Half Contention                          43.59           44.13   -0000.54     -01.23%
2 Threads Little Contention                        29.75           28.90   +0000.85     +02.96%
4 Threads Full Contention                         191.52          194.72   -0003.20     -01.64%
4 Threads Half Contention                         107.94          109.54   -0001.60     -01.46%
4 Threads Little Contention                        41.89           42.36   -0000.47     -01.10%
4 Threads Slope Contention                        146.28          141.76   +0004.52     +03.19%
8 Threads Full Contention                         462.72          454.54   +0008.18     +01.80%
8 Threads Half Contention                         247.26          250.90   -0003.64     -01.45%
8 Threads Little Contention                        67.80           69.41   -0001.61     -02.33%
8 Threads Slope Contention                        289.34          298.26   -0008.92     -02.99%
Single                                             24.42           23.92   +0000.50     +02.11%
Global Scope Randf                                109.24          109.62   -0000.38     -00.35%
Global Scope Randf Range                          362.28          361.48   +0000.80     +00.22%
Global Scope Randfn                               864.68          854.30   +0010.38     +01.22%
Global Scope Randi                                103.12          103.22   -0000.10     -00.10%
Global Scope Randi Range                          140.64          142.06   -0001.42     -01.00%
Global Scope Randomize                            785.32          789.24   -0003.92     -00.50%
Randf                                             269.80          268.38   +0001.42     +00.53%
Randf Range                                       294.08          294.84   -0000.76     -00.26%
Randfn                                            825.10          807.46   +0017.64     +02.18%
Randi                                              91.86           85.78   +0006.07     +07.08%
Randi Range                                       116.50          111.94   +0004.56     +04.07%
Randomize                                         808.56          808.92   -0000.36     -00.04%
Emission Params 0                                 214.14          216.82   -0002.68     -01.24%
Emission Params 1                                 216.16          217.04   -0000.88     -00.41%
Emission Params 10                                255.48          248.72   +0006.76     +02.72%
Deep Tree                                         127.26          129.26   -0002.00     -01.55%
Duplicate                                        1125.00         1131.20   -0006.20     -00.55%
Fragmentation                                    1229.80         1245.40   -0015.60     -01.25%
Wide Tree                                         121.28          124.18   -0002.90     -02.34%
Fill Loop                                         240.92          240.10   +0000.82     +00.34%
Fill Method                                        98.78          100.45   -0001.67     -01.66%
Packed Color Array                                145.72          146.38   -0000.66     -00.45%
Packed Float 32 Array                             108.22          115.38   -0007.16     -06.21%
Packed Float 64 Array                             113.42          111.50   +0001.92     +01.72%
Packed Int 32 Array                                90.35           90.55   -0000.21     -00.23%
Packed Int 64 Array                                93.45           93.27   +0000.18     +00.20%
Packed String Array                               524.88          523.20   +0001.68     +00.32%
Packed Vector 2 Array                             121.02          121.90   -0000.88     -00.72%
Packed Vector 3 Array                             137.30          137.42   -0000.12     -00.09%
Typed Color Array                                 219.04          218.68   +0000.36     +00.16%
Typed Float Array                                 183.20          177.68   +0005.52     +03.11%
Typed Int Array                                   173.44          172.26   +0001.18     +00.69%
Typed String Array                                614.34          621.84   -0007.50     -01.21%
Typed Vector 2 Array                              202.36          199.42   +0002.94     +01.47%
Typed Vector 3 Array                              217.34          210.10   +0007.24     +03.45%
Untyped Color Array                               261.36          255.68   +0005.68     +02.22%
Untyped Float Array                               215.64          208.64   +0007.00     +03.36%
Untyped Int Array                                 215.30          208.68   +0006.62     +03.17%
Untyped String Array                              665.52          664.30   +0001.22     +00.18%
Untyped Vector 2 Array                            242.60          237.60   +0005.00     +02.10%
Untyped Vector 3 Array                            252.96          247.26   +0005.70     +02.31%
Binary Trees 13                                   518.74          519.14   -0000.40     -00.08%
Binary Trees 15                                  2427.20         2433.60   -0006.40     -00.26%
For Loop Add                                        7.91            7.97   -0000.06     -00.77%
For Loop Call                                      60.64           60.82   -0000.18     -00.30%
Lambda Call                                        44.43           45.40   -0000.97     -02.13%
Mandelbrot Set                                   2352.20         2326.00   +0026.20     +01.13%
Merkle Trees 13                                  1630.00         1632.00   -0002.00     -00.12%
Merkle Trees 15                                  8120.20         8089.20   +0031.00     +00.38%
Nbody 1 000 000                                  5224.40         5388.60   -0164.20     -03.05%
Nbody 500 000                                    2651.80         2695.40   -0043.60     -01.62%
Spectral Norm 100                                  49.83           49.81   +0000.01     +00.03%
Spectral Norm 1000                               4556.80         4546.80   +0010.00     +00.22%
Spectral Norm 500                                1159.60         1171.20   -0011.60     -00.99%
Md 5 Buffer Empty                                 208.00          211.28   -0003.28     -01.55%
Md 5 Buffer Non Empty                             672.60          680.74   -0008.14     -01.20%
Md 5 Text Empty                                   172.24          175.28   -0003.04     -01.73%
Md 5 Text Non Empty                               629.54          650.24   -0020.70     -03.18%
Sha 1 Buffer Empty                                186.04          188.26   -0002.22     -01.18%
Sha 1 Buffer Non Empty                            593.46          589.50   +0003.96     +00.67%
Sha 1 Text Empty                                  151.44          155.70   -0004.26     -02.74%
Sha 1 Text Non Empty                              549.84          562.30   -0012.46     -02.22%
Sha 256 Buffer Empty                              266.46          278.10   -0011.64     -04.19%
Sha 256 Buffer Non Empty                          913.30          939.70   -0026.40     -02.81%
Sha 256 Text Empty                                240.50          242.86   -0002.36     -00.97%
Sha 256 Text Non Empty                            885.76          912.24   -0026.48     -02.90%
Complex Variable Concatenate                     1544.60         1574.40   -0029.80     -01.89%
Complex Variable Method                          2662.40         2709.20   -0046.80     -01.73%
Complex Variable Percent                         2838.60         2717.40   +0121.20     +04.46%
No Op Constant Method                             138.28          142.78   -0004.50     -03.15%
Simple Constant Concatenate                         4.56            4.61   -0000.05     -01.04%
Simple Constant Method                            722.00          557.76   +0164.24     +29.45%
Simple Constant Method Constant Dict              322.78          327.78   -0005.00     -01.53%
Simple Constant Percent                             4.54            4.52   +0000.02     +00.50%
Simple Variable Concatenate                       154.84          152.54   +0002.30     +01.51%
Simple Variable Method                            549.30          559.84   -0010.54     -01.88%
Simple Variable Percent                           408.04          410.16   -0002.12     -00.52%
Begins With                                        11.15           11.00   +0000.15     +01.36%
Bigrams                                           581.42          776.06   -0194.64     -25.08%
Capitalize                                       1008.18         1026.22   -0018.04     -01.76%
Casecmp To                                          8.83            8.86   -0000.04     -00.41%
Contains                                            9.59            9.50   +0000.09     +00.93%
Contains Gdscript In                                4.62            4.64   -0000.02     -00.51%
Count                                              51.28           52.21   -0000.92     -01.77%
Countn                                            245.22          250.22   -0005.00     -02.00%
Ends With                                          11.27           10.85   +0000.42     +03.89%
Find                                               35.71           35.22   +0000.49     +01.39%
Findn                                              64.88           65.53   -0000.65     -00.99%
Get Slice                                          71.54           74.86   -0003.32     -04.44%
Get Slice Count                                    22.57           21.68   +0000.89     +04.11%
Humanize Size                                     700.84          738.68   -0037.84     -05.12%
Insert                                             62.02           61.24   +0000.79     +01.29%
Is Valid Filename                                  23.40           22.51   +0000.89     +03.95%
Lpad                                              137.24          137.96   -0000.72     -00.52%
Naturalnocasecmp To                                11.88           12.30   -0000.42     -03.38%
Nocasecmp To                                       38.04           37.33   +0000.71     +01.90%
Pad Decimals                                      435.84          452.06   -0016.22     -03.59%
Pad Decimals Pre Constructed                       70.70           70.92   -0000.22     -00.30%
Pad Zeros                                         248.68          256.64   -0007.96     -03.10%
Pad Zeros Pre Constructed                         164.20          165.52   -0001.32     -00.80%
Rfind                                              40.32           39.61   +0000.71     +01.79%
Rfindn                                            184.68          181.82   +0002.86     +01.57%
Rpad                                              126.30          127.02   -0000.72     -00.57%
Rsplit                                            411.20          405.22   +0005.98     +01.48%
Similarity                                         23.23           23.05   +0000.18     +00.77%
Simplify Path                                    1227.40         1231.00   -0003.60     -00.29%
Split                                             384.06          385.96   -0001.90     -00.49%
Split Floats                                      322.08          323.12   -0001.04     -00.32%
Substr                                             59.96           60.25   -0000.29     -00.48%
To Camel Case                                     656.22          665.88   -0009.66     -01.45%
To Lower                                          197.96          215.34   -0017.38     -08.07%
To Pascal Case                                    928.56          942.82   -0014.26     -01.51%
To Snake Case                                     896.30          897.14   -0000.84     -00.09%
To Utf 16 Buffer                                  145.74          147.24   -0001.50     -01.02%
To Utf 32 Buffer                                   98.07           99.81   -0001.74     -01.74%
To Utf 8 Buffer                                   143.50          142.40   +0001.10     +00.77%
To Wchar Buffer                                   148.42          147.72   +0000.70     +00.47%
Uri Decode                                        488.90          497.78   -0008.88     -01.78%
Uri Encode                                        419.86          431.78   -0011.92     -02.76%
Validate Filename                                 304.04          311.20   -0007.16     -02.30%
Validate Node Name                                 73.00           73.94   -0000.94     -01.28%
Xml Escape                                        431.62          448.00   -0016.38     -03.66%
Xml Unescape                                       75.21           84.01   -0008.80     -10.47%
Both Clean And Simplify                          5894.80         5980.80   -0086.00     -01.44%
Only Clean                                       5859.20         5856.40   +0002.80     +00.05%
Only Simplify                                      15.76           14.82   +0000.94     +06.37%
Quickest                                           13.71           21.27   -0007.56     -35.56%
Delaunay 2d                                       113.44          118.06   -0004.62     -03.91%
Delaunay 3d                                       602.58          620.18   -0017.60     -02.84%
Parse 20 Complex Expressions Cold                 286.30          295.36   -0009.06     -03.07%
Parse 20 Complex Expressions With Cached Parse          286.24          291.08   -0004.84     -01.66%
Parse 20 Complex Expressions With Cached Parse Then Execute          289.74          294.42   -0004.68     -01.59%
Cellular                                         1155.40         1151.40   +0004.00     +00.35%
Perlin                                            406.90          409.50   -0002.60     -00.63%
Simplex                                           413.62          416.36   -0002.74     -00.66%
Simplex Smooth                                    496.04          493.54   +0002.50     +00.51%
Value                                             403.10          406.24   -0003.14     -00.77%
Value Cubic                                       704.56          715.60   -0011.04     -01.54%
Triangulate                                        93.32           93.38   -0000.07     -00.07%
Astar 5000 Times 5000 Points                      426.62          417.32   +0009.30     +02.23%
Astar 1000 Times 1000 Points                      565.94          574.08   -0008.14     -01.42%
1000 Moving Agents                                  0.02            0.02   -0000.00     -03.66%
Navigation 10 000 Random Paths                     10.57           10.90   -0000.33     -03.06%
Add Children With Same Name                        48.64           48.76   -0000.12     -00.24%
Add Children Without Name                          51.96           53.14   -0001.18     -02.22%
Delete Children In Order                           10.93           10.88   +0000.05     +00.44%
Delete Children Random Order                       14.04           13.21   +0000.83     +06.27%
Delete Children Reverse Order                       6.54            6.41   +0000.13     +02.10%
Get Node                                           29.87           30.64   -0000.77     -02.51%
Move Children                                     762.30          759.38   +0002.92     +00.38%
Activate 1024 Viewports                           224.86          233.56   -0008.70     -03.72%
Activate 256 Viewports                            116.26          119.14   -0002.88     -02.42%
Activate 64 Viewports                               6.67            6.40   +0000.28     +04.32%
Create 1024 Viewports                             120.44          123.18   -0002.74     -02.22%
Create 256 Viewports                               48.04           51.60   -0003.56     -06.89%
Create 64 Viewports                                 3.13            3.18   -0000.05     -01.57%
Sum of all tests                                97842.35        98530.31   -0687.96     -00.70%

Notes

The main merit you could expect from this PR is reduced binary size, as it's unlikely that we’ll repeatedly create String/StringName from string literals on hot paths (you can always cache the results). However, compile-time computation can eliminate the constructor code for String/StringName, reducing the size of the .text section, and in some cases, it can also replace more complex templates.

TODO

  • Discuss naming and implementing details.
  • Try to get a more stable benchmark result.
  • Find more code which can benifit from this.
  • Technically we can do same thing for Vector with simple types, but I haven't find usecases yet.
  • Handle previously static StringName properly.

@YYF233333 YYF233333 force-pushed the comptime_string branch 2 times, most recently from 40050bf to b05c112 Compare July 23, 2025 09:39
@Calinou Calinou added this to the 4.x milestone Jul 24, 2025
@YYF233333 YYF233333 force-pushed the comptime_string branch 3 times, most recently from 2b14769 to 8c092ac Compare July 26, 2025 06:02
@Ivorforce
Copy link
Copy Markdown
Member

Hi, this is an interesting proposition that we've been discussing a few times!

I have also made an attempt at implementing it, which you may want to have a look at: https://github.com/Ivorforce/godot/tree/constexpr-string

I don't exactly remember the outcome, but I believe the main problem I encountered was that users needed to use a custom macro (SString) to make use of constexpr strings. Ideally, the constexpr constructor would be automatically chosen for char[] values, although I don't know if that's possible (mind you, I haven't looked at your implementation yet!).

@YYF233333
Copy link
Copy Markdown
Contributor Author

I don't exactly remember the outcome, but I believe the main problem I encountered was that users needed to use a custom macro (SString) to make use of constexpr strings. Ideally, the constexpr constructor would be automatically chosen for char[] values, although I don't know if that's possible (mind you, I haven't looked at your implementation yet!).

Yes, I encountered the same issue. I believe the key here is that we must somehow write static constexpr str_storage = "literals";, because currently we cannot dynamically allocate memory at compile time, so the user must provide external memory to store the string. I provide a template struct ComptimeString which is basicly identical to your macro. Haven't find a way to automatically convert string literals to constexpr String yet as it is not possible now to get the space for refcount and size in functions.

@YYF233333 YYF233333 force-pushed the comptime_string branch 2 times, most recently from a1a0863 to c2b71f0 Compare August 3, 2025 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants