aaronrenner / phx_gen_auth

An authentication system generator for Phoenix 1.5 applications.
774 stars 56 forks source link

Slow recompilation in development #75

Closed liamwhite closed 4 years ago

liamwhite commented 4 years ago

Importing the UserAuth module into the router in development causes very minor changes to the application to end up recompiling the router. This is very slow.

Performance after touch lib/philomena/users.ex:

root@28c98c26a5aa:/srv/philomena# time mix compile
Compiling 2 files (.ex)
Generated philomena app

real    0m6.173s
user    0m6.524s
sys 0m0.888s
root@28c98c26a5aa:/srv/philomena# time mix compile
Compiling 2 files (.ex)

real    0m6.024s
user    0m6.408s
sys 0m0.706s

A small set of changes causes development recompilation performance to return to normal:

--- a/lib/philomena_web/router.ex
+++ b/lib/philomena_web/router.ex
@@ -1,15 +1,13 @@
 defmodule PhilomenaWeb.Router do
   use PhilomenaWeb, :router

-  import PhilomenaWeb.UserAuth
-
   pipeline :browser do
     plug :accepts, ["html"]
     plug :fetch_session
     plug :fetch_flash
     plug :protect_from_forgery
     plug :put_secure_browser_headers
-    plug :fetch_current_user
+    plug PhilomenaWeb.FetchUserPlug
     plug PhilomenaWeb.ContentSecurityPolicyPlug
     plug PhilomenaWeb.CurrentFilterPlug
     plug PhilomenaWeb.ImageFilterPlug
@@ -53,6 +51,14 @@ defmodule PhilomenaWeb.Router do
     plug PhilomenaWeb.FilterBannedUsersPlug
   end

+  pipeline :require_authenticated_user do
+    plug PhilomenaWeb.RequireUserPlug
+  end
+
+  pipeline :redirect_if_user_is_authenticated do
+    plug PhilomenaWeb.RequireNoUserPlug
+  end
+
   scope "/", PhilomenaWeb do
     pipe_through [:browser, :redirect_if_user_is_authenticated]

The added plugs simply call the relevant function in the UserAuth module.

Performance after touch lib/philomena/users.ex:

root@28c98c26a5aa:/srv/philomena# time mix compile
Compiling 1 file (.ex)
Generated philomena app

real    0m1.584s
user    0m1.687s
sys 0m0.599s
root@28c98c26a5aa:/srv/philomena# time mix compile
Compiling 1 file (.ex)

real    0m1.560s
user    0m1.588s
sys 0m0.595s

I've already confirmed that the problem only occurs because the router is recompiled, and recompiling the router alone is quite slow:

root@28c98c26a5aa:/srv/philomena# time mix compile
Compiling 1 file (.ex)

real    0m5.698s
user    0m5.929s
sys 0m0.802s
josevalim commented 4 years ago

This is much improved in Elixir v1.11, which no longer treats imports as compile-time dependencies. Since v1.11 will be out soon, I propose to keep things as is, thanks! :)

liamwhite commented 4 years ago

Okay, good to know!

liamwhite commented 3 years ago

Confirming that this is fixed in 1.11.