UnownedLocal of Harmony Foundation reduces RC overhead (Basic knowledge of HarmonyOS HarmonyOS development)

What is UnownedLocal?

Annotate methods or individual local variables.
Annotating a single local variable means to exclude instance references of this variable from the reference count. Annotating a method means revoking the method's ownership of all local variables (explicit or implicit) of the referenced object, and excluding references to these variables from the reference count of the referenced object.

The purpose of UnownedLocal is to reduce RC overhead, not to eliminate circular references.

For the unsafe HashMap.containsValue method in multithreaded mode, other threads are not allowed to modify the HashMap synchronously. Therefore, the reading of internal tables and nodes in HashMap is not included in the reference count, so the referenced objects will not be released. In this case, you can use UnownedLocal to annotate the object in the method.

   @UnownedLocal
   public boolean containsValue(Object value) {
       Node<K,V>[] tab;
       V v;
       if ((tab = table) != null && size > 0) {
           for (int i = 0; i < tab.length; ++i) {
               for (Node<K,V> e = tab[i]; e != null; e = e.next) {
                   if ((v = e.value) == value || (value != null && value.equals(v)))
                   return true;
               }
           }
       }
       return false;
   }
 
   public boolean containsValue(Object value) {
       Node<K,V>[] @Unowned tab;
       V @Unowned v;
       if ((tab = table) != null && size > 0) {
           for (int i = 0; i < tab.length; ++i) {
               for (Node<K,V> e = tab[i]; e != null; e = e.next) {
                   if ((v = e.value) == value || (value != null && value.equals(v)))
                       return true;
               }
           }
       }
       return false;
   }
 

Unauthorized reprinting is prohibited


For more technical exchanges, please join the QQ group

Group name: harmonyos Hongmeng Technology Exchange
Group number: 856567895


Start from scratch to learn HarmonyOS Hongmeng 2.0 development

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/108698509