Github user DaveBirdsall commented on a diff in the pull request:

    https://github.com/apache/incubator-trafodion/pull/750#discussion_r82643315
  
    --- Diff: core/sql/exp/exp_fixup.cpp ---
    @@ -1740,6 +1741,103 @@ Lng32 
ex_conv_clause::findIndexIntoInstrArray(ConvInstruction ci)
       return -1; // not found
     }
     
    +bool   ex_conv_clause::sv_instrOffsetIndexPopulated = false;
    +short  ex_conv_clause::sv_MaxOpTypeValue = -1;
    +int   *ex_conv_clause::sv_convIndexSparse = 0;
    +
    +/* 
    + * Scans convInstrInfo[] and populates a 
    + * sparsely populated index: sv_convIndexSparse. 
    + * 
    + * For a given op type: op_type, 
    + * if (sv_convIndexSparse[op_type>] != -1) then 
    + * it contains the offset into the first row of 
    + * convInstrInfo[]
    + */
    +void ex_conv_clause::populateInstrOffsetIndex()
    +{
    +  if (sv_instrOffsetIndexPopulated) {
    +    return;
    +  }
    +
    +  typedef struct convInstrIndexStruct {
    +    short type_op1;
    +    short offset;
    +  } convInstrIndexDef;
    +  
    +  short lv_MaxIndex = -1;
    +
    +  // Currently, the number of distinct 'type_op1' values in the 
convInstrInf[] is 32
    +  const int lv_MaxIndexCapacity = 100;
    +  convInstrIndexDef lv_convIndex[lv_MaxIndexCapacity];
    +
    +  Int32 i = 0;
    +  Int32 lv_source_type = -1;
    +  Int32 lv_max_array_size = sizeof(ex_conv_clause::convInstrInfo) / 
sizeof(ex_conv_clause::ConvInstrStruct);
    +  
    +  // collect distinct type_op1 values and their 1st offset in 
convInstrInfo[]
    +  while (i < lv_max_array_size) {
    +    if (ex_conv_clause::convInstrInfo[i].type_op1 != lv_source_type) {
    +      lv_source_type = ex_conv_clause::convInstrInfo[i].type_op1;
    +      if ((lv_source_type > -1) && 
    +     (lv_MaxIndex < (lv_MaxIndexCapacity - 1))) {
    +   lv_convIndex[++lv_MaxIndex].type_op1 = lv_source_type;
    +   lv_convIndex[lv_MaxIndex].offset = i;
    +      }
    +    }
    +    i++;
    +  }
    +
    +  // Find the max op type value in lv_convIndex
    +  int j = 0;
    +  while (j <= lv_MaxIndex) {
    +    if (sv_MaxOpTypeValue < lv_convIndex[j].type_op1) {
    +      sv_MaxOpTypeValue = lv_convIndex[j].type_op1;
    +    }
    +    j++;
    +  }
    +
    +  // just in case some op type has been assigned a pretty large value
    +  // we dont want to allocate a very large sparse value
    +  if (sv_MaxOpTypeValue > 8000) {
    +    sv_MaxOpTypeValue = 8000;
    +  }
    +  
    +  // Allocate a sparsely populated array
    +  sv_convIndexSparse = (int *) calloc(sizeof(int), 
    --- End diff --
    
    I think this logic is idempotent to it probably doesn't matter but it looks 
like two threads could do the calloc, one of them will win in setting 
sv_convIndexSparse, then both of them could interleave in the initialization 
logic below. Could be the fast one sets the sv_instrOffsetIndexPopulated flag 
to true while the slow one is in the Initialize to -1 loop so theoretically we 
could get wrong answers. Might be safer to use a local variable here and only 
set sv_convIndexSparse at the very end just before setting the boolean. Then 
the worst thing that happens is we allocated the array twice but it will never 
be inconsistent.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to