labstack / echo

High performance, minimalist Go web framework
https://echo.labstack.com
MIT License
29.94k stars 2.23k forks source link

CORS: the middleware needlessly compiles the same regexps during each of its invocations #2708

Closed jub0bs closed 3 hours ago

jub0bs commented 1 day ago

Issue Description

The CORS middleware currently recompiles the same regexps over and over during each of its invocations. However, for performance, compiling regexps on the hot path should be avoided when possible.

In this case, the regexps could be compiled once and for all during middleware initialisation. The following line

allowOriginPatterns := []string{}

could advantageously be replaced by

var allowOriginRegexp []*regexp.Regexp

and the rest of the necessary changes would flow from this simple change.

Checklist

Expected behaviour

No regexp compilation on the hot path.

Actual behaviour

Regexp compilation on the hot path.

Version/commit

v4.12.0

aldas commented 3 hours ago

Thanks for noticing. https://github.com/labstack/echo/pull/2709 reworks that part