From 37859a74f41cf8f9f791942cd2e96f84dc892a1f Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Tue, 21 Mar 2023 18:43:03 +0100
Subject: [PATCH] Pass arguments by const pointer instead of value

---
 src/stream_cgribex.c | 22 +++++++++++-----------
 src/stream_gribapi.c |  9 ++++-----
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/src/stream_cgribex.c b/src/stream_cgribex.c
index 0d0dde101..94d9ae9d1 100644
--- a/src/stream_cgribex.c
+++ b/src/stream_cgribex.c
@@ -734,13 +734,13 @@ cgribexVarSet(int param, int level1, int level2, int leveltype, int trange)
 }
 
 static inline int
-cgribexVarCompare(compvar_t compVar, record_t record, int flag)
+cgribexVarCompare(const compvar_t *compVar, const record_t *record, int flag)
 {
-  bool vinst = compVar.tsteptype == TSTEP_INSTANT || compVar.tsteptype == TSTEP_INSTANT2 || compVar.tsteptype == TSTEP_INSTANT3;
-  bool rinst = record.tsteptype == TSTEP_INSTANT || record.tsteptype == TSTEP_INSTANT2 || record.tsteptype == TSTEP_INSTANT3;
-  int tstepDiff = (!((flag == 0) & (vinst && rinst))) & (compVar.tsteptype != record.tsteptype);
-  int rstatus = (compVar.param != record.param) | (compVar.level1 != record.ilevel) | (compVar.level2 != record.ilevel2)
-                | (compVar.ltype != record.ltype) | tstepDiff;
+  bool vinst = compVar->tsteptype == TSTEP_INSTANT || compVar->tsteptype == TSTEP_INSTANT2 || compVar->tsteptype == TSTEP_INSTANT3;
+  bool rinst = record->tsteptype == TSTEP_INSTANT || record->tsteptype == TSTEP_INSTANT2 || record->tsteptype == TSTEP_INSTANT3;
+  int tstepDiff = (!((flag == 0) & (vinst && rinst))) & (compVar->tsteptype != record->tsteptype);
+  int rstatus = (compVar->param != record->param) | (compVar->level1 != record->ilevel) | (compVar->level2 != record->ilevel2)
+                | (compVar->ltype != record->ltype) | tstepDiff;
   return rstatus;
 }
 
@@ -861,7 +861,7 @@ cgribexScanTimestep1(stream_t *streamptr)
           record_t *records = streamptr->tsteps[tsID].records;
           for (recID = 0; recID < nrecs; recID++)
             {
-              if (cgribexVarCompare(compVar, records[recID], 0) == 0) break;
+              if (cgribexVarCompare(&compVar, &records[recID], 0) == 0) break;
             }
 
           if (CDI_Inventory_Mode == 1)
@@ -1035,7 +1035,7 @@ cgribexScanTimestep2(stream_t *streamptr)
 
       for (recID = 0; recID < nrecords; recID++)
         {
-          if (cgribexVarCompare(compVar, records[recID], 0) == 0) break;
+          if (cgribexVarCompare(&compVar, &records[recID], 0) == 0) break;
         }
 
       if (recID == nrecords)
@@ -1076,7 +1076,7 @@ cgribexScanTimestep2(stream_t *streamptr)
         Message("Read record %2d (id=%s lev1=%d lev2=%d) %s", nrecsScanned, paramstr, level1, level2,
                 CdiDateTime_string(vDateTime));
 
-      if (cgribexVarCompare(compVar, streamptr->tsteps[tsID].records[recID], 0) != 0)
+      if (cgribexVarCompare(&compVar, &streamptr->tsteps[tsID].records[recID], 0) != 0)
         {
           Message("tsID = %d recID = %d param = %3d new %3d  level = %3d new %3d", tsID, recID, records[recID].param, param,
                   records[recID].ilevel, level1);
@@ -1228,7 +1228,7 @@ cgribexScanTimestep(stream_t *streamptr)
           for (vrecID = 0; vrecID < nrecs; vrecID++)
             {
               recID = streamptr->tsteps[1].recIDs[vrecID];
-              if (cgribexVarCompare(compVar, records[recID], 0) == 0) break;
+              if (cgribexVarCompare(&compVar, &records[recID], 0) == 0) break;
             }
 
           if (vrecID == nrecs)
@@ -1271,7 +1271,7 @@ cgribexScanTimestep(stream_t *streamptr)
             Message("Read record %2d (id=%s lev1=%d lev2=%d) %s", nrecsScanned, paramstr, level1, level2,
                     CdiDateTime_string(vDateTime));
 
-          if (cgribexVarCompare(compVar, records[recID], 0) != 0)
+          if (cgribexVarCompare(&compVar, &records[recID], 0) != 0)
             {
               Message("tsID = %d recID = %d param = %3d new %3d  level = %3d new %3d", tsID, recID, records[recID].param, param,
                       records[recID].ilevel, level1);
diff --git a/src/stream_gribapi.c b/src/stream_gribapi.c
index 1dca317b5..c2701485e 100644
--- a/src/stream_gribapi.c
+++ b/src/stream_gribapi.c
@@ -952,13 +952,13 @@ typedef enum
 } checkTimeResult;
 
 static checkTimeResult
-checkTime(stream_t *streamptr, compvar2_t compVar, CdiDateTime verificationTime, CdiDateTime expectedVTime)
+checkTime(stream_t *streamptr, const compvar2_t *compVar, CdiDateTime verificationTime, CdiDateTime expectedVTime)
 {
   // First determine whether the current record exists already.
   int recID = 0;
   for (; recID < streamptr->nrecs; recID++)
     {
-      if (gribapiVarCompare(&compVar, &streamptr->tsteps[0].records[recID], 1) == 0) break;
+      if (gribapiVarCompare(compVar, &streamptr->tsteps[0].records[recID], 1) == 0) break;
     }
   bool recordExists = (recID < streamptr->nrecs);
 
@@ -1251,9 +1251,8 @@ gribapiScanTimestep1(stream_t *streamptr)
 
           int tsteptype = gribapiGetTsteptype(gh);
           size_t gridsize = gribapiGetGridsize(gh);
-          checkTimeResult result = checkTime(
-              streamptr, gribapiVarSet(param, level1, level2, leveltype1, tsteptype, gridsize, varname, scanKeys, tiles), vDateTime,
-              vDateTime0);
+          compvar2_t compVar = gribapiVarSet(param, level1, level2, leveltype1, tsteptype, gridsize, varname, scanKeys, tiles);
+          checkTimeResult result = checkTime(streamptr, &compVar, vDateTime, vDateTime0);
           if (result == CHECKTIME_STOP)
             {
               nrecsScanned--;
-- 
GitLab