epfl-lara / stainless

Verification framework and tool for higher-order Scala programs
https://epfl-lara.github.io/stainless/
Apache License 2.0
349 stars 51 forks source link

Cache refactor to improve performance and footprint #1576

Open samuelchassot opened 2 weeks ago

samuelchassot commented 2 weeks ago

Here are some ideas to improve the Stainless cache implementation to improve its performance and reduce its footprint:

vkuncak commented 2 weeks ago

Looks like the first thing to do is storing hashes. Copying and testing the post from stackoverflow, this works. We should perhaps keep bytes; strings are only for human inspection.

import java.security.MessageDigest
import java.util.HexFormat
object Test:
  val bytes = MessageDigest.getInstance("SHA-256")
    .digest("any string".getBytes("UTF-8"))

  @main
  def sha256: Unit =  
    val s = HexFormat.of().formatHex(bytes)
    println(s)
    // 1e57a452a094728c291bc42bf2bc7eb8d9fd8844d1369da2bf728588b46c4e75
    // same as output of:
    //    echo -n "any string" | openssl dgst -sha256

Someone could also write a dedicated hash table for storing fixed-length caches in a bytearray or long array. It should be notably faster than Hashmap, as we only need one bytearray to store hashes, and use the ending of the hash multiplied by some costant (by 32 if we have byte array) to index into this array.