Skip to content

Commit

Permalink
Minor performance improvements
Browse files Browse the repository at this point in the history
* O(n^2) -> O(n) for result collecting in async, finagle libs
* minor improvement for spark array nulls check
  • Loading branch information
a14e committed Sep 1, 2018
1 parent 84cc55d commit 38d0565
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,25 @@ abstract class AsyncContext[D <: SqlIdiom, N <: NamingStrategy, C <: Connection]
Future.sequence {
groups.map {
case BatchGroup(sql, prepare) =>
prepare.foldLeft(Future.successful(List.empty[Long])) {
prepare.foldLeft(Future.successful(List.newBuilder[Long])) {
case (acc, prepare) =>
acc.flatMap { list =>
executeAction(sql, prepare).map(list :+ _)
executeAction(sql, prepare).map(list += _)
}
}
}.map(_.result())
}
}.map(_.flatten.toList)

def executeBatchActionReturning[T](groups: List[BatchGroupReturning], extractor: Extractor[T])(implicit ec: ExecutionContext): Future[List[T]] =
Future.sequence {
groups.map {
case BatchGroupReturning(sql, column, prepare) =>
prepare.foldLeft(Future.successful(List.empty[T])) {
prepare.foldLeft(Future.successful(List.newBuilder[T])) {
case (acc, prepare) =>
acc.flatMap { list =>
executeActionReturning(sql, prepare, extractor, column).map(list :+ _)
executeActionReturning(sql, prepare, extractor, column).map(list += _)
}
}
}.map(_.result())
}
}.map(_.flatten.toList)
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,25 @@ class FinagleMysqlContext[N <: NamingStrategy](
Future.collect {
groups.map {
case BatchGroup(sql, prepare) =>
prepare.foldLeft(Future.value(List.empty[Long])) {
prepare.foldLeft(Future.value(List.newBuilder[Long])) {
case (acc, prepare) =>
acc.flatMap { list =>
executeAction(sql, prepare).map(list :+ _)
executeAction(sql, prepare).map(list += _)
}
}
}.map(_.result())
}
}.map(_.flatten.toList)

def executeBatchActionReturning[T](groups: List[BatchGroupReturning], extractor: Extractor[T]): Future[List[T]] =
Future.collect {
groups.map {
case BatchGroupReturning(sql, column, prepare) =>
prepare.foldLeft(Future.value(List.empty[T])) {
prepare.foldLeft(Future.value(List.newBuilder[T])) {
case (acc, prepare) =>
acc.flatMap { list =>
executeActionReturning(sql, prepare, extractor, column).map(list :+ _)
executeActionReturning(sql, prepare, extractor, column).map(list += _)
}
}
}.map(_.result())
}
}.map(_.flatten.toList)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ class FinaglePostgresContext[N <: NamingStrategy](val naming: N, client: Postgre
def executeBatchAction[B](groups: List[BatchGroup]): Future[List[Long]] = Future.collect {
groups.map {
case BatchGroup(sql, prepare) =>
prepare.foldLeft(Future.value(List.empty[Long])) {
prepare.foldLeft(Future.value(List.newBuilder[Long])) {
case (acc, prepare) =>
acc.flatMap { list =>
executeAction(sql, prepare).map(list :+ _)
executeAction(sql, prepare).map(list += _)
}
}
}.map(_.result())
}
}.map(_.flatten.toList)

Expand All @@ -94,12 +94,12 @@ class FinaglePostgresContext[N <: NamingStrategy](val naming: N, client: Postgre
Future.collect {
groups.map {
case BatchGroupReturning(sql, column, prepare) =>
prepare.foldLeft(Future.value(List.empty[T])) {
prepare.foldLeft(Future.value(List.newBuilder[T])) {
case (acc, prepare) =>
acc.flatMap { list =>
executeActionReturning(sql, prepare, extractor, column).map(list :+ _)
executeActionReturning(sql, prepare, extractor, column).map(list += _)
}
}
}.map(_.result())
}
}.map(_.flatten.toList)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ trait QuillSparkContext
*/
private def perculateNullArrays[T: SparkEncoder](ds: Dataset[T]) = {
def nullifyNullArray(schema: StructType) = udf(
(row: Row) => if ((0 until row.length).map(row.isNullAt(_)).forall(n => n)) null else row,
(row: Row) => if ((0 until row.length).forall(row.isNullAt)) null else row,
schema
)

Expand Down

0 comments on commit 38d0565

Please sign in to comment.