DEV Community

SameX
SameX

Posted on

HarmonyOS Next Collection Type: Performance Graph from Array to Map

In HarmonyOS Next development, collection types are an important tool for organizing and managing data.From continuous storage of Array to hash bucket-based Map, different collection types have their advantages and disadvantages in terms of memory layout, access complexity, and thread safety.As a technical expert with rich experience in this field, I will analyze the characteristics of these collection types in depth and show through practical cases how to choose the optimal collection type in different scenarios to achieve efficient data processing.

Chapter 1: Memory Layout

Array stores elements in a continuous manner in memory, which makes it extremely efficient when accessed sequentially.For example, when iterating over an Array that stores integers, the CPU can use the cache prefetch mechanism to load multiple adjacent elements into the cache at once, reducing the number of memory accesses, thereby increasing access speed.

let intArray: Array<Int> = [1, 2, 3, 4, 5]
for (num in intArray) {
// Sequential access, high efficiency
    print(num)
}
Enter fullscreen mode Exit fullscreen mode

However, when inserting and deleting elements, especially when operating in the middle of an array, Array requires moving a large number of elements to maintain continuity, which leads to high time complexity.

Map uses a hash bucket to store key-value pairs.It hash the key and stores the key-value pairs into the corresponding hash bucket.This storage method makes Map have the time complexity of O(1) in the average case in the search operation, which is very suitable for quickly finding the value corresponding to a specific key.

let stringMap: Map<String, Int> = [:]
stringMap["one"] = 1
stringMap["two"] = 2
let value = stringMap["one"]
print(value)
Enter fullscreen mode Exit fullscreen mode

However, hash conflicts may lead to a decrease in search efficiency. When the hash conflict is serious, the search time complexity will be close to O(n).At the same time, the memory usage of Map is relatively complex. In addition to storing the key-value pairs themselves, additional space is needed to store data structures such as hash buckets and conflict-solving linked lists or red and black trees.

Chapter 2: Thread Safety

In a multithreaded environment, it is crucial to ensure the thread safety of the collection type.Using the Actor model, thread safety for concurrent collection access can be achieved.For example, suppose there is a shared Array, which multiple threads need to read and write:

actor SafeArrayActor {
    private var array: Array<Int> = []
    receiver func append(element: Int) {
        array.append(element)
    }
    receiver func get(index: Int) -> Int? {
        if index >= 0 && index < array.size {
            return array[index]
        }
        return nil
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above code, the SafeArrayActor encapsulates the operation on Array.Through the Actor model, different threads send messages to SafeArrayActor in sequence, avoiding data competition and inconsistency caused by multiple threads simultaneous access to Array.This ensures that the read and write operations of Array are safe in a concurrent environment.

For the Map type, a similar method can be used to encapsulate the operations on Map inside the Actor to ensure security in a multi-threaded environment.For example:

actor SafeMapActor {
    private var map: Map<String, Int> = [:]
    receiver func put(key: String, value: Int) {
        map[key] = value
    }
    receiver func get(key: String) -> Int? {
        return map[key]
    }
}
Enter fullscreen mode Exit fullscreen mode

Chapter 3: Pattern Matching

when expression is very useful when dealing with nested collections.For example, there is a nested collection containing elements of different types:

let nestedCollection: Array<Any> = [1, "two", [3, 4]]
when (nestedCollection[0]) {
    is Int: {
        let num = nestedCollection[0] as! Int
        print("The first element is an integer: \(num)")
    }
    is String: {
        let str = nestedCollection[0] as! String
        print("The first element is a string: \(str)")
    }
    is Array<Int>: {
        let intArray = nestedCollection[0] as! Array<Int>
        print("The first element is an array of integers: \(intArray)")
    }
    else: {
        print("The first element is of an unknown type")
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above code, by combining the when expression with the is check, it is possible to easily type match the elements in the nested set and perform corresponding operations.This pattern matching method makes the code more concise and easy to read, while improving the maintainability of the code.

Understanding the memory layout, thread-safe implementation and pattern matching skills of different collection types will help developers choose the most appropriate collection type according to specific scenarios in HarmonyOS Next development, thereby optimizing code performance and ensuring efficient operation of the program in various situations.

Top comments (0)