Skip to content

perf(urls): do not instantiate routers each time NewBuilder is called

Hayley Swimelar requested to merge better-url-building into master

What

This change makes urls.NewBuilder faster. urls.NewBuilder is called once per request, and therefore we can expect significant value from this increase in performance looking at the profiling done here: (internal).

How

Previously, we built the routers for the V2 and V1 APIs each time urls.NewBuilder was called. However, these functions take no arguments, and do not read from the runtime environment. Given this, it's safe to store the objects returned from these calls once and reuse them across the life of the application.

Why

The functions that create the router need to compile regexes for each route registered under them. Compiling the regex is relatively slow, and the V2 router in particular has a fair number of routes.

Edited by Hayley Swimelar

Merge request reports