Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DMS-68] Deduplicate switch cases using or-patterns #29

Open
wants to merge 16 commits into
base: milestone-2
Choose a base branch
from

Conversation

s-and-witch
Copy link
Collaborator

In comparison with milestone-2

Map comparison

binary_size generate max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
persistentmap_100 186_806 201_602 42_600 52_323 122_234 124_817 440_282
persistentmap_baseline_100 187_090 201_602 42_600 51_044 122_234 124_817 440_282
rbtree_100 189_877 225_155 42_540 50_045 135_367 133_686 565_657
persistentmap_1000 186_806 2_724_937 568_248 70_188 160_005 168_031 4_153_206
persistentmap_baseline_1000 187_090 2_724_937 568_248 68_227 160_005 168_031 4_153_206
rbtree_1000 189_877 3_118_490 580_948 67_516 181_375 180_396 5_409_805
persistentmap_10000 186_806 45_412_473 480_360 86_835 195_152 214_853 41_210_098
persistentmap_baseline_10000 187_090 45_412_473 480_360 84_528 195_152 214_853 41_210_098
rbtree_10000 189_877 51_301_049 520_428 83_465 224_135 230_257 53_866_589
persistentmap_100000 186_806 531_616_890 4_800_360 101_880 233_003 258_058 542_245_665
persistentmap_baseline_100000 187_090 531_616_890 4_800_360 98_864 233_003 258_058 542_245_665
rbtree_100000 189_877 606_914_881 5_200_428 98_012 270_465 276_802 698_928_540
persistentmap_1000000 186_806 6_080_971_407 48_000_396 120_951 271_877 307_984 5_422_489_439
persistentmap_baseline_1000000 187_090 6_080_971_407 48_000_396 117_446 271_877 307_984 5_422_489_439
rbtree_1000000 189_877 6_993_676_959 52_000_464 116_417 317_299 330_296 6_988_883_198

Persistent map API

size foldLeft foldRight mapfilter map
persistentmap 100 20_551 20_663 89_869 29_538
persistentmap_baseline 100 19_787 19_699 89_105 29_538
persistentmap 1000 174_957 175_469 1_556_926 263_679
persistentmap_baseline 1000 167_597 166_109 1_549_566 263_679
persistentmap 10000 1_721_547 1_723_275 32_489_874 2_600_540
persistentmap_baseline 10000 1_648_003 1_629_731 32_416_330 2_600_540
persistentmap 100000 17_191_989 17_197_589 385_509_744 129_765_701
persistentmap_baseline 100000 16_454_053 16_259_653 384_771_808 129_765_701
persistentmap 1000000 171_934_401 171_950_689 4_443_344_176 1_297_599_225
persistentmap_baseline 1000000 164_559_185 162_575_473 4_435_968_960 1_297_599_225

s-and-witch and others added 16 commits October 1, 2024 00:55
No changes in logic so far, just simple refactoring
Add `MapOps` class with the following signature:

  public class MapOps<K>(compare : (K,K) -> O.Order) {

    public func put<V>(rbMap : Map<K, V>, key : K, value : V) : Map<K, V>

    public func fromIter<V>(i : I.Iter<(K,V)>) : Map<K, V>

    public func replace<V>(rbMap : Map<K, V>, key : K, value : V) : (Map<K,V>, ?V)

    public func mapFilter<V1, V2>(f : (K, V1) -> ?V2, rbMap : Map<K, V1>) : Map<K, V2>

    public func get<V>(key : K, rbMap : Map<K, V>) : ?V

    public func delete<V>(rbMap : Map<K, V>, key : K) : Map<K, V>

    public func remove< V>(rbMap : Map<K, V>, key : K) : (Map<K,V>, ?V)

  };

The other functionality provided as standalone functions, as they
don't require comparator:

  public type Direction = { #fwd; #bwd };

  public func iter<K, V>(rbMap : Map<K, V>, direction : Direction) : I.Iter<(K, V)>

  public func entries<K, V>(m : Map<K, V>) : I.Iter<(K, V)>

  public func keys<K, V>(m : Map<K, V>, direction : Direction) : I.Iter<K>

  public func vals<K, V>(m : Map<K, V>, direction : Direction) : I.Iter<V>

  public func map<K, V1, V2>(f : (K, V1) -> V2, rbMap : Map<K, V1>) : Map<K, V2>

  public func size<K, V>(t : Map<K, V>) : Nat

  public func foldLeft<Key, Value, Accum>(
    combine : (Key, Value, Accum) -> Accum,
    base : Accum,
    rbMap : Map<Key, Value>
  ) : Accum

  And foldRight with the same signature as foldLeft

The following functions are new for the API:
- MapOps.put, MapOps.delete
- MapOps.fromIter, entries, keys, vals
- MapOps.mapFilter, map
- foldLeft, foldRight
Problem: now order is not consistent within new module and with old
modules as well.

Solution: make the map argument always go first
In addition to tests this patch removes `direction`
argument from `keys` and `values` function to keep
them simple and provides a new function `Map.empty`
to create a map without knowing its internal representation.
This PR didn't touch folds and map as benchmark showed us
that it performs worse.
@GoPavel
Copy link

GoPavel commented Oct 21, 2024

Get: 1-2% slower
Folds: 5-6% slower
MapFilter: <1% slower

@GoPavel GoPavel added experiment experiment experiment:do-not-merge Unsuccessful optimization experiment and removed experiment experiment labels Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experiment:do-not-merge Unsuccessful optimization experiment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants