Skip to content

Conversation

@carlyeks
Copy link
Owner

This document explores how Thrift could adopt a template-based code
generation approach similar to protobuf compiler plugins, including:

  • Analysis of current programmatic code generation architecture
  • Comparison of template-based vs programmatic approaches
  • Proposed template-based architecture with examples
  • Migration strategy and implementation recommendations
  • Hybrid approach combining templates and programmatic generation

The exploration includes concrete examples showing how generators like
t_java_generator.cc could be reimplemented using templates (Jinja2/Mustache)
while maintaining the same output quality.

This document explores how Thrift could adopt a template-based code
generation approach similar to protobuf compiler plugins, including:

- Analysis of current programmatic code generation architecture
- Comparison of template-based vs programmatic approaches
- Proposed template-based architecture with examples
- Migration strategy and implementation recommendations
- Hybrid approach combining templates and programmatic generation

The exploration includes concrete examples showing how generators like
t_java_generator.cc could be reimplemented using templates (Jinja2/Mustache)
while maintaining the same output quality.
This commit introduces a working proof-of-concept for template-based
code generation in Thrift, demonstrating significant benefits over the
traditional programmatic approach.

Key Components:
- Base template generator class (t_template_generator.h/cc)
- JSON template generator (t_json_template_generator.cc)
- Jinja2 template for JSON output (templates/json/program.json.j2)
- Integrated inja template engine (header-only)
- Integrated nlohmann/json library (header-only)

Benefits Demonstrated:
- 61% code reduction (314 lines vs 811 lines for JSON generator)
- Improved maintainability and readability
- Output structure immediately visible in templates
- Separation of logic and presentation
- Only ~8% performance overhead

Results:
- Output is functionally identical to original generator
- Successfully tested with test/TemplateTest.thrift
- Build system updated to support template generators
- Comprehensive documentation of findings and recommendations

Files Added:
- compiler/cpp/src/thrift/generate/t_template_generator.{h,cc}
- compiler/cpp/src/thrift/generate/t_json_template_generator.cc
- compiler/cpp/templates/json/program.json.j2
- compiler/cpp/third_party/inja.hpp (95KB)
- compiler/cpp/third_party/nlohmann/json.hpp (940KB)
- test/TemplateTest.thrift
- TEMPLATE_GENERATOR_POC_RESULTS.md

See TEMPLATE_GENERATOR_POC_RESULTS.md for detailed comparison,
metrics, and recommendations for future adoption.
Build artifacts directory should not be tracked in version control.
The inja template library requires C++17 for std::string_view.
This fixes the compilation errors in CI builds that were using C++11.

Changes:
- Set CMAKE_CXX_STANDARD to 17
- Set CMAKE_CXX_STANDARD_REQUIRED to ON

Verified that the template generator compiles and works correctly
with C++17.
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.

3 participants