How does Scala's Vector work?

前端 未结 3 1283
醉梦人生
醉梦人生 2021-02-02 08:09

I read this page about the time complexity of Scala collections. As it says, Vector\'s complexity is eC for all operations.

It made me wonder w

3条回答
  •  时光说笑
    2021-02-02 08:16

    The other answers re 'Trie' are good. But as a close approximation, just for quick understanding:

    • Vector internally uses a tree structure - not a binary tree, but a 32-ary tree
    • Each '32-way node' uses Array[32] and can store either 0-32 references to child nodes or 0-32 pieces of data
    • The tree is structured to be balanced in a certain way - it is "n" levels deep, but levels 1 to n-1 are "index-only levels" (100% child references; no data) and level n contains all the data (100% data; no child references). So if the number of elements of data is "d" then n = log-base-32(d) rounded upwards

    Why this? Simple: for performance.

    Instead of doing thousands/millions/gazillions of memory allocations for each individual data element, memory is allocated in 32 element chunks. Instead of walking miles deep to find your data, the structure is quite shallow - it's a very wide, short tree. E.g. 5 levels deep can contain 32^5 data elements (for 4 byte elements = 132GB i.e. pretty big) and each data access would lookup & walk through 5 nodes from the root (whereas a big array would use a single data access). The vector does not proactively allocat memory for all of Level n (data), - it allocates in 32 element chunks as needed. It gives read performance somewhat similar to a huge array, whilst having functional characteristics (power & flexibility & memory-efficiency) somewhat similar to a binary tree.

    :)

提交回复
热议问题