typelevel / typelevel/skunk

quadratic (?) complexity when composing fragments

Open
#686 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.7k
Forks
175
Avg merge
5d 14h
Merged PRs (30d)
9

Description

Concatenating 10k Fragments 10 times is much faster than concatenating 100k fragments one time. This suggests a quadratic (or nonlinear) runtime complexity.

Reproduction in ammonite (Scala 2.13.8):

import $ivy.`org.tpolecat::skunk-core:0.2.3`; import skunk._; import skunk.implicits._

def measure(code: => Any) = {val start = System.nanoTime; code; println(f"${(System.nanoTime - start)/1000000000.0}%.2fs")}

measure { for(i <- 0 until 10) Array.fill(10000)(sql"x").foldLeft(sql"")((a,b) => sql"$a$b") }
// 18s
// is much faster than
measure { Array.fill(100000)(sql"x").foldLeft(sql"")((a,b) => sql"$a$b") }
// 213s

It could be caused by repeated concatenation of a growing List.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided Ammonite reproduction with skunk-core 0.2.3 first and compare the two concatenation patterns. Then locate the Fragment composition implementation and trace how the growing result is assembled. Done means identifying and addressing the nonlinear scaling while preserving the existing composition behavior, with measurements or regression coverage for large inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
databases, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.