Explorar o código

ncd: NCDInterpreter: use a STATEMENT_LOG macro to check whether we would actually log the message before calling
statement_log(). This improves performance.

ambrop7 %!s(int64=13) %!d(string=hai) anos
pai
achega
0e54c2280b
Modificáronse 1 ficheiros con 21 adicións e 25 borrados
  1. 21 25
      ncd/NCDInterpreter.c

+ 21 - 25
ncd/NCDInterpreter.c

@@ -122,6 +122,8 @@ static btime_t statement_instance_func_interp_getretrytime (void *vinterp);
 static void process_moduleprocess_func_event (struct process *p, int event);
 static void process_moduleprocess_func_event (struct process *p, int event);
 static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
 static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
 
 
+#define STATEMENT_LOG(ps, channel, ...) if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, channel)) statement_log(ps, channel, __VA_ARGS__)
+
 int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_len, struct NCDInterpreter_params params)
 int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_len, struct NCDInterpreter_params params)
 {
 {
     ASSERT(program);
     ASSERT(program);
@@ -711,7 +713,7 @@ void process_work_job_handler_working (struct process *p)
             return;
             return;
         }
         }
         
         
-        statement_log(ps, BLOG_INFO, "killing");
+        STATEMENT_LOG(ps, BLOG_INFO, "killing");
         
         
         // set statement state DYING
         // set statement state DYING
         ps->inst.istate = SSTATE_DYING;
         ps->inst.istate = SSTATE_DYING;
@@ -729,7 +731,7 @@ void process_work_job_handler_working (struct process *p)
         struct statement *ps = &p->statements[p->ap - 1];
         struct statement *ps = &p->statements[p->ap - 1];
         ASSERT(ps->inst.istate == SSTATE_CHILD)
         ASSERT(ps->inst.istate == SSTATE_CHILD)
         
         
-        statement_log(ps, BLOG_INFO, "clean");
+        STATEMENT_LOG(ps, BLOG_INFO, "clean");
         
         
         // report clean
         // report clean
         NCDModuleInst_Clean(&ps->inst);
         NCDModuleInst_Clean(&ps->inst);
@@ -756,7 +758,7 @@ void process_work_job_handler_working (struct process *p)
     ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
     ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
     
     
     if (p->error) {
     if (p->error) {
-        statement_log(ps, BLOG_INFO, "waiting after error");
+        STATEMENT_LOG(ps, BLOG_INFO, "waiting after error");
         
         
         // clear error
         // clear error
         p->error = 0;
         p->error = 0;
@@ -839,7 +841,7 @@ again:
         return;
         return;
     }
     }
     
     
-    statement_log(ps, BLOG_INFO, "killing");
+    STATEMENT_LOG(ps, BLOG_INFO, "killing");
     
     
     // update AP
     // update AP
     if (p->ap > ps->i) {
     if (p->ap > ps->i) {
@@ -848,7 +850,7 @@ again:
     
     
     // optimize for statements which can be destroyed immediately
     // optimize for statements which can be destroyed immediately
     if (NCDModuleInst_TryFree(&ps->inst)) {
     if (NCDModuleInst_TryFree(&ps->inst)) {
-        statement_log(ps, BLOG_INFO, "died");
+        STATEMENT_LOG(ps, BLOG_INFO, "died");
         
         
         // free arguments memory
         // free arguments memory
         NCDValMem_Free(&ps->args_mem);
         NCDValMem_Free(&ps->args_mem);
@@ -899,7 +901,7 @@ void process_advance (struct process *p)
     struct statement *ps = &p->statements[p->ap];
     struct statement *ps = &p->statements[p->ap];
     ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
     ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
     
     
-    statement_log(ps, BLOG_INFO, "initializing");
+    STATEMENT_LOG(ps, BLOG_INFO, "initializing");
     
     
     // need to determine the module and object to use it on (if it's a method)
     // need to determine the module and object to use it on (if it's a method)
     const struct NCDModule *module;
     const struct NCDModule *module;
@@ -917,7 +919,7 @@ void process_advance (struct process *p)
         
         
         if (!module) {
         if (!module) {
             const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
             const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
-            statement_log(ps, BLOG_ERROR, "unknown simple statement: %s", cmdname_str);
+            STATEMENT_LOG(ps, BLOG_ERROR, "unknown simple statement: %s", cmdname_str);
             goto fail0;
             goto fail0;
         }
         }
     } else {
     } else {
@@ -930,7 +932,7 @@ void process_advance (struct process *p)
         // get object type
         // get object type
         NCD_string_id_t object_type = NCDObject_Type(&object);
         NCD_string_id_t object_type = NCDObject_Type(&object);
         if (object_type < 0) {
         if (object_type < 0) {
-            statement_log(ps, BLOG_ERROR, "cannot call method on object with no type");
+            STATEMENT_LOG(ps, BLOG_ERROR, "cannot call method on object with no type");
             goto fail0;
             goto fail0;
         }
         }
         
         
@@ -943,7 +945,7 @@ void process_advance (struct process *p)
         if (!module) {
         if (!module) {
             const char *type_str = NCDStringIndex_Value(&p->interp->string_index, object_type);
             const char *type_str = NCDStringIndex_Value(&p->interp->string_index, object_type);
             const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
             const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
-            statement_log(ps, BLOG_ERROR, "unknown method statement: %s::%s", type_str, cmdname_str);
+            STATEMENT_LOG(ps, BLOG_ERROR, "unknown method statement: %s::%s", type_str, cmdname_str);
             goto fail0;
             goto fail0;
         }
         }
     }
     }
@@ -952,19 +954,19 @@ void process_advance (struct process *p)
     NCDValRef args;
     NCDValRef args;
     NCDValReplaceProg prog;
     NCDValReplaceProg prog;
     if (!NCDInterpProcess_CopyStatementArgs(p->iprocess, ps->i, &ps->args_mem, &args, &prog)) {
     if (!NCDInterpProcess_CopyStatementArgs(p->iprocess, ps->i, &ps->args_mem, &args, &prog)) {
-        statement_log(ps, BLOG_ERROR, "NCDInterpProcess_CopyStatementArgs failed");
+        STATEMENT_LOG(ps, BLOG_ERROR, "NCDInterpProcess_CopyStatementArgs failed");
         goto fail0;
         goto fail0;
     }
     }
     
     
     // replace placeholders with values of variables
     // replace placeholders with values of variables
     if (!NCDValReplaceProg_Execute(prog, &ps->args_mem, replace_placeholders_callback, p)) {
     if (!NCDValReplaceProg_Execute(prog, &ps->args_mem, replace_placeholders_callback, p)) {
-        statement_log(ps, BLOG_ERROR, "failed to replace variables in arguments with values");
+        STATEMENT_LOG(ps, BLOG_ERROR, "failed to replace variables in arguments with values");
         goto fail1;
         goto fail1;
     }
     }
     
     
     // allocate memory
     // allocate memory
     if (!statement_allocate_memory(ps, module->alloc_size)) {
     if (!statement_allocate_memory(ps, module->alloc_size)) {
-        statement_log(ps, BLOG_ERROR, "failed to allocate memory");
+        STATEMENT_LOG(ps, BLOG_ERROR, "failed to allocate memory");
         goto fail1;
         goto fail1;
     }
     }
     
     
@@ -1099,10 +1101,6 @@ void statement_logfunc (struct statement *ps)
 
 
 void statement_log (struct statement *ps, int level, const char *fmt, ...)
 void statement_log (struct statement *ps, int level, const char *fmt, ...)
 {
 {
-    if (!BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
-        return;
-    }
-    
     va_list vl;
     va_list vl;
     va_start(vl, fmt);
     va_start(vl, fmt);
     BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
     BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
@@ -1132,7 +1130,7 @@ int statement_allocate_memory (struct statement *ps, int alloc_size)
         // allocate new memory
         // allocate new memory
         char *new_mem = malloc(alloc_size);
         char *new_mem = malloc(alloc_size);
         if (!new_mem) {
         if (!new_mem) {
-            statement_log(ps, BLOG_ERROR, "malloc failed");
+            STATEMENT_LOG(ps, BLOG_ERROR, "malloc failed");
             return 0;
             return 0;
         }
         }
         
         
@@ -1173,7 +1171,7 @@ void statement_instance_func_event (NCDModuleInst *inst, int event)
         case NCDMODULE_EVENT_UP: {
         case NCDMODULE_EVENT_UP: {
             ASSERT(ps->inst.istate == SSTATE_CHILD)
             ASSERT(ps->inst.istate == SSTATE_CHILD)
             
             
-            statement_log(ps, BLOG_INFO, "up");
+            STATEMENT_LOG(ps, BLOG_INFO, "up");
             
             
             // set state ADULT
             // set state ADULT
             ps->inst.istate = SSTATE_ADULT;
             ps->inst.istate = SSTATE_ADULT;
@@ -1182,7 +1180,7 @@ void statement_instance_func_event (NCDModuleInst *inst, int event)
         case NCDMODULE_EVENT_DOWN: {
         case NCDMODULE_EVENT_DOWN: {
             ASSERT(ps->inst.istate == SSTATE_ADULT)
             ASSERT(ps->inst.istate == SSTATE_ADULT)
             
             
-            statement_log(ps, BLOG_INFO, "down");
+            STATEMENT_LOG(ps, BLOG_INFO, "down");
             
             
             // set state CHILD
             // set state CHILD
             ps->inst.istate = SSTATE_CHILD;
             ps->inst.istate = SSTATE_CHILD;
@@ -1201,10 +1199,8 @@ void statement_instance_func_event (NCDModuleInst *inst, int event)
         case NCDMODULE_EVENT_DOWNUP: {
         case NCDMODULE_EVENT_DOWNUP: {
             ASSERT(ps->inst.istate == SSTATE_ADULT)
             ASSERT(ps->inst.istate == SSTATE_ADULT)
             
             
-            if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
-                statement_log(ps, BLOG_INFO, "down");
-                statement_log(ps, BLOG_INFO, "up");
-            }
+            STATEMENT_LOG(ps, BLOG_INFO, "down");
+            STATEMENT_LOG(ps, BLOG_INFO, "up");
             
             
             // clear error
             // clear error
             if (ps->i < p->ap) {
             if (ps->i < p->ap) {
@@ -1218,7 +1214,7 @@ void statement_instance_func_event (NCDModuleInst *inst, int event)
         } break;
         } break;
         
         
         case NCDMODULE_EVENT_DEAD: {
         case NCDMODULE_EVENT_DEAD: {
-            statement_log(ps, BLOG_INFO, "died");
+            STATEMENT_LOG(ps, BLOG_INFO, "died");
             
             
             // free instance
             // free instance
             NCDModuleInst_Free(&ps->inst);
             NCDModuleInst_Free(&ps->inst);
@@ -1241,7 +1237,7 @@ void statement_instance_func_event (NCDModuleInst *inst, int event)
         } break;
         } break;
         
         
         case NCDMODULE_EVENT_DEADERROR: {
         case NCDMODULE_EVENT_DEADERROR: {
-            statement_log(ps, BLOG_ERROR, "died with error");
+            STATEMENT_LOG(ps, BLOG_ERROR, "died with error");
             
             
             // free instance
             // free instance
             NCDModuleInst_Free(&ps->inst);
             NCDModuleInst_Free(&ps->inst);