Skip to content

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

Closed
razvanalex wants to merge 7 commits intogoogle:masterfrom
razvanalex:php-finish-prefix-size
Closed

[PHP] Use IDL namer in generator and add prefixed size, clear, and createBytesVector in builder#8202
razvanalex wants to merge 7 commits intogoogle:masterfrom
razvanalex:php-finish-prefix-size

Conversation

@razvanalex
Copy link
Contributor

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

  • in PHP builder: finish with prefixed size, clear, and createBytesVector methods. I drew inspiration from TS.
  • added PHP IDL generator namer; partially addressed in NamerPhp #7491, and I continued that work.
  • for-loops replaced with substr_replace in createString and growByteBuffer. createBytesVector also uses this.

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

Some languages (such as PHP) are more restrictive and don't allow the
use of keywords, despite their casing in variables, functions, classes,
etc. names. This commit adds the keywords_case_sensitive config for IDL
namer. By default it is set to true, as of the languages that use the
IDL namer don't require this check.
Some languages use the IDL interface to generate the method/varible/etc
names. This commit adds the IDL namer to php, basically continuing the
workf from PR google#7491.

Now, each method uses namer_.Method, while the legacy stuff is kept for
backwards compatibility. At the moment, PHP is the only language that
uses the case sensitive check when generating the names.
Clear and createBytesVector are implemented in other languages, and this
commit brings these functions into php. Clear method just resets the
internal buffer state while createBytesVector is a variation on
createString that allows non-UTF8 data.

Also, performance was improved by using substr_replace instead of a
for-loop.
Applies the TODO about when expanding the byte buffer and it is needed
to copy the content into the new buffer. substr_replace seems to be much
faster, especially when considering longer strings. Below are some
benchmarks:

Before:
``` txt
BenchmarkCreateString/1 0.0019645690917969 ms
BenchmarkCreateString/10        0.0022602081298828 ms
BenchmarkCreateString/100       0.0092792510986328 ms
BenchmarkCreateString/1000      0.068621635437012 ms
BenchmarkCreateString/10000     0.77774047851562 ms
BenchmarkCreateString/100000    7.1578598022461 ms
BenchmarkCreateString/1000000   66.291980743408 ms
BenchmarkCreateString/10000000  789.96389865875 ms
```

After:
``` txt
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
```
On Windows, the compiler generates a warning which is treated as error:
D:\a\flatbuffers\flatbuffers\src\namer.h(216,28): error C2220: the following warning is treated as an error [D:\a\flatbuffers\flatbuffers\flatc.vcxproj]
D:\a\flatbuffers\flatbuffers\src\namer.h(216,28): warning C4244: '=': conversion from 'int' to 'char', possible loss of data [D:\a\flatbuffers\flatbuffers\flatc.vcxproj]

Enforcing char should silence the warning.
This fixes the name generation for methods for start, create, finish,
end, and identifier, to use the old naming conventions, instead of lower
camel. Also, it considers the escaped keywords. For example, `string`
would yield to `String_::startString_()`, `String_::getRootAsString_()`,
or `String_::finishString_Buffer()`.
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added the stale label Sep 24, 2024
razvanalex pushed a commit to razvanalex/flatbuffers that referenced this pull request Oct 7, 2024
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
@razvanalex
Copy link
Contributor Author

Will close, as I will split this PR in smaller ones.

@razvanalex razvanalex closed this Oct 8, 2024
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Oct 8, 2024
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Jul 6, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Jul 6, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Nov 27, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Nov 27, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Dec 10, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Dec 10, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
razvanalex added a commit to razvanalex/flatbuffers that referenced this pull request Dec 21, 2025
This refactors the code from google#8202, specifically the
part related to using the NamerPhp.
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.

1 participant