google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.17k stars 3.24k forks source link

[PHP] Use IDL namer in generator and add prefixed size, clear, and createBytesVector in builder #8202

Open razvanalex opened 9 months ago

razvanalex commented 9 months ago

This PR adds some missing functionality and improved performance to PHP flatbuffers:

During internal testing, I noticed that tables using PHP keywords as names (e.g., bytes, string) would yield errors in the generated code because keywords cannot be used as class names despite their casing (also mentioned in PHP docs). Therefore, I added a check (see commit a58f40c) when escaping keywords, which generally is disabled, except for IDL PHP generator.

Also, when I replaced for-loops with substr_replace in createString and growByteBuffer, the performance improved, especially for longer strings. Below is a benchmark (here is the code).

Before the change:

BenchmarkCreateString/1 0.0019979476928711 ms
BenchmarkCreateString/10        0.0022983551025391 ms
BenchmarkCreateString/100       0.010838508605957 ms
BenchmarkCreateString/1000      0.083937644958496 ms
BenchmarkCreateString/10000     0.96836090087891 ms
BenchmarkCreateString/100000    8.6467790603638 ms
BenchmarkCreateString/1000000   82.010021209717 ms
BenchmarkCreateString/10000000  936.62708282471 ms

After the change:

BenchmarkCreateString/1 0.0019216537475586 ms
BenchmarkCreateString/10        0.0020408630371094 ms
BenchmarkCreateString/100       0.0075387954711914 ms
BenchmarkCreateString/1000      0.053601264953613 ms
BenchmarkCreateString/10000     0.52011966705322 ms
BenchmarkCreateString/100000    4.9904012680054 ms
BenchmarkCreateString/1000000   50.608458518982 ms
BenchmarkCreateString/10000000  506.61317825317 ms
github-actions[bot] commented 1 week ago

This pull request is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.