schema compile OPTIMIZE separate tasks performed only once

... which do not need to be performed for every
recompilation.
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 5b2807d..c030634 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -1554,7 +1554,7 @@
         }
 
         /* recompile */
-        ret = lys_compile(mod, 0, &unres);
+        ret = lys_compile(mod, 0, 1, &unres);
         if (ret) {
             if (!log) {
                 LOGERR(mod->ctx, ret, "Recompilation of module \"%s\" failed.", mod->name);
@@ -1610,7 +1610,7 @@
 }
 
 LY_ERR
-lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres)
+lys_compile(struct lys_module *mod, uint32_t options, ly_bool recompile, struct lys_glob_unres *unres)
 {
     struct lysc_ctx ctx = {0};
     struct lysc_module *mod_c = NULL;
@@ -1625,12 +1625,7 @@
 
     /* if some previous module was not fully compiled, it is forbidden to compile another one (even though it
      * may be okay in some cases) */
-    assert(!unres->recompile);
-
-    if (!mod->implemented) {
-        /* just imported modules are not compiled */
-        return LY_SUCCESS;
-    }
+    assert(mod->implemented && !unres->recompile);
 
     sp = mod->parsed;
 
@@ -1642,20 +1637,26 @@
     ctx.path[0] = '/';
     ctx.unres = unres;
 
-    /* augments and deviations */
-    LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), cleanup);
+    /* skip compilation tasks that need to be performed only once for implemented modules */
+    if (!recompile) {
+        /* identities */
+        LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), cleanup);
 
-    if (!unres->recompile && !unres->full_compilation) {
-        /* check whether this module may reference any already-compiled modules */
-        if (lys_has_compiled_import_r(mod)) {
-            /* it may and we need even disabled nodes in those modules, recompile them */
-            unres->recompile = 1;
+        /* augments and deviations */
+        LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), cleanup);
+
+        if (!unres->recompile && !unres->full_compilation) {
+            /* check whether this module may reference any already-compiled modules */
+            if (lys_has_compiled_import_r(mod)) {
+                /* it may and we need even disabled nodes in those modules, recompile them */
+                unres->recompile = 1;
+            }
         }
-    }
 
-    if (unres->recompile) {
-        /* we need the context recompiled */
-        goto cleanup;
+        if (unres->recompile) {
+            /* we need the context recompiled */
+            goto cleanup;
+        }
     }
 
     /* now the actual compilation will take place */
@@ -1664,9 +1665,6 @@
     LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
     mod_c->mod = mod;
 
-    /* identities */
-    LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), cleanup);
-
     /* compile augments and deviations of our module from other modules so they can be applied during compilation */
     LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), cleanup);
     LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), cleanup);
diff --git a/src/schema_compile.h b/src/schema_compile.h
index bd19aed..3206b39 100644
--- a/src/schema_compile.h
+++ b/src/schema_compile.h
@@ -281,10 +281,11 @@
  * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
  * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
  * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
+ * @param[in] recompile Whether @p mod is compiled for the first time or only recompiled.
  * @param[in,out] unres Global unres structure for newly implemented modules. If `recompile` was set, @p mod
  * was actually not compiled because there is at least one compiled imported module that must be recompiled first.
  * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
  */
-LY_ERR lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres);
+LY_ERR lys_compile(struct lys_module *mod, uint32_t options, ly_bool recompile, struct lys_glob_unres *unres);
 
 #endif /* LY_SCHEMA_COMPILE_H_ */
diff --git a/src/schema_compile_amend.c b/src/schema_compile_amend.c
index 95d23a5..9fff787 100644
--- a/src/schema_compile_amend.c
+++ b/src/schema_compile_amend.c
@@ -2058,6 +2058,7 @@
 lys_precompile_own_deviations(struct lysc_ctx *ctx)
 {
     LY_ARRAY_COUNT_TYPE u, v, w;
+    struct lys_module *orig_cur_mod;
     const struct lys_module *dev_mod;
     struct lysc_deviation *dev;
     struct lysp_deviate *d;
@@ -2098,8 +2099,15 @@
             }
         }
         if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
+            orig_cur_mod = ctx->cur_mod;
+            ctx->cur_mod = dev->dev_pmods[u]->mod;
+            lysc_update_path(ctx, NULL, "{deviation}");
+            lysc_update_path(ctx, NULL, dev->nodeid->expr);
             LOGVAL(ctx->ctx, LYVE_SEMANTICS,
                     "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
+            lysc_update_path(ctx, NULL, NULL);
+            lysc_update_path(ctx, NULL, NULL);
+            ctx->cur_mod = orig_cur_mod;
             return LY_EVALID;
         }
 
diff --git a/src/tree_schema.c b/src/tree_schema.c
index ac876d1..93f2979 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -832,7 +832,7 @@
     mod->implemented = 1;
 
     /* compile the schema */
-    LY_CHECK_RET(lys_compile(mod, 0, unres));
+    LY_CHECK_RET(lys_compile(mod, 0, 0, unres));
 
     /* new module is implemented and compiled */
     unres->full_compilation = 0;