chore: remove usages of sort.Sort (#6689)

improve language stats rounding:

- Add tests (I had to omit some edge cases as the current method is
non-determistic in some cases, due to random order of map access).
- Document the algorithm used.
- Lower the amount of calculations that need to be done.
- Because of the aforementioned non-determistic don't use stable sort
and instead regular sort.

better sorting for `RepositoryList`:

- Add testing
- Use `slices.Sortfunc` instead of `sort.Sort`.
- Remove the methods needed for `sort.Sort`.

better git tag sorter:

- Use `slices.SortFunc` instead of `sort.Sort`.
- Remove `tagSorter` and its related methods.
- Added testing.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6689
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-01-26 13:30:00 +00:00 committed by Gusted
parent 048af05db9
commit 270a2c7fa3
8 changed files with 97 additions and 51 deletions

View file

@ -4,9 +4,10 @@
package repo
import (
"cmp"
"context"
"math"
"sort"
"slices"
"strings"
"code.gitea.io/gitea/models/db"
@ -67,34 +68,37 @@ func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
return langPerc
}
// Rounds to 1 decimal point, target should be the expected sum of percs
// Use the quota method to round the percentages to one decimal place while
// keeping the sum of the percentages at 100%.
func roundByLargestRemainder(percs map[string]float32, target float32) {
// Tracks the difference between the sum of percentage and 100%.
leftToDistribute := int(target * 10)
keys := make([]string, 0, len(percs))
type key struct {
language string
remainder float64
}
keys := make([]key, 0, len(percs))
for k, v := range percs {
percs[k] = v * 10
floored := math.Floor(float64(percs[k]))
floored, frac := math.Modf(float64(v * 10))
percs[k] = float32(floored)
leftToDistribute -= int(floored)
keys = append(keys, k)
keys = append(keys, key{language: k, remainder: frac})
}
// Sort the keys by the largest remainder
sort.SliceStable(keys, func(i, j int) bool {
_, remainderI := math.Modf(float64(percs[keys[i]]))
_, remainderJ := math.Modf(float64(percs[keys[j]]))
return remainderI > remainderJ
// Sort the fractional part in an ascending order.
slices.SortFunc(keys, func(b, a key) int {
return cmp.Compare(a.remainder, b.remainder)
})
// Increment the values in order of largest remainder
// As long as the sum of 100% is not reached, add 0.1% percentage.
for _, k := range keys {
percs[k] = float32(math.Floor(float64(percs[k])))
if leftToDistribute > 0 {
percs[k]++
percs[k.language]++
leftToDistribute--
}
percs[k] /= 10
percs[k.language] /= 10
}
}

View file

@ -0,0 +1,66 @@
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package repo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLanguagePercentages(t *testing.T) {
testCases := []struct {
input LanguageStatList
output map[string]float32
}{
{
[]*LanguageStat{{Language: "Go", Size: 500}, {Language: "Rust", Size: 501}},
map[string]float32{
"Go": 50.0,
"Rust": 50.0,
},
},
{
[]*LanguageStat{{Language: "Go", Size: 10}, {Language: "Rust", Size: 91}},
map[string]float32{
"Go": 9.9,
"Rust": 90.1,
},
},
{
[]*LanguageStat{{Language: "Go", Size: 1}, {Language: "Rust", Size: 2}},
map[string]float32{
"Go": 33.3,
"Rust": 66.7,
},
},
{
[]*LanguageStat{{Language: "Go", Size: 1}, {Language: "Rust", Size: 2}, {Language: "Shell", Size: 3}, {Language: "C#", Size: 4}, {Language: "Zig", Size: 5}, {Language: "Coq", Size: 6}, {Language: "Haskell", Size: 7}},
map[string]float32{
"Go": 3.6,
"Rust": 7.1,
"Shell": 10.7,
"C#": 14.3,
"Zig": 17.9,
"Coq": 21.4,
"Haskell": 25,
},
},
{
[]*LanguageStat{{Language: "Go", Size: 1000}, {Language: "PHP", Size: 1}, {Language: "Java", Size: 1}},
map[string]float32{
"Go": 99.8,
"other": 0.2,
},
},
{
[]*LanguageStat{},
map[string]float32{},
},
}
for _, testCase := range testCases {
assert.Equal(t, testCase.output, testCase.input.getLanguagePercentages())
}
}

View file

@ -36,18 +36,6 @@ const RepositoryListDefaultPageSize = 64
// RepositoryList contains a list of repositories
type RepositoryList []*Repository
func (repos RepositoryList) Len() int {
return len(repos)
}
func (repos RepositoryList) Less(i, j int) bool {
return repos[i].FullName() < repos[j].FullName()
}
func (repos RepositoryList) Swap(i, j int) {
repos[i], repos[j] = repos[j], repos[i]
}
// ValuesRepository converts a repository map to a list
// FIXME: Remove in favor of maps.values when MIN_GO_VERSION >= 1.18
func ValuesRepository(m map[int64]*Repository) []*Repository {