Skip to content

Conversation

ericproulx
Copy link
Contributor

@ericproulx ericproulx commented Jul 13, 2025

Optimize API Parameter Documentation and Memory Usage

Motivation

The current implementation of parameter documentation in Grape can lead to unnecessary memory usage, especially for internal APIs where documentation is not required. This PR aims to optimize the way parameters are documented and stored, reducing memory footprint and improving efficiency.

Summary of Changes

  • Refactored ParamsDocumentation module in lib/grape/validations/params_documentation.rb to:
    • Only document parameters when documentation is needed (i.e., when do_not_document is not set in the namespace).
    • Clean up attributes to avoid storing unnecessary data in RAM for internal or undocumented APIs.
    • Use a more efficient TypeCache for type stringification, reducing repeated computation and memory usage.
    • Improved the extraction of parameter details, ensuring only relevant information is kept and documented.
    • Added support for except_values in parameter documentation, which was previously missing. Now, parameters that specify except_values will have this information included in the documentation output.
  • The changes are fully backward compatible for users who rely on parameter documentation, but provide a significant optimization for those who do not.

Impact

  • Performance: Reduces memory usage for APIs that do not require parameter documentation.
  • Maintainability: The code is now cleaner and easier to extend for future documentation or validation features.
  • Documentation Completeness: Parameters with except_values are now properly documented, improving the accuracy and completeness of generated API docs.
  • Backward Compatibility: No breaking changes; all existing documentation features remain intact for public APIs.

Additional Notes

  • This PR does not change any public API or configuration. It is an internal optimization.
  • All existing tests pass, and no user-facing behavior is altered except for improved efficiency and more complete documentation.

@grape-bot
Copy link

grape-bot commented Jul 13, 2025

1 Warning
⚠️ There're library changes, but not tests. That's OK as long as you're refactoring existing code.

Generated by 🚫 Danger

@ericproulx ericproulx force-pushed the optimize_api_documentation branch from 0c83482 to 04ab8f1 Compare July 13, 2025 14:03
@ericproulx ericproulx requested a review from dblock July 13, 2025 14:04
@dblock dblock merged commit 49e8e63 into ruby-grape:master Jul 13, 2025
47 checks passed
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