Skip to content

Commit

Permalink
optimize, remove option verbose (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin authored Mar 24, 2022
1 parent fb93718 commit c288e9b
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 217 deletions.
6 changes: 3 additions & 3 deletions cacheable-benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ redis {

```
[info] Benchmark (limitRandNum) Mode Cnt Score Error Units
[info] CacheableBenchmarks.benchmarkCaffeineCache 2 thrpt 15 668084.614 ± 20389.518 ops/s
[info] CacheableBenchmarks.benchmarkNoCache 2 thrpt 15 164.198 ± 1.656 ops/s
[info] CacheableBenchmarks.benchmarkRedisCache 2 thrpt 15 663.941 ± 316.381 ops/s
[info] CacheableBenchmarks.benchmarkCaffeineCache 2 thrpt 15 562046.757 ± 28099.275 ops/s
[info] CacheableBenchmarks.benchmarkNoCache 2 thrpt 15 164.256 ± 3.277 ops/s
[info] CacheableBenchmarks.benchmarkRedisCache 2 thrpt 15 576.943 ± 336.063 ops/s
```
1 change: 1 addition & 0 deletions tools/src/main/scala/org/bitlap/tools/LogLevel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object LogLevel extends Enumeration {
val INFO, WARN, DEBUG = Value

private[bitlap] def getLogLevel(shortType: String): LogLevel = {
// TODO not good way
val tpe1 = s"$PACKAGE.elapsed.$shortType" //LogLevel.INFO
val tpe2 = s"$PACKAGE.elapsed.LogLevel.$shortType" // INFO
val v = LogLevel.values.find(p => {
Expand Down
3 changes: 1 addition & 2 deletions tools/src/main/scala/org/bitlap/tools/apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import scala.annotation.{ StaticAnnotation, compileTimeOnly }
* annotation to generate apply method for primary construction of ordinary classes.
*
* @author 梦境迷离
* @param verbose Whether to enable detailed log.
* @since 2021/6/30
* @version 1.0
*/
@compileTimeOnly("enable macro to expand macro annotations")
final class apply(verbose: Boolean = false) extends StaticAnnotation {
final class apply extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro applyMacro.applyProcessor.impl
}
3 changes: 1 addition & 2 deletions tools/src/main/scala/org/bitlap/tools/constructor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import scala.annotation.{ StaticAnnotation, compileTimeOnly }
* annotation to generate secondary constructor method for classes.
*
* @author 梦境迷离
* @param verbose Whether to enable detailed log.
* @param excludeFields Whether to exclude the specified var fields.
* @since 2021/7/3
* @version 1.0
*/
@compileTimeOnly("enable macro to expand macro annotations")
final class constructor(verbose: Boolean = false, excludeFields: Seq[String] = Nil) extends StaticAnnotation {
final class constructor(excludeFields: Seq[String] = Nil) extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro constructorMacro.ConstructorProcessor.impl
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import scala.annotation.{ StaticAnnotation, compileTimeOnly }
* annotation to generate equals and hashcode method for classes.
*
* @author 梦境迷离
* @param verbose Whether to enable detailed log.
* @param excludeFields Whether to exclude the specified internal fields.
* @since 2021/7/18
* @version 1.0
*/
@compileTimeOnly("enable macro to expand macro annotations")
final class equalsAndHashCode(verbose: Boolean = false, excludeFields: Seq[String] = Nil) extends StaticAnnotation {
final class equalsAndHashCode(excludeFields: Seq[String] = Nil) extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro EqualsAndHashCodeProcessor.impl
}
3 changes: 1 addition & 2 deletions tools/src/main/scala/org/bitlap/tools/jacksonEnum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import scala.annotation.{ StaticAnnotation, compileTimeOnly }
* @author 梦境迷离
* @author choly
*
* @param verbose Whether to enable detailed log.
* @param nonTypeRefers Whether to not generate the subclass of the TypeReference for paramTypes of class.
* @since 2021/8/3
* @version 1.0
*/
@compileTimeOnly("enable macro to expand macro annotations")
final class jacksonEnum(verbose: Boolean = false, nonTypeRefers: Seq[String] = Nil) extends StaticAnnotation {
final class jacksonEnum(nonTypeRefers: Seq[String] = Nil) extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro jacksonEnumMacro.JacksonEnumProcessor.impl
}
3 changes: 1 addition & 2 deletions tools/src/main/scala/org/bitlap/tools/javaCompatible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import org.bitlap.tools.macros.javaCompatibleMacro
* Fields marked `private[this]` in curry are not supported !
*
* @author 梦境迷离
* @param verbose Whether to enable detailed log.
* @since 2021/11/23
* @version 1.0
*/
@compileTimeOnly("enable macro to expand macro annotations")
final class javaCompatible(verbose: Boolean = false) extends StaticAnnotation {
final class javaCompatible extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro javaCompatibleMacro.JavaCompatibleProcessor.impl
}
3 changes: 1 addition & 2 deletions tools/src/main/scala/org/bitlap/tools/log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ import scala.annotation.{ compileTimeOnly, StaticAnnotation }
* annotation to generate log.
*
* @author 梦境迷离
* @param verbose Whether to enable detailed log.
* @param logType Specifies the type of `log` that needs to be generated
* @since 2021/6/28
* @version 1.0
*/
@compileTimeOnly("enable macro to expand macro annotations")
final class log(verbose: Boolean = false, logType: LogType.LogType = LogType.JLog) extends StaticAnnotation {
final class log(logType: LogType.LogType = LogType.JLog) extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro logMacro.LogProcessor.impl
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {

protected final lazy val SDKClasses = Set("java.lang.Object", "scala.AnyRef")

protected val verbose: Boolean = false

/**
* Subclasses should override the method and return the final result abstract syntax tree, or an abstract syntax tree close to the final result.
*
Expand All @@ -58,7 +56,7 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
def impl(annottees: Expr[Any]*): Expr[Any] = {
checkAnnottees(annottees)
val resTree = collectCustomExpr(annottees)(createCustomExpr)
printTree(force = verbose, resTree.tree)
printTree(force = true, resTree.tree)
resTree
}

Expand All @@ -78,15 +76,6 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
*/
def evalTree[T: WeakTypeTag](tree: Tree): T = c.eval(c.Expr[T](c.untypecheck(tree.duplicate)))

def extractArgumentsTuple2[T1: WeakTypeTag, T2: WeakTypeTag](partialFunction: PartialFunction[Tree, (T1, T2)]): (T1, T2) = {
partialFunction.apply(c.prefix.tree)
}

def extractArgumentsTuple4[T1: WeakTypeTag, T2: WeakTypeTag, T3: WeakTypeTag, T4: WeakTypeTag]
(partialFunction: PartialFunction[Tree, (T1, T2, T3, T4)]): (T1, T2, T3, T4) = {
partialFunction.apply(c.prefix.tree)
}

/**
* Output ast result.
*
Expand Down
10 changes: 0 additions & 10 deletions tools/src/main/scala/org/bitlap/tools/macros/applyMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ object applyMacro {

import c.universe._

private val extractArgumentsDetail: Tuple1[Boolean] = {
c.prefix.tree match {
case q"new apply(verbose=$verbose)" => Tuple1(evalTree(verbose.asInstanceOf[Tree]))
case q"new apply()" => Tuple1(false)
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}
}

override def createCustomExpr(classDecl: ClassDef, compDeclOpt: Option[ModuleDef] = None): Any = {
val classDefinition = mapToClassDeclInfo(classDecl)
val apply = getApplyMethodWithCurrying(classDefinition.className, classDefinition.classParamss, classDefinition.classTypeParams)
Expand All @@ -61,8 +53,6 @@ object applyMacro {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_CASE_CLASS)
}
}

override protected val verbose: Boolean = extractArgumentsDetail._1
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ object constructorMacro {

import c.universe._

private val extractArgumentsDetail: Tuple2[Boolean, Seq[String]] = {
extractArgumentsTuple2 {
case q"new constructor(verbose=$verbose)" => (evalTree(verbose.asInstanceOf[Tree]), Nil)
case q"new constructor(excludeFields=$excludeFields)" => (false, evalTree(excludeFields.asInstanceOf[Tree]))
case q"new constructor(verbose=$verbose, excludeFields=$excludeFields)" => (evalTree(verbose.asInstanceOf[Tree]), evalTree(excludeFields.asInstanceOf[Tree]))
case q"new constructor()" => (false, Nil)
private val extractArgs: Seq[String] = {
c.prefix.tree match {
case q"new constructor(excludeFields=$excludeFields)" => evalTree(excludeFields.asInstanceOf[Tree])
case q"new constructor($excludeFields)" => evalTree(excludeFields.asInstanceOf[Tree])
case q"new constructor()" => Seq.empty[String]
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}
}

private def getMutableValDefAndExcludeFields(annotteeClassDefinitions: Seq[Tree]): Seq[c.universe.ValDef] = {
getClassMemberValDefs(annotteeClassDefinitions).filter(v => v.mods.hasFlag(Flag.MUTABLE) &&
!extractArgumentsDetail._2.contains(v.name.decodedName.toString))
!extractArgs.contains(v.name.decodedName.toString))
}

/**
Expand Down Expand Up @@ -116,7 +115,6 @@ object constructorMacro {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_CLASS)
}
}
override val verbose: Boolean = extractArgumentsDetail._1
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object elapsedMacro {
}
}

private val extractArgumentsDetail: (Duration, LogLevel) = extractArgumentsTuple2 {
private val extractArgumentsDetail: (Duration, LogLevel) = c.prefix.tree match {
case q"new elapsed(limit=$limit, logLevel=$logLevel)" => (evalTree(limit.asInstanceOf[Tree]), getLogLevel(logLevel.asInstanceOf[Tree]))
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ object equalsAndHashCodeMacro {

import c.universe._

private val extractArgumentsDetail: (Boolean, Nil.type) = extractArgumentsTuple2 {
case q"new equalsAndHashCode(verbose=$verbose)" => (evalTree(verbose.asInstanceOf[Tree]), Nil)
case q"new equalsAndHashCode(excludeFields=$excludeFields)" => (false, evalTree(excludeFields.asInstanceOf[Tree]))
case q"new equalsAndHashCode(verbose=$verbose, excludeFields=$excludeFields)" => (evalTree(verbose.asInstanceOf[Tree]), evalTree(excludeFields.asInstanceOf[Tree]))
case q"new equalsAndHashCode()" => (false, Nil)
private val extractArgs: Seq[String] = c.prefix.tree match {
case q"new equalsAndHashCode(excludeFields=$excludeFields)" => evalTree(excludeFields.asInstanceOf[Tree])
case q"new equalsAndHashCode($excludeFields)" => evalTree(excludeFields.asInstanceOf[Tree])
case q"new equalsAndHashCode()" => Nil
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}

Expand All @@ -51,17 +50,15 @@ object equalsAndHashCodeMacro {
}
}

override val verbose: Boolean = extractArgumentsDetail._1

/**
* Extract the internal fields of members belonging to the class.
*/
private def getInternalFieldsTermNameExcludeLocal(annotteeClassDefinitions: Seq[Tree]): Seq[TermName] = {
if (annotteeClassDefinitions.exists(f => isNotLocalClassMember(f))) {
c.info(c.enclosingPosition, s"There is a non private class definition inside the class", extractArgumentsDetail._1)
c.info(c.enclosingPosition, s"There is a non private class definition inside the class", true)
}
getClassMemberValDefs(annotteeClassDefinitions).filter(p => isNotLocalClassMember(p) &&
!extractArgumentsDetail._2.contains(p.name.decodedName.toString)).map(_.name.toTermName)
!extractArgs.contains(p.name.decodedName.toString)).map(_.name.toTermName)
}

// equals method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ object jacksonEnumMacro {

import c.universe._

private val extractArgumentsDetail: Tuple2[Boolean, Seq[String]] = {
extractArgumentsTuple2 {
case q"new jacksonEnum(verbose=$verbose, nonTypeRefers=$nonTypeRefers)" => Tuple2(evalTree(verbose.asInstanceOf[Tree]), evalTree(nonTypeRefers.asInstanceOf[Tree]))
case q"new jacksonEnum(nonTypeRefers=$nonTypeRefers)" => Tuple2(false, evalTree(nonTypeRefers.asInstanceOf[Tree]))
case q"new jacksonEnum()" => Tuple2(false, Nil)
private val extractArgs: Seq[String] = {
c.prefix.tree match {
case q"new jacksonEnum(nonTypeRefers=$nonTypeRefers)" => evalTree(nonTypeRefers.asInstanceOf[Tree])
case q"new jacksonEnum($nonTypeRefers)" => evalTree(nonTypeRefers.asInstanceOf[Tree])
case q"new jacksonEnum()" => Nil
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}
}
Expand All @@ -43,7 +43,7 @@ object jacksonEnumMacro {
// Enum ?
safeValDefs.filter(_.symbol.name.toTermName.toString == "Value").
map(getTypeTermName).
filter(v => !extractArgumentsDetail._2.contains(v.decodedName.toString)).
filter(v => !extractArgs.contains(v.decodedName.toString)).
distinct.
map(c => q"""class ${TypeName(c.decodedName.toString + "TypeRefer")} extends _root_.com.fasterxml.jackson.core.`type`.TypeReference[$c.type]""")
}
Expand All @@ -68,16 +68,14 @@ object jacksonEnumMacro {
// duplication should be removed
val mods = safeValDef.mods.mapAnnotations(f => {
if (!f.toString().contains("JsonScalaEnumeration") &&
!extractArgumentsDetail._2.contains(getTypeTermName(safeValDef).decodedName.toString)) f ++ List(getAnnotation(valDefTree)) else f
!extractArgs.contains(getTypeTermName(safeValDef).decodedName.toString)) f ++ List(getAnnotation(valDefTree)) else f
})
ValDef(mods, safeValDef.name, safeValDef.tpt, safeValDef.rhs)
} else {
valDefTree
}
}

override val verbose: Boolean = extractArgumentsDetail._1

override def createCustomExpr(classDecl: c.universe.ClassDef, compDeclOpt: Option[c.universe.ModuleDef]): Any = {
// return all typeReferClasses and new classDef
val classDefinition = mapToClassDeclInfo(classDecl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object javaCompatibleMacro {
*/
private def getNoArgsContrWithCurrying(annotteeClassParams: List[List[Tree]], annotteeClassDefinitions: Seq[Tree]): Tree = {
if (annotteeClassDefinitions.exists(f => !isNotLocalClassMember(f))) {
c.info(c.enclosingPosition, s"The params of 'private[this]' exists in class constructor", verbose)
c.info(c.enclosingPosition, s"The params of 'private[this]' exists in class constructor", true)
}
annotteeClassDefinitions.foreach {
case defDef: DefDef if defDef.name.decodedName.toString == "this" && defDef.vparamss.isEmpty =>
Expand Down
23 changes: 11 additions & 12 deletions tools/src/main/scala/org/bitlap/tools/macros/logMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ object logMacro {

import c.universe._

private val extractArgumentsDetail: (Boolean, logs.LogType.Value) = extractArgumentsTuple2 {
private val extractArgs: logs.LogType.Value = c.prefix.tree match {
case q"new log(logType=$logType)" =>
val tpe = getLogType(logType.asInstanceOf[Tree])
(false, tpe)
case q"new log(verbose=$verbose)" => (evalTree(verbose.asInstanceOf[Tree]), LogType.JLog)
case q"new log(verbose=$verbose, logType=$logType)" =>
tpe
case q"new log($logType)" =>
val tpe = getLogType(logType.asInstanceOf[Tree])
(evalTree(verbose.asInstanceOf[Tree]), tpe)
case q"new log()" => (false, LogType.JLog)
tpe
case q"new log()" => LogType.JLog
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}

Expand All @@ -63,11 +62,11 @@ object logMacro {
val buildArg = (name: Name) => LogTransferArgument(name.toTermName.decodedName.toString, isClass = true)
(annottees.map(_.tree) match {
case (classDef: ClassDef) :: Nil =>
LogType.getLogImpl(extractArgumentsDetail._2).getTemplate(c)(buildArg(classDef.name))
LogType.getLogImpl(extractArgs).getTemplate(c)(buildArg(classDef.name))
case (moduleDef: ModuleDef) :: Nil =>
LogType.getLogImpl(extractArgumentsDetail._2).getTemplate(c)(buildArg(moduleDef.name).copy(isClass = false))
LogType.getLogImpl(extractArgs).getTemplate(c)(buildArg(moduleDef.name).copy(isClass = false))
case (classDef: ClassDef) :: (_: ModuleDef) :: Nil =>
LogType.getLogImpl(extractArgumentsDetail._2).getTemplate(c)(buildArg(classDef.name))
LogType.getLogImpl(extractArgs).getTemplate(c)(buildArg(classDef.name))
case _ => c.abort(c.enclosingPosition, ErrorMessage.ONLY_OBJECT_CLASS)
}).asInstanceOf[Tree]
}
Expand All @@ -78,7 +77,7 @@ object logMacro {
if (classDef.mods.hasFlag(Flag.CASE)) {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_OBJECT_CLASS)
}
val newClass = extractArgumentsDetail._2 match {
val newClass = extractArgs match {
case ScalaLoggingLazy | ScalaLoggingStrict =>
appendImplDefSuper(checkGetClassDef(annottees), _ => List(logTree(annottees)))
case _ =>
Expand All @@ -90,7 +89,7 @@ object logMacro {
$newClass
"""
case (_: ModuleDef) :: _ =>
extractArgumentsDetail._2 match {
extractArgs match {
case ScalaLoggingLazy | ScalaLoggingStrict => appendImplDefSuper(getModuleDefOption(annottees).get, _ => List(logTree(annottees)))
case _ => prependImplDefBody(getModuleDefOption(annottees).get, _ => List(logTree(annottees)))
}
Expand All @@ -99,7 +98,7 @@ object logMacro {
// see https://docs.scala-lang.org/overviews/macros/annotations.html
}

printTree(force = extractArgumentsDetail._1, resTree)
printTree(force = true, resTree)
c.Expr[Any](resTree)
}
}
Expand Down
Loading

0 comments on commit c288e9b

Please sign in to comment.