Value Type Optimization

Cangjie language introduces value-type objects. Local variables of a value type can be directly accessed without GC-related barriers during memory read/write without being affected by the change of reference information. Proper use of value-type semantics can effectively improve program running efficiency. semantic

Using value types provides more data placement and access modes. Proper data structure design provides excellent spatial/temporal locality in terms of data access, and brings more advantages in operations such as computing and access. As shown in the following figure, for the array of class A, when the member variables of the array are accessed, the load operation needs to be performed twice. For the array of the value type, the load operation needs to be performed only once when the member variables of struct A are accessed. structure

The On Stack Replacement (OSR) optimization technique is applicable for value types. When the OSR is used properly, some value-type data can be directly distributed to registers, which brings more advantages to data access and calculation. In the following example, an SA object of the value type is split into a and b, both of which can be represented in a register without repeated load. Subsequently, a and b can be directly represented by constants through constant propagation.

struct A {
  var a : UInt64 = 10
  var b : UInt64 = 100
}

main() {
  var SA = A()
  var sum: UInt64 = 0
  for (i in 0..100) {
    sum += SA.a
  }
  return sum + SA.b
}

After OSR optimization, the preceding source code can be converted into the following form to reduce the operations of access to struct member variables.

main() {
  var a: UInt64 = 10
  var b: UInt64 = 100
  var sum: UInt64 = 0
  for (i in 0..100) {
    sum += a
  }
  return sum + b
}

Compared with a reference type, a value type is allocated more quickly and reclaimed efficiently and automatically with the rollback of the stack space.

If a reference type is used, deep copy occurs, which introduces extra memory access overhead. If a value type is used, indirect addressing and deep copy are avoided. This advantage is more obvious in the scenarios where basic types, such as numbers and Boolean values, are processed.