Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7246

Optimising NamespacedKey

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • None
    • CraftBukkit version 3638-Spigot-d90018e-7dcb59b (MC: 1.19.3) (Implementing API version 1.19.3-R0.1-SNAPSHOT)
    • Yes

      Each creation of org.bukkit.NamespacedKey creates a Matcher to check if the namespace and key matches the compiled Pattern. This ends up throwing a lot of garbage on the heap and is a hot path as it's used everywhere. You can instead use the following methods from ResourceLocation (Mojang Mappings) as it's faster and doesn't allocate Matcher for each creation.
       
      For Key:

      private static boolean isValidPath(String path) {
      for(int i = 0; i < path.length(); ++i) {
      if (!validPathChar(path.charAt(i))) {
      return false;
      }
      }
      
      return true;
      }    public static boolean validPathChar(char character) {
      return character == '_' || character == '-' || character >= 'a' && character <= 'z' || character >= '0' && character <= '9' || character == '/' || character == '.';
      } 

       
      For Namespace

      private static boolean isValidNamespace(String namespace) {
      for(int i = 0; i < namespace.length(); ++i) {
      if (!validNamespaceChar(namespace.charAt(i))) {
      return false;
      }
      }
      
      return true;
      }   
      private static boolean validNamespaceChar(char character) {
      return character == '_' || character == '-' || character >= 'a' && character <= 'z' || character >= '0' && character <= '9' || character == '.';
      }

            Unassigned Unassigned
            Mersphydena Mersphydena
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: