Repository navigation
RFC: Update route syntax - #10
Conversation
|
After conducting a series of implementation experiments based on the RFC proposal to update the route declaration format, there is a significant performance impact when mixing static and named parameters within a single route segment, specifically when the static part follows the named parameter and the node in question has For example, consider the following route declarations: In this context, when processing a request such as for i := 0; i < len(remainingCharsInPath); i++ {
idx := linearSearch(current.childKeys, remainingCharsInPath[i])
if idx >= 0 {
break
}
charsMatched++
}The impact becomes more significant as the segment to match increases in size and the number of child node grow. To mitigate this performance impact, the router must enforces certain restrictions on the route format, such as allowing only one parameter per path segment and requiring that a parameter be directly followed by a slash or the end of the route. These restrictions simplify the matching process when attempting to identify the end of a parameter within a path segment. By knowing that a parameter is always followed by a slash or the end of the route, the router can quickly determine the boundary of the parameter and reduce the amount of searching required, leading to better overall performance. |
f4753de to
e83a755
Compare
Benchmark shows a 4.02% overall performance improvement. No significant change observed for static route and small improvement on wildcard route. |
Codecov Report
@@ Coverage Diff @@
## master #10 +/- ##
==========================================
+ Coverage 87.55% 87.89% +0.34%
==========================================
Files 8 8
Lines 1221 1231 +10
==========================================
+ Hits 1069 1082 +13
+ Misses 128 123 -5
- Partials 24 26 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Introduction
This RFC proposes to update the current route declaration format for named and catch-all parameters by using
{}and*{} or {*}as delimiters instead of:and*. This change aims to improve compatibility with common URL patterns and provide greater flexibility for mixing static and named route segments.Motivation
The current route declaration format using
:for named parameters and*for catch-all parameters has some limitations in terms of readability, especially when working with certain API patterns. For instance,/users/uid:1234/profileis relatively common pattern in APIs, and the proposed change would make it easier to define such routes by providing more intuitive delimiters (e.g./users/uid:{id}/profile).Additionally, the current format limits the flexibility to mix static and named route segments in the same path segment. For example, consider the
route /date/{year}-{month}-{day}. With the current format, this would have to be defined as/date/:ymdand require additional parsing of theymdparameter to extract the year, month, and day. The proposed format with{}delimiters allows for more intuitive and straightforward route definitions. In the new format, each named parameter is defined explicitly and can be accessed directly without the need for additional parsing.The new syntax also paves the way for more advanced routing features, such as incorporating regular expressions directly into route definitions (e.g
/users/{id:[0-9]+}).Design
The proposed update involves changing the route declaration format for named and catch-all parameters as follows:
:with{}as the delimiter for named parameters.*with*{}or{*}as the delimiter for catch-all parameters.Examples
Current declaration:
Updated declaration:
Direct benefits
This new design would also provide more flexible route declaration
/datetime/{year}-{month}-{day}T{hour}:{minute}:{second}Example:
/datetime/2023-03-26T18:30:15/geo/{latitude},{longitude}Example:
/geo/37.7749,-122.4194/products/{product_id}-size{size}-color{color}Example:
/products/1234-sizeM-colorRed/users/{user_id}@{domain}/profileExample:
/users/1234@example.com/profileDrawbacks
Conclusion
The proposed new syntax aims to enhance readability and flexibility when declaring routes. At the same time, the intention is to improve the routing experience without compromising efficiency and speed. As such, the acceptance of this proposal is contingent upon ensuring that the impact on performance remains minimal to negligible.