Skip to content

Commit

Permalink
fastutil
Browse files Browse the repository at this point in the history
  • Loading branch information
ahirreddy committed Jan 3, 2024
1 parent ddce88c commit ca3a977
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lazy val main = (project in file("sjsonnet"))
"org.scala-lang.modules" %% "scala-collection-compat" % "2.4.0",
"org.tukaani" % "xz" % "1.8",
"org.yaml" % "snakeyaml" % "1.30",
"it.unimi.dsi" % "fastutil" % "8.5.12",
),
libraryDependencies ++= Seq(
"com.lihaoyi" %% "utest" % "0.7.7",
Expand Down
3 changes: 3 additions & 0 deletions sjsonnet/src-jvm/sjsonnet/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.tukaani.xz.LZMA2Options
import org.tukaani.xz.XZOutputStream
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import it.unimi.dsi.fastutil.objects.{Object2BooleanLinkedOpenHashMap, Object2ObjectLinkedOpenHashMap}

object Platform {
def gzipBytes(b: Array[Byte]): String = {
Expand Down Expand Up @@ -64,6 +65,7 @@ object Platform {
* All returned maps preserve the insertion order of the original map. No map returned from this
* method should be mutated.
*/
/*
def compactHashMap[K, V](map: LinkedHashMap[K, V]): JMap[K, V] = {
val size = map.size()
if (size == 0) {
Expand Down Expand Up @@ -91,4 +93,5 @@ object Platform {
newMap
}
}
*/
}
10 changes: 6 additions & 4 deletions sjsonnet/src/sjsonnet/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ class Evaluator(resolver: CachedResolver,
newScope
}

val builder = new java.util.LinkedHashMap[String, Val.Obj.Member]
val builder = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap[String, Val.Obj.Member]
fields.foreach {
case Member.Field(offset, fieldName, plus, null, sep, rhs) =>
val k = visitFieldName(fieldName, offset)
Expand All @@ -598,7 +598,8 @@ class Evaluator(resolver: CachedResolver,
builder.put(k, v)
}
}
cachedObj = new Val.Obj(objPos, Platform.compactHashMap(builder), false, if(asserts != null) assertions else null, sup)
builder.trim()
cachedObj = new Val.Obj(objPos, builder, false, if(asserts != null) assertions else null, sup)
cachedObj
}

Expand All @@ -607,7 +608,7 @@ class Evaluator(resolver: CachedResolver,
val compScope: ValScope = scope //.clearSuper

lazy val newSelf: Val.Obj = {
val builder = new java.util.LinkedHashMap[String, Val.Obj.Member]
val builder = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap[String, Val.Obj.Member]
for(s <- visitComp(e.first :: e.rest, Array(compScope))){
lazy val newScope: ValScope = s.extend(newBindings, newSelf, null)

Expand All @@ -628,7 +629,8 @@ class Evaluator(resolver: CachedResolver,
case Val.Null(_) => // do nothing
}
}
new Val.Obj(e.pos, Platform.compactHashMap(builder), false, null, sup)
builder.trim()
new Val.Obj(e.pos, builder, false, null, sup)
}

newSelf
Expand Down
5 changes: 3 additions & 2 deletions sjsonnet/src/sjsonnet/Materializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ abstract class Materializer {
case ujson.Str(s) => Val.Str(pos, s)
case ujson.Arr(xs) => new Val.Arr(pos, xs.map(x => (() => reverse(pos, x)): Lazy).toArray[Lazy])
case ujson.Obj(xs) =>
val builder = new java.util.LinkedHashMap[String, Val.Obj.Member]
val builder = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap[String, Val.Obj.Member]
for(x <- xs) {
val v = new Val.Obj.Member(false, Visibility.Normal) {
def invoke(self: Val.Obj, sup: Val.Obj, fs: FileScope, ev: EvalScope): Val = reverse(pos, x._2)
}
builder.put(x._1, v)
}
new Val.Obj(pos, Platform.compactHashMap(builder), false, null, null)
builder.trim()
new Val.Obj(pos, builder, false, null, null)
}

def toExpr(v: ujson.Value)(implicit ev: EvalScope): Expr = v match{
Expand Down
5 changes: 3 additions & 2 deletions sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class Std {
val func = _func.asFunc
val obj = _obj.asObj
val allKeys = obj.allKeyNames
val m = new util.LinkedHashMap[String, Val.Obj.Member]()
val m = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap[String, Val.Obj.Member]
var i = 0
while(i < allKeys.length) {
val k = allKeys(i)
Expand All @@ -303,7 +303,8 @@ class Std {
m.put(k, v)
i += 1
}
new Val.Obj(pos, Platform.compactHashMap(m), false, null, null)
m.trim()
new Val.Obj(pos, m, false, null, null)
}
}

Expand Down
25 changes: 14 additions & 11 deletions sjsonnet/src/sjsonnet/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ object Val{
}

def mk(pos: Position, members: (String, Obj.Member)*): Obj = {
val m = new util.LinkedHashMap[String, Obj.Member]()
val m = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap[String, Val.Obj.Member]
for((k, v) <- members) m.put(k, v)
new Obj(pos, Platform.compactHashMap(m), false, null, null)
m.trim()
new Obj(pos, m, false, null, null)
}
}

Expand All @@ -144,19 +145,20 @@ object Val{
triggerAsserts: Val.Obj => Unit,
`super`: Obj,
valueCache: mutable.HashMap[Any, Val] = mutable.HashMap.empty[Any, Val],
private[this] var allKeys: util.Map[String, java.lang.Boolean] = null) extends Literal with Expr.ObjBody {
private[this] var allKeys: it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap[String] = null) extends Literal with Expr.ObjBody {
var asserting: Boolean = false

def getSuper = `super`

private[this] def getValue0: util.Map[String, Obj.Member] = {
if(value0 == null) {
val value0 = new java.util.LinkedHashMap[String, Val.Obj.Member]
val value0 = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap[String, Val.Obj.Member]
allKeys.forEach { (k, _) =>
value0.put(k, new Val.Obj.ConstMember(false, Visibility.Normal, valueCache(k)))
}
// Only assign to field after initialization is complete to allow unsynchronized multi-threaded use:
this.value0 = Platform.compactHashMap(value0)
value0.trim()
this.value0 = value0
}
value0
}
Expand All @@ -176,7 +178,7 @@ object Val{
def prettyName = "object"
override def asObj: Val.Obj = this

private def gatherKeys(mapping: util.LinkedHashMap[String, java.lang.Boolean]): Unit = {
private def gatherKeys(mapping: it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap[String]): Unit = {
if(static) mapping.putAll(allKeys)
else {
if(`super` != null) `super`.gatherKeys(mapping)
Expand All @@ -191,9 +193,9 @@ object Val{

private def getAllKeys = {
if(allKeys == null) {
val tmpKeys = new util.LinkedHashMap[String, java.lang.Boolean]
gatherKeys(tmpKeys)
allKeys = Platform.compactHashMap(tmpKeys)
allKeys = new it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap[String]
gatherKeys(allKeys)
allKeys.trim()
}
allKeys
}
Expand Down Expand Up @@ -300,13 +302,14 @@ object Val{

def staticObject(pos: Position, fields: Array[Expr.Member.Field]): Obj = {
val cache = mutable.HashMap.empty[Any, Val]
val allKeys = new util.LinkedHashMap[String, java.lang.Boolean]
val allKeys = new it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap[String]
fields.foreach {
case Expr.Member.Field(_, Expr.FieldName.Fixed(k), _, _, _, rhs: Val.Literal) =>
cache.put(k, rhs)
allKeys.put(k, false)
}
new Val.Obj(pos, null, true, null, null, cache, Platform.compactHashMap(allKeys))
allKeys.trim()
new Val.Obj(pos, null, true, null, null, cache, allKeys)
}

abstract class Func(val pos: Position,
Expand Down
5 changes: 3 additions & 2 deletions sjsonnet/src/sjsonnet/ValVisitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ValVisitor(pos: Position) extends JsVisitor[Val, Val] { self =>

def visitObject(length: Int, index: Int): ObjVisitor[Val, Val] = new ObjVisitor[Val, Val] {
val cache = mutable.HashMap.empty[Any, Val]
val allKeys = new util.LinkedHashMap[String, java.lang.Boolean]
val allKeys = new it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap[String]
var key: String = null
def subVisitor: Visitor[_, _] = self
def visitKey(index: Int) = upickle.core.StringVisitor
Expand All @@ -28,7 +28,8 @@ class ValVisitor(pos: Position) extends JsVisitor[Val, Val] { self =>
allKeys.put(key, false)
}
def visitEnd(index: Int): Val = {
new Val.Obj(pos, null, true, null, null, cache, Platform.compactHashMap(allKeys))
allKeys.trim()
new Val.Obj(pos, null, true, null, null, cache, allKeys)
}
}

Expand Down

0 comments on commit ca3a977

Please sign in to comment.