From e3e345571da7fe2ea01e20b2921d0186c1ca7274 Mon Sep 17 00:00:00 2001 From: michikawa07 Date: Fri, 30 Sep 2022 11:00:03 +0900 Subject: [PATCH 1/6] Faster sortexp() --- src/display.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/display.jl b/src/display.jl index 97d82d6a..5a1873d3 100644 --- a/src/display.jl +++ b/src/display.jl @@ -170,7 +170,7 @@ function show(io::IO, x::Unitlike) showoperators = get(io, :showoperators, false) first = "" sep = showoperators ? "*" : " " - foreach(sortexp(typeof(x).parameters[1])) do y + foreach(sortexp(x)) do y print(io,first) showrep(io,y) first = sep @@ -182,10 +182,9 @@ end sortexp(xs) Sort units to show positive exponents first. """ -function sortexp(xs) - vcat([x for x in xs if power(x) >= 0], - [x for x in xs if power(x) < 0]) -end +sortexp(::Dimensions{D}) where D = sortexp(D) +sortexp(::Units{U}) where U = sortexp(U) +sortexp(xs)= sort!(collect(xs), by = u->power(u)>0 ? 1 : -1, rev=true) """ showrep(io::IO, x::Unit) From b981c0a372fb0c06146020d54536e4c111172b7a Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Mon, 31 Oct 2022 17:48:49 +0100 Subject: [PATCH 2/6] Move sortexp calculation to compile time --- src/display.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/display.jl b/src/display.jl index 5a1873d3..e69d03cf 100644 --- a/src/display.jl +++ b/src/display.jl @@ -182,9 +182,9 @@ end sortexp(xs) Sort units to show positive exponents first. """ -sortexp(::Dimensions{D}) where D = sortexp(D) -sortexp(::Units{U}) where U = sortexp(U) sortexp(xs)= sort!(collect(xs), by = u->power(u)>0 ? 1 : -1, rev=true) +@generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,) +@generated sortexp(::Units{U}) where U = (sortexp(U)...,) """ showrep(io::IO, x::Unit) From 6fbcac96f37a0e103975af52dae1ee00739e6d15 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Mon, 31 Oct 2022 19:05:34 +0100 Subject: [PATCH 3/6] Simplify implementation of sortexp --- src/display.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index e69d03cf..afb1c312 100644 --- a/src/display.jl +++ b/src/display.jl @@ -182,7 +182,7 @@ end sortexp(xs) Sort units to show positive exponents first. """ -sortexp(xs)= sort!(collect(xs), by = u->power(u)>0 ? 1 : -1, rev=true) +sortexp(xs)= sort!(collect(xs), by = u->power(u)<0) @generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,) @generated sortexp(::Units{U}) where U = (sortexp(U)...,) From 564a547c993f4cb2fe96d48c57b3d3e89691be4d Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Tue, 1 Nov 2022 11:23:28 +0100 Subject: [PATCH 4/6] Small optimization in sortexp --- src/display.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index afb1c312..72b8c4ec 100644 --- a/src/display.jl +++ b/src/display.jl @@ -182,7 +182,7 @@ end sortexp(xs) Sort units to show positive exponents first. """ -sortexp(xs)= sort!(collect(xs), by = u->power(u)<0) +sortexp(xs)= sort!(collect(xs), by = u->signbit(power(u))) @generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,) @generated sortexp(::Units{U}) where U = (sortexp(U)...,) From 09ecbc52c431f4e5801fcbcfc4c7665ab443f4cd Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:08:55 +0100 Subject: [PATCH 5/6] Whitespace --- src/display.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index 72b8c4ec..d4370cb0 100644 --- a/src/display.jl +++ b/src/display.jl @@ -182,7 +182,7 @@ end sortexp(xs) Sort units to show positive exponents first. """ -sortexp(xs)= sort!(collect(xs), by = u->signbit(power(u))) +sortexp(xs) = sort!(collect(xs), by = u->signbit(power(u))) @generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,) @generated sortexp(::Units{U}) where U = (sortexp(U)...,) From be808dc07386a545a5bb08db3c320e3f97a8d52e Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Thu, 8 Dec 2022 09:26:12 +0100 Subject: [PATCH 6/6] Use `InsertionSort` in `sortexp` Since the length of `xs` is typically very small, `InsertionSort` is faster --- src/display.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index d4370cb0..032c86b1 100644 --- a/src/display.jl +++ b/src/display.jl @@ -182,7 +182,7 @@ end sortexp(xs) Sort units to show positive exponents first. """ -sortexp(xs) = sort!(collect(xs), by = u->signbit(power(u))) +sortexp(xs) = sort!(collect(xs), by = u->signbit(power(u)), alg = InsertionSort) @generated sortexp(::Dimensions{D}) where D = (sortexp(D)...,) @generated sortexp(::Units{U}) where U = (sortexp(U)...,)