Articles SortedSet[0] is basically a pseudo-BTree with fixed depth = 2 and fixed max_size of leafs. This gives O(log n) search and O(sqrt n) inserts [1].<p>It would mean that insertion at beginning is worst case scenario (split + need to move all buckets), but timing of insert is actually dominated by adding size of the buckets to calculate final index.<p>Spending a little bit of time on research, finding <a href="https://en.wikipedia.org/wiki/Order_statistic_tree" rel="nofollow">https://en.wikipedia.org/wiki/Order_statistic_tree</a> and just using G++ implementation would probably yield better result and less code to support.<p>G++ has __gnu_pbds which add O(log n) "find_by_order" and "order_of_key" to trees, e.g. [2].<p>[0] <a href="https://github.com/discord/sorted_set_nif/blob/master/native/sorted_set_nif" rel="nofollow">https://github.com/discord/sorted_set_nif/blob/master/native...</a><p>[1] Technically O(n/max_size + max_size) but we can assume that max_size is selected to be ~sqrt n<p>[2] <a href="https://www.ideone.com/8mzxGR" rel="nofollow">https://www.ideone.com/8mzxGR</a>