Skip to content

Commit

Permalink
Merge branch 'mkljakubowski-speedup-round1'
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Jan 1, 2025
2 parents 793b2a6 + b0e0cbd commit 8af3cc4
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions avrohugger-core/src/main/scala/generators/FileGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ private[avrohugger] class FileGenerator {
typeMatcher: TypeMatcher,
restrictedFields: Boolean,
targetScalaPartialVersion: String): Unit = {
val schemaOrProtocols = stringParser.getSchemaOrProtocols(str, schemaStore).distinct
schemaOrProtocols.foreach {
case Left(schema) =>
schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion)
case Right(protocol) =>
protocolToFile(protocol, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion)
}
distinctSchemaOrProtocol(stringParser.getSchemaOrProtocols(str, schemaStore))
.foreach {
case Left(schema) =>
schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion)
case Right(protocol) =>
protocolToFile(protocol, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion)
}
}

def fileToFile(
Expand All @@ -76,8 +76,7 @@ private[avrohugger] class FileGenerator {
classLoader: ClassLoader,
restrictedFields: Boolean,
targetScalaPartialVersion: String): Unit = {
fileParser.getSchemaOrProtocols(inFile, format, classStore, classLoader)
.distinct
distinctSchemaOrProtocol(fileParser.getSchemaOrProtocols(inFile, format, classStore, classLoader))
.foreach {
case Left(schema) =>
schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion)
Expand All @@ -97,8 +96,7 @@ private[avrohugger] class FileGenerator {
classLoader: ClassLoader,
restrictedFields: Boolean,
targetScalaPartialVersion: String): Unit = {
inFiles.flatMap(fileParser.getSchemaOrProtocols(_, format, classStore, classLoader))
.distinct
distinctSchemaOrProtocol(inFiles.flatMap(fileParser.getSchemaOrProtocols(_, format, classStore, classLoader)))
.foreach {
case Left(schema) =>
schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion)
Expand All @@ -107,4 +105,26 @@ private[avrohugger] class FileGenerator {
}
}

private def distinctSchemaOrProtocol(schemaOrProtocols: List[Either[Schema, Protocol]]): List[Either[Schema, Protocol]] = {
var processed = Set.empty[String]

schemaOrProtocols.flatMap {
case Left(schema) =>
if (!processed.contains(schema.getFullName)) {
processed += schema.getFullName
Some(Left(schema))
} else {
None
}
case Right(protocol) =>
val fullName = Option(protocol.getNamespace).map(ns => s"$ns.${protocol.getName}").getOrElse(protocol.getName)
if (!processed.contains(fullName)) {
processed += fullName
Some(Right(protocol))
} else {
None
}
}
}

}

0 comments on commit 8af3cc4

Please sign in to comment.