Skip to content

Conversation

@christoph-dfinity
Copy link
Contributor

No description provided.

@christoph-dfinity christoph-dfinity changed the base branch from main to christoph/contextual-dot September 27, 2025 19:25
@github-actions
Copy link

github-actions bot commented Sep 27, 2025

No description provided.

@github-actions
Copy link

Benchmark Results

bench/ArrayBuilding.bench.mo $({\color{gray}0\%})$

Large known-size array building

Compares performance of different data structures for building arrays of known size.

Instructions: ${\color{gray}0\%}$
Heap: ${\color{gray}0\%}$
Stable Memory: ${\color{gray}0\%}$
Garbage Collection: ${\color{gray}0\%}$

Instructions

1000 100000 1000000
List 548_233 $({\color{gray}0\%})$ 48_324_535 $({\color{gray}0\%})$ 478_161_875 $({\color{gray}0\%})$
Buffer 342_005 $({\color{gray}0\%})$ 33_903_435 $({\color{gray}0\%})$ 339_003_650 $({\color{gray}0\%})$
pure/List 302_135 $({\color{gray}0\%})$ 30_003_590 $({\color{gray}0\%})$ 300_055_972 $({\color{gray}0\%})$
VarArray ?T 180_526 $({\color{gray}0\%})$ 17_802_956 $({\color{gray}0\%})$ 178_003_171 $({\color{gray}0\%})$
VarArray T 160_813 $({\color{gray}0\%})$ 15_803_243 $({\color{gray}0\%})$ 158_003_458 $({\color{gray}0\%})$
Array (baseline) 42_695 $({\color{gray}0\%})$ 4_003_125 $({\color{gray}0\%})$ 40_003_340 $({\color{gray}0\%})$

Heap

1000 100000 1000000
List 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
Buffer 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
pure/List 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
VarArray ?T 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
VarArray T 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
Array (baseline) 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$

Garbage Collection

1000 100000 1000000
List 10.05 KiB $({\color{gray}0\%})$ 797.56 KiB $({\color{gray}0\%})$ 7.67 MiB $({\color{gray}0\%})$
Buffer 8.71 KiB $({\color{gray}0\%})$ 782.15 KiB $({\color{gray}0\%})$ 7.63 MiB $({\color{gray}0\%})$
pure/List 19.95 KiB $({\color{gray}0\%})$ 1.91 MiB $({\color{gray}0\%})$ 19.07 MiB $({\color{gray}0\%})$
VarArray ?T 8.24 KiB $({\color{gray}0\%})$ 781.68 KiB $({\color{gray}0\%})$ 7.63 MiB $({\color{gray}0\%})$
VarArray T 8.23 KiB $({\color{gray}0\%})$ 781.67 KiB $({\color{gray}0\%})$ 7.63 MiB $({\color{gray}0\%})$
Array (baseline) 4.3 KiB $({\color{gray}0\%})$ 391.02 KiB $({\color{gray}0\%})$ 3.82 MiB $({\color{gray}0\%})$
bench/FromIters.bench.mo $({\color{gray}0\%})$

Benchmarking the fromIter functions

Columns describe the number of elements in the input iter.

Instructions: ${\color{gray}0\%}$
Heap: ${\color{gray}0\%}$
Stable Memory: ${\color{gray}0\%}$
Garbage Collection: ${\color{gray}0\%}$

Instructions

100 10_000 100_000
Array.fromIter 48_764 $({\color{gray}0\%})$ 4_712_025 $({\color{gray}0\%})$ 47_103_135 $({\color{gray}0\%})$
List.fromIter 31_698 $({\color{gray}0\%})$ 3_061_541 $({\color{gray}0\%})$ 30_603_553 $({\color{gray}0\%})$
List.fromIter . Iter.reverse 50_297 $({\color{gray}0\%})$ 4_832_563 $({\color{gray}0\%})$ 48_305_477 $({\color{gray}0\%})$

Heap

100 10_000 100_000
Array.fromIter 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
List.fromIter 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
List.fromIter . Iter.reverse 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$

Garbage Collection

100 10_000 100_000
Array.fromIter 2.76 KiB $({\color{gray}0\%})$ 234.79 KiB $({\color{gray}0\%})$ 2.29 MiB $({\color{gray}0\%})$
List.fromIter 3.51 KiB $({\color{gray}0\%})$ 312.88 KiB $({\color{gray}0\%})$ 3.05 MiB $({\color{gray}0\%})$
List.fromIter . Iter.reverse 5.11 KiB $({\color{gray}0\%})$ 469.17 KiB $({\color{gray}0\%})$ 4.58 MiB $({\color{gray}0\%})$
bench/ListBufferNewArray.bench.mo $({\color{gray}0\%})$

List vs. Buffer for creating known-size arrays

Performance comparison between List and Buffer for creating a new array.

Instructions: ${\color{gray}0\%}$
Heap: ${\color{gray}0\%}$
Stable Memory: ${\color{gray}0\%}$
Garbage Collection: ${\color{gray}0\%}$

Instructions

0 (baseline) 1 5 10 100 (for loop)
List 1_547 $({\color{gray}0\%})$ 2_916 $({\color{gray}0\%})$ 9_046 $({\color{gray}0\%})$ 13_948 $({\color{gray}0\%})$ 74_564 $({\color{gray}0\%})$
pure/List 1_247 $({\color{gray}0\%})$ 1_355 $({\color{gray}0\%})$ 2_439 $({\color{gray}0\%})$ 3_801 $({\color{gray}0\%})$ 31_868 $({\color{gray}0\%})$
Buffer 2_119 $({\color{gray}0\%})$ 2_271 $({\color{gray}0\%})$ 3_518 $({\color{gray}0\%})$ 5_085 $({\color{gray}0\%})$ 36_640 $({\color{gray}0\%})$

Heap

0 (baseline) 1 5 10 100 (for loop)
List 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
pure/List 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$
Buffer 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$ 272 B $({\color{gray}0\%})$

Garbage Collection

0 (baseline) 1 5 10 100 (for loop)
List 576 B $({\color{gray}0\%})$ 616 B $({\color{gray}0\%})$ 776 B $({\color{gray}0\%})$ 884 B $({\color{gray}0\%})$ 1.93 KiB $({\color{gray}0\%})$
pure/List 360 B $({\color{gray}0\%})$ 380 B $({\color{gray}0\%})$ 460 B $({\color{gray}0\%})$ 560 B $({\color{gray}0\%})$ 2.3 KiB $({\color{gray}0\%})$
Buffer 856 B $({\color{gray}0\%})$ 864 B $({\color{gray}0\%})$ 896 B $({\color{gray}0\%})$ 936 B $({\color{gray}0\%})$ 1.62 KiB $({\color{gray}0\%})$
bench/PureListStackSafety.bench.mo $({\color{gray}0\%})$

List Stack safety

Check stack-safety of the following pure/List-related functions.

Instructions: ${\color{gray}0\%}$
Heap: ${\color{gray}0\%}$
Stable Memory: ${\color{gray}0\%}$
Garbage Collection: ${\color{gray}0\%}$

Instructions

pure/List.split 24_602_524 $({\color{gray}0\%})$
pure/List.all 7_901_014 $({\color{gray}0\%})$
pure/List.any 8_001_390 $({\color{gray}0\%})$
pure/List.map 23_103_767 $({\color{gray}0\%})$
pure/List.filter 21_104_188 $({\color{gray}0\%})$
pure/List.filterMap 27_404_742 $({\color{gray}0\%})$
pure/List.partition 21_304_994 $({\color{gray}0\%})$
pure/List.join 33_105_326 $({\color{gray}0\%})$
pure/List.flatten 24_805_667 $({\color{gray}0\%})$
pure/List.take 24_605_664 $({\color{gray}0\%})$
pure/List.drop 9_904_119 $({\color{gray}0\%})$
pure/List.foldRight 19_105_768 $({\color{gray}0\%})$
pure/List.merge 31_808_584 $({\color{gray}0\%})$
pure/List.chunks 51_510_344 $({\color{gray}0\%})$
pure/Queue 142_662_505 $({\color{gray}0\%})$

Heap

pure/List.split 272 B $({\color{gray}0\%})$
pure/List.all 272 B $({\color{gray}0\%})$
pure/List.any 272 B $({\color{gray}0\%})$
pure/List.map 272 B $({\color{gray}0\%})$
pure/List.filter 272 B $({\color{gray}0\%})$
pure/List.filterMap 272 B $({\color{gray}0\%})$
pure/List.partition 272 B $({\color{gray}0\%})$
pure/List.join 272 B $({\color{gray}0\%})$
pure/List.flatten 272 B $({\color{gray}0\%})$
pure/List.take 272 B $({\color{gray}0\%})$
pure/List.drop 272 B $({\color{gray}0\%})$
pure/List.foldRight 272 B $({\color{gray}0\%})$
pure/List.merge 272 B $({\color{gray}0\%})$
pure/List.chunks 272 B $({\color{gray}0\%})$
pure/Queue 272 B $({\color{gray}0\%})$

Garbage Collection

pure/List.split 3.05 MiB $({\color{gray}0\%})$
pure/List.all 328 B $({\color{gray}0\%})$
pure/List.any 328 B $({\color{gray}0\%})$
pure/List.map 3.05 MiB $({\color{gray}0\%})$
pure/List.filter 3.05 MiB $({\color{gray}0\%})$
pure/List.filterMap 3.05 MiB $({\color{gray}0\%})$
pure/List.partition 3.05 MiB $({\color{gray}0\%})$
pure/List.join 3.05 MiB $({\color{gray}0\%})$
pure/List.flatten 3.05 MiB $({\color{gray}0\%})$
pure/List.take 3.05 MiB $({\color{gray}0\%})$
pure/List.drop 328 B $({\color{gray}0\%})$
pure/List.foldRight 1.53 MiB $({\color{gray}0\%})$
pure/List.merge 4.58 MiB $({\color{gray}0\%})$
pure/List.chunks 7.63 MiB $({\color{gray}0\%})$
pure/Queue 18.31 MiB $({\color{gray}0\%})$
bench/Queues.bench.mo $({\color{gray}0\%})$

Different queue implementations

Compare the performance of the following queue implementations:

  • pure/Queue: The default immutable double-ended queue implementation.
    • Pros: Good amortized performance, meaning that the average cost of operations is low O(1).
    • Cons: In worst case, an operation can take O(size) time rebuilding the queue as demonstrated in the Pop front 2 elements scenario.
  • pure/RealTimeQueue
    • Pros: Every operation is guaranteed to take at most O(1) time and space.
    • Cons: Poor amortized performance: Instruction cost is on average 3x for pop and 8x for push compared to pure/Queue.
  • mutable Queue
    • Pros: Also O(1) guarantees with a lower constant factor than pure/RealTimeQueue. Amortized performance is comparable to pure/Queue.
    • Cons: It is mutable and cannot be used in shared types (not shareable).

Instructions: ${\color{gray}0\%}$
Heap: ${\color{gray}0\%}$
Stable Memory: ${\color{gray}0\%}$
Garbage Collection: ${\color{gray}0\%}$

Instructions

pure/Queue pure/RealTimeQueue mutable Queue
Initialize with 2 elements 3_092 $({\color{gray}0\%})$ 2_304 $({\color{gray}0\%})$ 3_040 $({\color{gray}0\%})$
Push 500 elements 90_713 $({\color{gray}0\%})$ 744_219 $({\color{gray}0\%})$ 219_284 $({\color{gray}0\%})$
Pop front 2 elements 86_966 $({\color{gray}0\%})$ 4_446 $({\color{gray}0\%})$ 3_847 $({\color{gray}0\%})$
Pop 150 front&back 92_095 $({\color{gray}0\%})$ 304_908 $({\color{gray}0\%})$ 124_581 $({\color{gray}0\%})$

Heap

pure/Queue pure/RealTimeQueue mutable Queue
Initialize with 2 elements 324 B $({\color{gray}0\%})$ 300 B $({\color{gray}0\%})$ 352 B $({\color{gray}0\%})$
Push 500 elements 8.08 KiB $({\color{gray}0\%})$ 8.17 KiB $({\color{gray}0\%})$ 19.8 KiB $({\color{gray}0\%})$
Pop front 2 elements 240 B $({\color{gray}0\%})$ 240 B $({\color{gray}0\%})$ 192 B $({\color{gray}0\%})$
Pop 150 front&back -4.42 KiB $({\color{gray}0\%})$ -492 B $({\color{gray}0\%})$ -11.45 KiB $({\color{gray}0\%})$

Garbage Collection

pure/Queue pure/RealTimeQueue mutable Queue
Initialize with 2 elements 508 B $({\color{gray}0\%})$ 444 B $({\color{gray}0\%})$ 456 B $({\color{gray}0\%})$
Push 500 elements 10.1 KiB $({\color{gray}0\%})$ 137.84 KiB $({\color{gray}0\%})$ 344 B $({\color{gray}0\%})$
Pop front 2 elements 12.19 KiB $({\color{gray}0\%})$ 528 B $({\color{gray}0\%})$ 424 B $({\color{gray}0\%})$
Pop 150 front&back 15.61 KiB $({\color{gray}0\%})$ 49.66 KiB $({\color{gray}0\%})$ 12.1 KiB $({\color{gray}0\%})$

Note: Renamed benchmarks cannot be compared. Refer to the current baseline for manual comparison.

@crusso
Copy link
Contributor

crusso commented Sep 27, 2025

Actually, we can even make toText work now by using a let with an explicit type annotation to overload the two arguments manually

let toText : ... = func ...

@christoph-dfinity christoph-dfinity changed the title experiment: use implicits on Map experiment: use implicits on Map & Set Sep 28, 2025
* First pass

* Another pass

* Fix stragglers

* Fix more stragglers

* Apply suggestions from code review

Co-authored-by: Christoph <christoph.hegemann@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>

* Apply suggestions from code review

Co-authored-by: Claudio Russo <claudio@dfinity.org>

* Various fixes

* Apply suggestions from code review

Co-authored-by: Claudio Russo <claudio@dfinity.org>

* Fix

* Update API lockfile

---------

Co-authored-by: Christoph <christoph.hegemann@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
@christoph-dfinity christoph-dfinity changed the title experiment: use implicits on Map & Set feat: support for implicits Sep 29, 2025
crusso and others added 7 commits September 29, 2025 20:44
* Partial implementation.

* Fix bug and add peek.

* Started with tests, and more functions.

* Towards more tests, adding SetPriorityQueue.

* Completing first implementation, with tests.

* Add comments to PriorityQueue.mo

* Move set implementation and add benchmark.

* Better holes.

* Extra benchmark.

* More tests.

* .

* Comments.

* Optimization.

* Comments for SetWrapper.

* Move SetWrapper.

* Run format.

* .

* npm run validate

* Fix imports in docstrings.

* Add Changelog entry.

* Fix Changelog entry.

* Remove inefficient push and pop operations (and benchmarks for them).

* Move PriorityQueueSet.mo

* Fix validation.

* Update Changelog.
@christoph-dfinity christoph-dfinity marked this pull request as ready for review October 13, 2025 13:58
@christoph-dfinity christoph-dfinity requested a review from a team as a code owner October 13, 2025 13:59
@christoph-dfinity christoph-dfinity merged commit 68196a7 into christoph/contextual-dot Oct 13, 2025
7 of 8 checks passed
@christoph-dfinity christoph-dfinity deleted the christoph/implicits branch October 13, 2025 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants