Skip to content

Commit

Permalink
core: use HashSet for reserved key rule (Netflix#1626)
Browse files Browse the repository at this point in the history
Some testing shows HashSet performs a little bit better (~5%)
for the contains check under high load for the keys we have.
  • Loading branch information
brharrington authored and manolama committed May 22, 2024
1 parent 7c78102 commit 056d539
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 056d539

Please sign in to comment.