From 056d539a0487007c03120de9e266e7996f9a3d48 Mon Sep 17 00:00:00 2001 From: brharrington Date: Thu, 14 Mar 2024 15:19:06 -0500 Subject: [PATCH] core: use HashSet for reserved key rule (#1626) Some testing shows HashSet performs a little bit better (~5%) for the contains check under high load for the keys we have. --- .../netflix/atlas/core/validation/ReservedKeyRule.scala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/atlas-core/src/main/scala/com/netflix/atlas/core/validation/ReservedKeyRule.scala b/atlas-core/src/main/scala/com/netflix/atlas/core/validation/ReservedKeyRule.scala index 6f51ab2cb..9099a14fc 100644 --- a/atlas-core/src/main/scala/com/netflix/atlas/core/validation/ReservedKeyRule.scala +++ b/atlas-core/src/main/scala/com/netflix/atlas/core/validation/ReservedKeyRule.scala @@ -17,6 +17,8 @@ package com.netflix.atlas.core.validation import com.typesafe.config.Config +import java.util + /** * Verifies that only allowed keys are used for reserved prefixes. Reserved prefixes are used * to prevent user defined tags from overlapping with common infrastructure tagging that should @@ -31,8 +33,13 @@ import com.typesafe.config.Config */ case class ReservedKeyRule(prefix: String, allowedKeys: Set[String]) extends TagRule { + import scala.jdk.CollectionConverters.* + + // Used for contains check as it testing shows it to have better performance + private val hashSet = new util.HashSet[String](allowedKeys.asJava) + override def validate(k: String, v: String): String = { - if (k.startsWith(prefix) && !allowedKeys.contains(k)) + if (k.startsWith(prefix) && !hashSet.contains(k)) s"invalid key for reserved prefix '$prefix': $k" else TagRule.Pass