ScorexFoundation / sigmastate-interpreter

ErgoScript compiler and ErgoTree Interpreter implementation for Ergo blockchain
MIT License
61 stars 40 forks source link

New collection methods for 6.0.0 #1004

Open kushti opened 3 months ago

kushti commented 3 months ago

Methods to add in 6.0

From #418:

From #479 and also reverse

Consider

"Currently it partially implemented. See also corresponding TODOs referring to this issue."

Methods which were proposed but will not be implemented in ErgoTree :

However, they support can be added to the frontend compiler, thus the compiler will replace the method with ones existing in the tree

aslesarenko commented 3 months ago

diff can be replaced by something like c1.filter({(b: Byte) => c2.indexOf(b, 0) == -1 })

This will have O(n^2) complexity, which is, combined with unlimited collection lengths can lead to potential attack vectors. The diff can be implemented with O(n) complexity using HashMap.

mapReduce - ??? - no details provided

This is quite generic operation which can be used in many use cases (for example groupBy can be implemented with mapReduce). Same motivation as for diff, direct implementation can be O(n) using HashMap internally, which is not possible since we don't have Map type in ErgoTree.

groupBy - ??? it is producing Map in Scala SDK, what was the plan here?

having mapReduce we can implement groupBy as: def groupBy(key: T => K): Coll[(K, Coll[T])] = mapReduce(t => (key(t), Coll(t)), ((_,t1), (_, t2)) => t1 ++ t2) This is not very efficient (and the cost will be high), but at least possible. So, the plan was to have native implementation using ArrayBuilders internally.