Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d441794
update API of addMutableState
kiszk Nov 24, 2017
870d106
eliminate initialization with default value
kiszk Nov 25, 2017
24d7087
allocate a global Java array to store a lot of mutable state in a class
kiszk Nov 26, 2017
3eb5842
fix scala style error
kiszk Nov 26, 2017
074d711
fix test failure of ExpressionEncoderSuite.NestedArray
kiszk Nov 27, 2017
eafa3f8
rebase with master
kiszk Nov 30, 2017
90d15f3
address review comments
kiszk Nov 30, 2017
c456c07
add useFreshname parameter to addMutableState
kiszk Nov 30, 2017
9ca5ab3
rebase with master
kiszk Nov 30, 2017
5b36c61
fix test failures
kiszk Dec 1, 2017
fd51d75
drop to creat a loop for initialization
kiszk Dec 7, 2017
effe918
fix test failure
kiszk Dec 8, 2017
9df109c
update comments
kiszk Dec 8, 2017
634d494
address review comment
kiszk Dec 10, 2017
d3438fd
address review comment
kiszk Dec 12, 2017
f4f3754
address review comment
kiszk Dec 12, 2017
f1e1fca
address review comments except test case
kiszk Dec 13, 2017
0937ef2
rebase with master
kiszk Dec 13, 2017
4bfcc1a
Do not use compaction as possible for frequently-accessed variable
kiszk Dec 13, 2017
49119a9
exclude mutable state from argument list for ExpressionCodegn
kiszk Dec 13, 2017
24f49c5
fix test failures
kiszk Dec 14, 2017
15e967e
address review comments
kiszk Dec 14, 2017
d6c1a97
address review comments
kiszk Dec 14, 2017
a9d40e9
address review comments
kiszk Dec 14, 2017
31914c0
address review comments
kiszk Dec 15, 2017
0e45c19
address review comments
kiszk Dec 19, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address review comment
  • Loading branch information
kiszk committed Dec 14, 2017
commit f4f37549c719e3e8dcbd76108bbfac3d77e8107d
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,28 @@ class CodegenContext {
mutableStates += ((javaType, varName, initCode))
varName
} else {
val arrayName = mutableStateArrayCurrentNames.getOrElse(javaType, "")
val prevIdx = mutableStateArrayIdx.getOrElse((javaType, arrayName), -1)
if (0 <= prevIdx && prevIdx < CodeGenerator.MUTABLESTATEARRAY_SIZE_LIMIT - 1) {
// a mutableStateArray for the given type and name has already been declared,
// update the max index of the array and return an array element
val idx = prevIdx + 1
mutableStateArrayIdx.update((javaType, arrayName), idx)
val initCode = codeFunctions(s"$arrayName[$idx]")
mutableStateArrayInitCodes += initCode
s"$arrayName[$idx]"
} else {
// mutableStateArray has not been declared yet for the given type and name.
// Create a new name for the array, and add an entry to keep track of current array name
// for type. In addition, init code is stored for code generation
val newArrayName = freshName("mutableStateArray")
mutableStateArrayCurrentNames += javaType -> newArrayName
val idx = 0
mutableStateArrayIdx += (javaType, newArrayName) -> idx
val initCode = codeFunctions(s"$newArrayName[$idx]")
mutableStateArrayInitCodes += initCode
s"$newArrayName[$idx]"
// mutableStateArray has not been declared yet for the given type and name. Create a new name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If ...

// for the array, The mutableStateArray for the given type and name has been declared,
// update the max index of the array. Then, add an entry to keep track of current array name
// for type and nit code is stored for code generation. Finally, return an array element
val (arrayName, newIdx) = {
val compactArrayName = "mutableStateArray"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

class MutableStateArrays {
  val arrayNames = mutable.ListBuffer.empty[String]
  createNewArray()
  private[this] var currentIndex = 0

  def getNextSlot(): String = {
    if (currentIndex < CodeGenerator.MUTABLESTATEARRAY_SIZE_LIMIT) {
      val res = s"${arrayNames.last}[$currentIndex]"
      currentIndex += 1
      res
    } else {
      createNewArray()
      currentIndex = 1
      s"${arrayNames.last}[0]"
    }
  }

  private def createNewArray() = arrayNames.append(freshName("mutableStateArray"))
}

val mutableStateArrayMap: Map[String, MutableStateArrays] = ... // type name -> MutableStateArrays

and here the logic can be very simple

val arrays = mutableStateArrayMap.getOrElseUpdate(javaType, new MutableStateArrays)
arrays.getNextSlot()

var name = mutableStateArrayCurrentNames.getOrElse(javaType, freshName(compactArrayName))
var idx = mutableStateArrayIdx.getOrElse((javaType, name), -1)
if (idx >= CodeGenerator.MUTABLESTATEARRAY_SIZE_LIMIT - 1) {
// Create a new array name to avoid array index whose number is larger than 32767 that
// requires a constant pool entry
name = freshName(compactArrayName)
idx = -1
}
(name, idx + 1)
}
mutableStateArrayCurrentNames(javaType) = arrayName
mutableStateArrayIdx((javaType, arrayName)) = newIdx

val initCode = codeFunctions(s"$arrayName[$newIdx]")
mutableStateArrayInitCodes += initCode
s"$arrayName[$newIdx]"
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ class CodegenContext {
def initMutableStates(): String = {
// It's possible that we add same mutable state twice, e.g. the `mergeExpressions` in
// `TypedAggregateExpression`, we should call `distinct` here to remove the duplicated ones.
val initCodes = mutableStates.distinct.map(_._3 + "\n")
val initCodes = mutableStates.map(_._3).distinct.map(_ + "\n")
// statements for array element initialization
val arrayInitCodes = mutableStateArrayInitCodes.distinct.map(_ + "\n")

Expand Down Expand Up @@ -1257,9 +1257,6 @@ object CodeGenerator extends Logging {
// bytecode instruction
val MUTABLESTATEARRAY_SIZE_LIMIT = 32768

// This is an index variable name used in a loop for initializing global variables
val INIT_LOOP_VARIABLE_NAME = "i"

/**
* Compile the Java source code into a Java class, using Janino.
*
Expand Down