# Atomic (read-only) snapshot and clear for TrieMap?

**URL:** <https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072>\
**Category:** Scala Standard Library\
**Created:** [January 22, 2023, 3:35am UTC](https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072 "2023-01-22T03:35:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aschweig](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aschweig/32/2655_2.png) [@aschweig](https://contributors.scala-lang.org/u/aschweig)\
**Post date:** [January 22, 2023, 3:35am UTC](https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072/1 "2023-01-22T03:35:45Z")

</div>

I’d like to recycle a discussion [regarding a feature request for TrieMap from scala-users](https://users.scala-lang.org/t/atomic-snapshot-and-clear-for-triemap/9047). The goal is to provide an atomic shapshot-and-clear method to the existing TrieMap interface.

I proposed an implementation drawing from `clear` and `readOnlySnapshot`. It makes use of the [private method RDCSS\_ROOT](https://github.com/scala/scala/blob/2c8b2a4c52403c52236d3f97b0a4c4f97f552fd3/src/library/scala/collection/concurrent/TrieMap.scala#L789) – hence it is difficult to see how to implement short of duplicating TrieMap.scala:

```auto
@tailrec def snapshotAndClear(): scala.collection.Map[K, V] = {
    val r = RDCSS_READ_ROOT()
    val expmain = r.gcasRead(this)
    if (RDCSS_ROOT(r, expmain, INode.newRootNode[K, V](equality) )) new TrieMap(r, null, hashing, equality)
    else snapshotAndClear()
  }

```

The name `readOnlySnapshotAndClear` is perhaps more in-line with the existing interface’s naming – my feeling was that this was too long.

Would this make sense as a contribution? If so, would scala-library-next be the appropriate next step?

---

<div class="post-metadata">

**Author:** ![lrytz](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lrytz/32/1653_2.png) [@lrytz](https://contributors.scala-lang.org/u/lrytz)\
**Post date:** [January 23, 2023, 9:42am UTC](https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072/2 "2023-01-23T09:42:12Z")

</div>

Hi @aschweig. Unfortunately the Scala standard library is currently not open for new additions. There is however a proposal currently being discussed to change that: [https://github.com/scala/improvement-proposals/pull/54](https://github.com/scala/improvement-proposals/pull/54).

`scala-library-next` is an option in principle, however that library hasn’t seen much support so far, there are no releases. Also, the implementation you propose uses the private `RDCSS_ROOT` which cannot be accessed externally.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [January 23, 2023, 1:31pm UTC](https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072/3 "2023-01-23T13:31:52Z")

</div>

> [@lrytz](#):
>
> `scala-library-next` is an option in principle, however that library hasn’t seen much support so far, there are no releases.

I don’t think it was ever intended to have releases? AFAIK, `scala-library-next` is intended to be the holding bin for exactly this sort of thing – additions that are potentially desireable to add to stdlib once that reopens to new content.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [January 23, 2023, 1:59pm UTC](https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072/4 "2023-01-23T13:59:26Z")

</div>

In that case, wouldn’t it be better to just start a `2.14.x` or `maybe-future` or `fingers-crossed` branch in the scala/scala repo?

---

<div class="post-metadata">

**Author:** ![SethTisue](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sethtisue/32/3185_2.png) [@SethTisue](https://contributors.scala-lang.org/u/SethTisue)\
**Post date:** [January 23, 2023, 7:08pm UTC](https://contributors.scala-lang.org/t/atomic-read-only-snapshot-and-clear-for-triemap/6072/5 "2023-01-23T19:08:40Z")

</div>

scala-library-next was intended to have releases, so that people could easily try the additions out, or even depend on scala-library-next in their projects.
