Upgrade sorting function #785

Merged
zxq9 merged 1 commits from mergesort into lima 2021-02-23 16:58:37 +09:00
zxq9 commented 2021-02-09 22:23:11 +09:00 (Migrated from gitlab.com)

Created by: radrow

Complexity matters. This shamelessly stolen from Haskell list library merge sort reduces gas usage by:

  • shit a lot on sorted lists
List.sort((a, b) => a < b, [1..50])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
call gas: 212521
Sort.sort((a, b) => a < b, [1..50])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
call gas: 6013
  • shit a lot on reverse-sorted lists
List.sort((a, b) => a < b, List.reverse([1..50]))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
call gas: 235762
Sort.sort((a, b) => a < b, List.reverse([1..50]))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
call gas: 6010
  • just a lot on shuffled lists
List.sort((a, b) => a < b, [(x * 19) mod 37 | x <- [1..50]])
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 12, 13,
 14, 15, 16, 17, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24,
 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
call gas: 69676
Sort.sort((a, b) => a < b, [(x * 19) mod 37 | x <- [1..50]])
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 12, 13,
 14, 15, 16, 17, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24,
 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
call gas: 27970

Here List is the original module and Sort is my testing one

*Created by: radrow* Complexity matters. This shamelessly stolen from [Haskell list library](https://hackage.haskell.org/package/base-4.14.1.0/docs/src/Data.OldList.html#sort) merge sort reduces gas usage by: * shit a lot on sorted lists ``` List.sort((a, b) => a < b, [1..50]) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] call gas: 212521 Sort.sort((a, b) => a < b, [1..50]) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] call gas: 6013 ``` * shit a lot on reverse-sorted lists ``` List.sort((a, b) => a < b, List.reverse([1..50])) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] call gas: 235762 Sort.sort((a, b) => a < b, List.reverse([1..50])) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] call gas: 6010 ``` * just a lot on shuffled lists ``` List.sort((a, b) => a < b, [(x * 19) mod 37 | x <- [1..50]]) [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36] call gas: 69676 Sort.sort((a, b) => a < b, [(x * 19) mod 37 | x <- [1..50]]) [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36] call gas: 27970 ``` Here `List` is the original module and `Sort` is my testing one
zxq9 commented 2021-02-11 19:55:03 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

Review: Approved

*Created by: UlfNorell* **Review:** Approved
zxq9 commented 2021-02-23 04:44:01 +09:00 (Migrated from gitlab.com)

Created by: hanssv

Review: Approved

*Created by: hanssv* **Review:** Approved
zxq9 commented 2021-02-23 16:58:37 +09:00 (Migrated from gitlab.com)

Merged by: radrow at 2021-02-23 07:58:37 UTC

*Merged by: radrow at 2021-02-23 07:58:37 UTC*
Sign in to join this conversation.
No description provided.