src/Pure/General/cache.scala
author wenzelm
Sat, 24 Jul 2021 15:38:41 +0200
changeset 74056 fb8d5c0133c9
parent 73031 f93f0597f4fb
child 75393 87ebf5a50283
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68396
7433ee1ed7e3 tuned header;
wenzelm
parents: 68266
diff changeset
     1
/*  Title:      Pure/General/cache.scala
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     3
73023
e15621aa8c72 tuned comments;
wenzelm
parents: 71382
diff changeset
     4
Cache for partial sharing (weak table).
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     5
*/
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     6
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     7
package isabelle
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     8
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
     9
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    10
import java.util.{Collections, WeakHashMap, Map => JMap}
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    11
import java.lang.ref.WeakReference
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    12
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    13
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    14
object Cache
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    15
{
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    16
  val default_max_string = 100
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    17
  val default_initial_size = 131071
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    18
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73027
diff changeset
    19
  def make(
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73027
diff changeset
    20
      max_string: Int = default_max_string,
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73027
diff changeset
    21
      initial_size: Int = default_initial_size): Cache =
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    22
    new Cache(max_string, initial_size)
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    23
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    24
  val none: Cache = make(max_string = 0)
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    25
}
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    26
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    27
class Cache(max_string: Int, initial_size: Int)
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    28
{
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    29
  val no_cache: Boolean = max_string == 0
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    30
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    31
  private val table: JMap[Any, WeakReference[Any]] =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    32
    if (max_string == 0) null
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    33
    else  Collections.synchronizedMap(new WeakHashMap[Any, WeakReference[Any]](initial_size))
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    34
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    35
  override def toString: String =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    36
    if (no_cache) "Cache.none" else "Cache(size = " + table.size + ")"
68266
f13bb379c573 tuned output;
wenzelm
parents: 68265
diff changeset
    37
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    38
  protected def lookup[A](x: A): Option[A] =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    39
  {
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    40
    if (table == null) None
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    41
    else {
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    42
      val ref = table.get(x)
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    43
      if (ref == null) None
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    44
      else Option(ref.asInstanceOf[WeakReference[A]].get)
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    45
    }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    46
  }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    47
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    48
  protected def store[A](x: A): A =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    49
  {
73027
000bcf2524fd clarified boundary case;
wenzelm
parents: 73024
diff changeset
    50
    if (table == null || x == null) x
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    51
    else {
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    52
      table.put(x, new WeakReference[Any](x))
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    53
      x
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    54
    }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    55
  }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    56
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    57
  protected def cache_string(x: String): String =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    58
  {
73027
000bcf2524fd clarified boundary case;
wenzelm
parents: 73024
diff changeset
    59
    if (x == null) null
000bcf2524fd clarified boundary case;
wenzelm
parents: 73024
diff changeset
    60
    else if (x == "") ""
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    61
    else if (x == "true") "true"
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    62
    else if (x == "false") "false"
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    63
    else if (x == "0.0") "0.0"
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    64
    else if (Library.is_small_int(x)) Library.signed_string_of_int(Integer.parseInt(x))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    65
    else {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    66
      lookup(x) match {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    67
        case Some(y) => y
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    68
        case None =>
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    69
          val z = Library.isolate_substring(x)
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    70
          if (z.length > max_string) z else store(z)
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    71
      }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    72
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    73
  }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    74
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    75
  // main methods
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    76
  def string(x: String): String =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 73023
diff changeset
    77
    if (no_cache) x else synchronized { cache_string(x) }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents:
diff changeset
    78
}