From 4f69778319df2dcbd5ade38ea250d011466964d7 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Thu, 30 Nov 2023 07:04:15 +0000 Subject: [PATCH] Revert "Replace x/slices with standard slices pkg (#2193)" This reverts commit 594b23b3f79647bf181e1a29ed98c013a343e636. --- go.mod | 27 +- go.sum | 38 +- hack/go.mod.default | 2 +- hack/tools/metrics/main.go | 7 +- internal/file/file.go | 5 +- internal/info/info.go | 2 +- internal/net/grpc/pool/pool.go | 2 +- internal/net/grpc/stream.go | 15 +- internal/slices/slices.go | 46 +++ internal/slices/slices_test.go | 368 ++++++++++++++++++ pkg/agent/core/ngt/service/ngt.go | 2 +- pkg/agent/core/ngt/service/vqueue/queue.go | 13 +- pkg/discoverer/k8s/service/discover.go | 27 +- pkg/gateway/filter/usecase/vald.go | 2 +- pkg/gateway/lb/handler/grpc/aggregation.go | 20 +- pkg/gateway/lb/handler/grpc/handler.go | 2 +- .../lb/handler/grpc/search_benchmark_test.go | 7 +- 17 files changed, 490 insertions(+), 95 deletions(-) create mode 100644 internal/slices/slices.go create mode 100644 internal/slices/slices_test.go diff --git a/go.mod b/go.mod index 903703fb21..a88d8c06c9 100755 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ replace ( cloud.google.com/go/bigquery => cloud.google.com/go/bigquery v1.55.0 cloud.google.com/go/compute => cloud.google.com/go/compute v1.23.0 cloud.google.com/go/datastore => cloud.google.com/go/datastore v1.14.0 - cloud.google.com/go/firestore => cloud.google.com/go/firestore v1.13.0 + cloud.google.com/go/firestore => cloud.google.com/go/firestore v1.12.0 cloud.google.com/go/iam => cloud.google.com/go/iam v1.1.2 cloud.google.com/go/kms => cloud.google.com/go/kms v1.15.2 cloud.google.com/go/monitoring => cloud.google.com/go/monitoring v1.16.0 @@ -38,18 +38,18 @@ replace ( github.com/DATA-DOG/go-sqlmock => github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/GoogleCloudPlatform/cloudsql-proxy => github.com/GoogleCloudPlatform/cloudsql-proxy v1.33.10 github.com/Masterminds/semver/v3 => github.com/Masterminds/semver/v3 v3.2.1 - github.com/ajstarks/deck => github.com/ajstarks/deck v0.0.0-20230917174535-ca5324ee72b2 - github.com/ajstarks/deck/generate => github.com/ajstarks/deck/generate v0.0.0-20230917174535-ca5324ee72b2 + github.com/ajstarks/deck => github.com/ajstarks/deck v0.0.0-20230713212537-3862a8c503c2 + github.com/ajstarks/deck/generate => github.com/ajstarks/deck/generate v0.0.0-20230713212537-3862a8c503c2 github.com/ajstarks/svgo => github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b github.com/antihax/optional => github.com/antihax/optional v1.0.0 github.com/armon/go-socks5 => github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 - github.com/aws/aws-sdk-go => github.com/aws/aws-sdk-go v1.45.12 + github.com/aws/aws-sdk-go => github.com/aws/aws-sdk-go v1.45.10 github.com/aws/aws-sdk-go-v2 => github.com/aws/aws-sdk-go-v2 v1.21.0 github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream => github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 - github.com/aws/aws-sdk-go-v2/config => github.com/aws/aws-sdk-go-v2/config v1.18.40 - github.com/aws/aws-sdk-go-v2/credentials => github.com/aws/aws-sdk-go-v2/credentials v1.13.38 + github.com/aws/aws-sdk-go-v2/config => github.com/aws/aws-sdk-go-v2/config v1.18.39 + github.com/aws/aws-sdk-go-v2/credentials => github.com/aws/aws-sdk-go-v2/credentials v1.13.37 github.com/aws/aws-sdk-go-v2/feature/ec2/imds => github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 - github.com/aws/aws-sdk-go-v2/feature/s3/manager => github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.84 + github.com/aws/aws-sdk-go-v2/feature/s3/manager => github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.83 github.com/aws/aws-sdk-go-v2/internal/configsources => github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 => github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 github.com/aws/aws-sdk-go-v2/internal/ini => github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42 @@ -60,11 +60,11 @@ replace ( github.com/aws/aws-sdk-go-v2/service/kms => github.com/aws/aws-sdk-go-v2/service/kms v1.24.5 github.com/aws/aws-sdk-go-v2/service/s3 => github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5 github.com/aws/aws-sdk-go-v2/service/secretsmanager => github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.21.3 - github.com/aws/aws-sdk-go-v2/service/sns => github.com/aws/aws-sdk-go-v2/service/sns v1.22.0 + github.com/aws/aws-sdk-go-v2/service/sns => github.com/aws/aws-sdk-go-v2/service/sns v1.21.5 github.com/aws/aws-sdk-go-v2/service/sqs => github.com/aws/aws-sdk-go-v2/service/sqs v1.24.5 github.com/aws/aws-sdk-go-v2/service/ssm => github.com/aws/aws-sdk-go-v2/service/ssm v1.37.5 - github.com/aws/aws-sdk-go-v2/service/sso => github.com/aws/aws-sdk-go-v2/service/sso v1.14.0 - github.com/aws/aws-sdk-go-v2/service/sts => github.com/aws/aws-sdk-go-v2/service/sts v1.22.0 + github.com/aws/aws-sdk-go-v2/service/sso => github.com/aws/aws-sdk-go-v2/service/sso v1.13.6 + github.com/aws/aws-sdk-go-v2/service/sts => github.com/aws/aws-sdk-go-v2/service/sts v1.21.5 github.com/aws/smithy-go => github.com/aws/smithy-go v1.14.2 github.com/benbjohnson/clock => github.com/benbjohnson/clock v1.3.5 github.com/beorn7/perks => github.com/beorn7/perks v1.0.1 @@ -190,7 +190,7 @@ replace ( github.com/jstemmer/go-junit-report => github.com/jstemmer/go-junit-report v1.0.0 github.com/kisielk/errcheck => github.com/kisielk/errcheck v1.6.3 github.com/kisielk/gotool => github.com/kisielk/gotool v1.0.0 - github.com/klauspost/compress => github.com/klauspost/compress v1.17.1-0.20230918170125-6125c11bae77 + github.com/klauspost/compress => github.com/klauspost/compress v1.16.8-0.20230909131757-0e8837ceede6 github.com/klauspost/cpuid/v2 => github.com/klauspost/cpuid/v2 v2.2.5 github.com/kpango/fastime => github.com/kpango/fastime v1.1.9 github.com/kpango/fuid => github.com/kpango/fuid v0.0.0-20221203053508-503b5ad89aa1 @@ -292,7 +292,7 @@ replace ( go.uber.org/zap => go.uber.org/zap v1.26.0 gocloud.dev => gocloud.dev v0.34.0 golang.org/x/crypto => golang.org/x/crypto v0.13.0 - golang.org/x/exp => golang.org/x/exp v0.0.0-20230905200255-921286631fa9 + golang.org/x/exp => golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb golang.org/x/exp/typeparams => golang.org/x/exp/typeparams v0.0.0-20230905200255-921286631fa9 golang.org/x/image => golang.org/x/image v0.12.0 golang.org/x/lint => golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 @@ -388,6 +388,7 @@ require ( go.uber.org/goleak v1.2.1 go.uber.org/zap v1.24.0 gocloud.dev v0.0.0-00010101000000-000000000000 + golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b golang.org/x/net v0.15.0 golang.org/x/oauth2 v0.12.0 golang.org/x/sync v0.3.0 @@ -435,7 +436,7 @@ require ( github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.1 // indirect github.com/go-openapi/swag v0.22.4 // indirect - github.com/go-pdf/fpdf v0.9.0 // indirect + github.com/go-pdf/fpdf v0.8.0 // indirect github.com/go-toolsmith/astcopy v1.0.2 // indirect github.com/go-toolsmith/astequal v1.1.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect diff --git a/go.sum b/go.sum index 2f4fdb2dcc..fde8f3fbc5 100644 --- a/go.sum +++ b/go.sum @@ -55,7 +55,7 @@ cloud.google.com/go/errorreporting v0.3.0/go.mod h1:xsP2yaAp+OAW4OIm60An2bbLpqIh cloud.google.com/go/essentialcontacts v1.6.2/go.mod h1:T2tB6tX+TRak7i88Fb2N9Ok3PvY3UNbUsMag9/BARh4= cloud.google.com/go/eventarc v1.13.0/go.mod h1:mAFCW6lukH5+IZjkvrEss+jmt2kOdYlN8aMx3sRJiAI= cloud.google.com/go/filestore v1.7.1/go.mod h1:y10jsorq40JJnjR/lQ8AfFbbcGlw3g+Dp8oN7i7FjV4= -cloud.google.com/go/firestore v1.13.0/go.mod h1:QojqqOh8IntInDUSTAh0c8ZsPYAr68Ma8c5DWOy8xb8= +cloud.google.com/go/firestore v1.12.0/go.mod h1:b38dKhgzlmNNGTNZZwe7ZRFEuRab1Hay3/DBsIGKKy4= cloud.google.com/go/functions v1.15.1/go.mod h1:P5yNWUTkyU+LvW/S9O6V+V423VZooALQlqoXdoPz5AE= cloud.google.com/go/gkebackup v1.3.0/go.mod h1:vUDOu++N0U5qs4IhG1pcOnD1Mac79xWy6GoBFlWCWBU= cloud.google.com/go/gkeconnect v0.8.1/go.mod h1:KWiK1g9sDLZqhxB2xEuPV8V9NYzrqTUmQR9shJHpOZw= @@ -161,8 +161,8 @@ github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJ github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= -github.com/ajstarks/deck v0.0.0-20230917174535-ca5324ee72b2/go.mod h1:kSGTjaazWvDqs9hZd/UJu5WmdGOkYCDzQYOgQEa+v9Y= -github.com/ajstarks/deck/generate v0.0.0-20230917174535-ca5324ee72b2/go.mod h1:u04DhpZIpzaPnAUmhhjibCj450/2ITtLTEnvhaM49as= +github.com/ajstarks/deck v0.0.0-20230713212537-3862a8c503c2/go.mod h1:Nd40ZJeNA/I7Wu/RloLtis/eRLHLR/Q9YhYrDZndIeg= +github.com/ajstarks/deck/generate v0.0.0-20230713212537-3862a8c503c2/go.mod h1:u04DhpZIpzaPnAUmhhjibCj450/2ITtLTEnvhaM49as= github.com/ajstarks/fc v0.0.0-20230606144319-ef5d5cb73a3d/go.mod h1:Qp3TfzbBiIjHwDxIpu+g9nYfNw+xXF2Yqp4WmMlTtwM= github.com/ajstarks/openvg v0.0.0-20191008131700-c6885d824eb8/go.mod h1:jpZHIkd4sQEgrzshrUQrRfv5OUMMq0w/Q1yK6ZYhUlk= github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw= @@ -176,20 +176,20 @@ github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/P github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/aws/aws-sdk-go v1.45.12 h1:+bKbbesGNPp+TeGrcqfrWuZoqcIEhjwKyBMHQPp80Jo= -github.com/aws/aws-sdk-go v1.45.12/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= +github.com/aws/aws-sdk-go v1.45.10 h1:GoqAm25t0qrs4rrXAjqt3luZnM9mV0lzfNwzgaCKpm4= +github.com/aws/aws-sdk-go v1.45.10/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go-v2 v1.21.0 h1:gMT0IW+03wtYJhRqTVYn0wLzwdnK9sRMcxmtfGzRdJc= github.com/aws/aws-sdk-go-v2 v1.21.0/go.mod h1:/RfNgGmRxI+iFOB1OeJUyxiU+9s88k3pfHvDagGEp0M= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 h1:OPLEkmhXf6xFPiz0bLeDArZIDx1NNS4oJyG4nv3Gct0= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13/go.mod h1:gpAbvyDGQFozTEmlTFO8XcQKHzubdq0LzRyJpG6MiXM= -github.com/aws/aws-sdk-go-v2/config v1.18.40 h1:dbu1llI/nTIL+r6sYHMeVLl99DM8J8/o1I4EPurnhLg= -github.com/aws/aws-sdk-go-v2/config v1.18.40/go.mod h1:JjrCZQwSPGCoZRQzKHyZNNueaKO+kFaEy2sR6mCzd90= -github.com/aws/aws-sdk-go-v2/credentials v1.13.38 h1:gDAuCdVlA4lmmgQhvpZlscwicloCqH44vkxLklGkQLA= -github.com/aws/aws-sdk-go-v2/credentials v1.13.38/go.mod h1:sD4G/Ybgp6s89mWIES3Xn97CsRLpxvz9uVSdv0UxY8I= +github.com/aws/aws-sdk-go-v2/config v1.18.39 h1:oPVyh6fuu/u4OiW4qcuQyEtk7U7uuNBmHmJSLg1AJsQ= +github.com/aws/aws-sdk-go-v2/config v1.18.39/go.mod h1:+NH/ZigdPckFpgB1TRcRuWCB/Kbbvkxc/iNAKTq5RhE= +github.com/aws/aws-sdk-go-v2/credentials v1.13.37 h1:BvEdm09+ZEh2XtN+PVHPcYwKY3wIeB6pw7vPRM4M9/U= +github.com/aws/aws-sdk-go-v2/credentials v1.13.37/go.mod h1:ACLrdkd4CLZyXOghZ8IYumQbcooAcp2jo/s2xsFH8IM= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 h1:uDZJF1hu0EVT/4bogChk8DyjSF6fof6uL/0Y26Ma7Fg= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11/go.mod h1:TEPP4tENqBGO99KwVpV9MlOX4NSrSLP8u3KRy2CDwA8= -github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.84 h1:LENrVcqnWTyI8fbIUCvxAMe+fXbREIaXzcR8WPwco1U= -github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.84/go.mod h1:LHxCiYAStsgps4srke7HujyADd504MSkNXjLpOtICTc= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.83 h1:wcluDLIQ0uYaxv0fCWQRimbXkPdTgWHUD21j1CzXEwc= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.83/go.mod h1:nGCBuon134gW67yAtxHKV73x+tAcY/xG4ZPNPDB1h/I= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 h1:22dGT7PneFMx4+b3pz7lMTRyN8ZKH7M2cW4GP9yUS2g= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41/go.mod h1:CrObHAuPneJBlfEJ5T3szXOUkLEThaGfvnhTf33buas= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 h1:SijA0mgjV8E+8G45ltVHs0fvKpTj8xmZJ3VwhGKtUSI= @@ -208,12 +208,12 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4 h1:v0jkRigbSD6uOd github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.15.4/go.mod h1:LhTyt8J04LL+9cIt7pYJ5lbS/U98ZmXovLOR/4LUsk8= github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5 h1:A42xdtStObqy7NGvzZKpnyNXvoOmm+FENobZ0/ssHWk= github.com/aws/aws-sdk-go-v2/service/s3 v1.38.5/go.mod h1:rDGMZA7f4pbmTtPOk5v5UM2lmX6UAbRnMDJeDvnH7AM= -github.com/aws/aws-sdk-go-v2/service/sso v1.14.0 h1:AR/hlTsCyk1CwlyKnPFvIMvnONydRjDDRT9OGb0i+/g= -github.com/aws/aws-sdk-go-v2/service/sso v1.14.0/go.mod h1:fIAwKQKBFu90pBxx07BFOMJLpRUGu8VOzLJakeY+0K4= +github.com/aws/aws-sdk-go-v2/service/sso v1.13.6 h1:2PylFCfKCEDv6PeSN09pC/VUiRd10wi1VfHG5FrW0/g= +github.com/aws/aws-sdk-go-v2/service/sso v1.13.6/go.mod h1:fIAwKQKBFu90pBxx07BFOMJLpRUGu8VOzLJakeY+0K4= github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.1 h1:hd0SKLMdOL/Sl6Z0np1PX9LeH2gqNtBe0MhTedA8MGI= github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.1/go.mod h1:XO/VcyoQ8nKyKfFW/3DMsRQXsfh/052tHTWmg3xBXRg= -github.com/aws/aws-sdk-go-v2/service/sts v1.22.0 h1:s4bioTgjSFRwOoyEFzAVCmFmoowBgjTR8gkrF/sQ4wk= -github.com/aws/aws-sdk-go-v2/service/sts v1.22.0/go.mod h1:VC7JDqsqiwXukYEDjoHh9U0fOJtNWh04FPQz4ct4GGU= +github.com/aws/aws-sdk-go-v2/service/sts v1.21.5 h1:CQBFElb0LS8RojMJlxRSo/HXipvTZW2S44Lt9Mk2aYQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.21.5/go.mod h1:VC7JDqsqiwXukYEDjoHh9U0fOJtNWh04FPQz4ct4GGU= github.com/aws/smithy-go v1.14.2 h1:MJU9hqBGbvWZdApzpvoF2WAIJDbtjK2NDJSiJP7HblQ= github.com/aws/smithy-go v1.14.2/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -425,8 +425,8 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C github.com/kisielk/errcheck v1.6.3/go.mod h1:nXw/i/MfnvRHqXa7XXmQMUB0oNFGuBrNI8d8NLy0LPw= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE= -github.com/klauspost/compress v1.17.1-0.20230918170125-6125c11bae77 h1:02kW20OlAeZhbVue+Xriy4NWy9tskroqvX0PW1v/ACA= -github.com/klauspost/compress v1.17.1-0.20230918170125-6125c11bae77/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/compress v1.16.8-0.20230909131757-0e8837ceede6 h1:tAUcIfGiK54W2Qa/pUncetfyVsVSvAGorsyGZkuqn10= +github.com/klauspost/compress v1.16.8-0.20230909131757-0e8837ceede6/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/kpango/fastime v1.1.9 h1:xVQHcqyPt5M69DyFH7g1EPRns1YQNap9d5eLhl/Jy84= @@ -612,8 +612,8 @@ gocloud.dev v0.34.0 h1:LzlQY+4l2cMtuNfwT2ht4+fiXwWf/NmPTnXUlLmGif4= gocloud.dev v0.34.0/go.mod h1:psKOachbnvY3DAOPbsFVmLIErwsbWPUG2H5i65D38vE= golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb h1:xIApU0ow1zwMa2uL1VDNeQlNVFTWMQxZUZCMDy0Q4Us= +golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/exp/shiny v0.0.0-20220827204233-334a2380cb91/go.mod h1:VjAR7z0ngyATZTELrBSkxOOHhhlnVUxDye4mcjx5h/8= golang.org/x/exp/shiny v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0= golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0= diff --git a/hack/go.mod.default b/hack/go.mod.default index fa261d6dcb..3fd1767d19 100755 --- a/hack/go.mod.default +++ b/hack/go.mod.default @@ -292,7 +292,7 @@ replace ( go.uber.org/zap => go.uber.org/zap latest gocloud.dev => gocloud.dev latest golang.org/x/crypto => golang.org/x/crypto latest - golang.org/x/exp => golang.org/x/exp latest + golang.org/x/exp => golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb golang.org/x/exp/typeparams => golang.org/x/exp/typeparams latest golang.org/x/image => golang.org/x/image latest golang.org/x/lint => golang.org/x/lint latest diff --git a/hack/tools/metrics/main.go b/hack/tools/metrics/main.go index c2d1bad960..e25c167ada 100644 --- a/hack/tools/metrics/main.go +++ b/hack/tools/metrics/main.go @@ -14,18 +14,17 @@ package main import ( - "cmp" "encoding/gob" "flag" "fmt" "image/color" "io/fs" "os" - "slices" "github.com/lucasb-eyer/go-colorful" "github.com/vdaas/vald/hack/benchmark/metrics" "github.com/vdaas/vald/internal/log" + "github.com/vdaas/vald/internal/slices" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/vg" @@ -88,8 +87,8 @@ func run() error { step = 1 / float64(len(ms)-1) } for i, m := range ms { - slices.SortFunc(m.Search, func(left, right *metrics.SearchMetrics) int { - return cmp.Compare(left.Recall, right.Recall) + slices.SortFunc(m.Search, func(left, right *metrics.SearchMetrics) bool { + return left.Recall < right.Recall }) xys := make(plotter.XYs, len(m.Search)) for i, s := range m.Search { diff --git a/internal/file/file.go b/internal/file/file.go index deee2ad8f7..1f565a62cc 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -18,12 +18,10 @@ package file import ( - "cmp" "context" "io/fs" "os" "path/filepath" - "slices" "strconv" "time" @@ -32,6 +30,7 @@ import ( "github.com/vdaas/vald/internal/io" "github.com/vdaas/vald/internal/log" "github.com/vdaas/vald/internal/safety" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/strings" "github.com/vdaas/vald/internal/sync/errgroup" ) @@ -339,7 +338,7 @@ func ReadDir(path string) (dirs []fs.DirEntry, err error) { }() dirs, err = f.ReadDir(-1) - slices.SortFunc(dirs, func(a, b fs.DirEntry) int { return cmp.Compare(a.Name(), b.Name()) }) + slices.SortFunc(dirs, func(a, b fs.DirEntry) bool { return a.Name() < b.Name() }) return dirs, err } diff --git a/internal/info/info.go b/internal/info/info.go index c375145f50..b948ac8905 100644 --- a/internal/info/info.go +++ b/internal/info/info.go @@ -21,11 +21,11 @@ import ( "fmt" "reflect" "runtime" - "slices" "strconv" "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/log" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/strings" "github.com/vdaas/vald/internal/sync" ) diff --git a/internal/net/grpc/pool/pool.go b/internal/net/grpc/pool/pool.go index 8f195bc2c1..6be2b9373b 100644 --- a/internal/net/grpc/pool/pool.go +++ b/internal/net/grpc/pool/pool.go @@ -21,7 +21,6 @@ import ( "context" "fmt" "math" - "slices" "strconv" "sync/atomic" "time" @@ -31,6 +30,7 @@ import ( "github.com/vdaas/vald/internal/log" "github.com/vdaas/vald/internal/net" "github.com/vdaas/vald/internal/safety" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/strings" "github.com/vdaas/vald/internal/sync" "github.com/vdaas/vald/internal/sync/errgroup" diff --git a/internal/net/grpc/stream.go b/internal/net/grpc/stream.go index 4dc6e56de3..0efa399293 100644 --- a/internal/net/grpc/stream.go +++ b/internal/net/grpc/stream.go @@ -18,11 +18,9 @@ package grpc import ( - "cmp" "context" "fmt" "runtime" - "slices" "sync/atomic" "github.com/vdaas/vald/internal/errors" @@ -33,6 +31,7 @@ import ( "github.com/vdaas/vald/internal/net/grpc/status" "github.com/vdaas/vald/internal/observability/trace" "github.com/vdaas/vald/internal/safety" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/sync" "github.com/vdaas/vald/internal/sync/errgroup" "google.golang.org/grpc" @@ -75,8 +74,8 @@ func BidirectionalStream[Q any, R any](ctx context.Context, stream ServerStream, errs = append(errs, err) emu.Unlock() } - removeDuplicates(errs, func(left, right error) int { - return cmp.Compare(left.Error(), right.Error()) + slices.RemoveDuplicates(errs, func(left, right error) bool { + return left.Error() < right.Error() }) emu.Lock() err = errors.Join(errs...) @@ -229,11 +228,3 @@ func BidirectionalStreamClient(stream ClientStream, } }() } - -func removeDuplicates[S ~[]E, E comparable](x S, less func(left, right E) int) S { - if len(x) < 2 { - return x - } - slices.SortStableFunc(x, less) - return slices.Compact(x) -} diff --git a/internal/slices/slices.go b/internal/slices/slices.go new file mode 100644 index 0000000000..82e87e0731 --- /dev/null +++ b/internal/slices/slices.go @@ -0,0 +1,46 @@ +// Copyright (C) 2019-2023 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package slices + +import ( + "golang.org/x/exp/constraints" + "golang.org/x/exp/slices" +) + +func Sort[E constraints.Ordered](x []E) { + slices.Sort(x) +} + +func SortFunc[E any](x []E, less func(left, right E) bool) { + slices.SortFunc(x, less) +} + +func SortStableFunc[E any](x []E, less func(left, right E) bool) { + slices.SortStableFunc(x, less) +} + +func RemoveDuplicates[E comparable](x []E, less func(left, right E) bool) []E { + if len(x) < 2 { + return x + } + SortStableFunc(x, less) + up := 0 // uniqPointer + for i := 1; i < len(x); i++ { + if x[up] != x[i] { + up++ + x[up] = x[i] + } + } + return x[:up+1] +} diff --git a/internal/slices/slices_test.go b/internal/slices/slices_test.go new file mode 100644 index 0000000000..9791cc341f --- /dev/null +++ b/internal/slices/slices_test.go @@ -0,0 +1,368 @@ +// Copyright (C) 2019-2023 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package slices + +import ( + "reflect" + "testing" + + "github.com/vdaas/vald/internal/errors" + "github.com/vdaas/vald/internal/test/goleak" +) + +func TestSort(t *testing.T) { + type args struct { + x []int + } + type want struct { + want []int + } + type test struct { + name string + args args + want want + checkFunc func([]int, want) error + beforeFunc func(*testing.T, args) + afterFunc func(*testing.T, args) + } + defaultCheckFunc := func(got []int, w want) error { + if len(got) != len(w.want) { + return errors.New("len not match") + } + for i := 0; i < len(got); i++ { + if got[i] != w.want[i] { + return errors.New("slice not sorted") + } + } + return nil + } + tests := []test{ + { + name: "success to sort 10 elements", + args: args{ + x: []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + }, + want: want{ + want: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + }, + }, + { + name: "success to sort 1 elements", + args: args{ + x: []int{0}, + }, + want: want{ + want: []int{0}, + }, + }, + { + name: "success to sort 0 elements", + args: args{ + x: []int{}, + }, + want: want{ + want: []int{}, + }, + }, + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(tt, test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(tt, test.args) + } + checkFunc := test.checkFunc + if test.checkFunc == nil { + checkFunc = defaultCheckFunc + } + + Sort(test.args.x) + if err := checkFunc(test.args.x, test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestSortFunc(t *testing.T) { + type args struct { + x []int + less func(left, right int) bool + } + type want struct { + want []int + } + type test struct { + name string + args args + want want + checkFunc func([]int, want) error + beforeFunc func(*testing.T, args) + afterFunc func(*testing.T, args) + } + + defaultLessFn := func(l, r int) bool { + return l < r + } + defaultCheckFunc := func(got []int, w want) error { + if len(got) != len(w.want) { + return errors.New("len not match") + } + for i := 0; i < len(got); i++ { + if got[i] != w.want[i] { + return errors.New("slice not sorted") + } + } + return nil + } + tests := []test{ + { + name: "success to sort 10 elements", + args: args{ + x: []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + less: defaultLessFn, + }, + want: want{ + want: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + }, + }, + { + name: "success to sort 1 elements", + args: args{ + x: []int{0}, + less: defaultLessFn, + }, + want: want{ + want: []int{0}, + }, + }, + { + name: "success to sort 0 elements", + args: args{ + x: []int{}, + less: defaultLessFn, + }, + want: want{ + want: []int{}, + }, + }, + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(tt, test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(tt, test.args) + } + checkFunc := test.checkFunc + if test.checkFunc == nil { + checkFunc = defaultCheckFunc + } + + SortFunc(test.args.x, test.args.less) + if err := checkFunc(test.args.x, test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestSortStableFunc(t *testing.T) { + type args struct { + x []int + less func(left, right int) bool + } + type want struct { + want []int + } + type test struct { + name string + args args + want want + checkFunc func([]int, want) error + beforeFunc func(*testing.T, args) + afterFunc func(*testing.T, args) + } + + defaultLessFn := func(l, r int) bool { + return l < r + } + defaultCheckFunc := func(got []int, w want) error { + if len(got) != len(w.want) { + return errors.New("len not match") + } + for i := 0; i < len(got); i++ { + if got[i] != w.want[i] { + return errors.New("slice not sorted") + } + } + return nil + } + tests := []test{ + { + name: "success to sort 10 elements", + args: args{ + x: []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + less: defaultLessFn, + }, + want: want{ + want: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + }, + }, + { + name: "success to sort 1 elements", + args: args{ + x: []int{0}, + less: defaultLessFn, + }, + want: want{ + want: []int{0}, + }, + }, + { + name: "success to sort 0 elements", + args: args{ + x: []int{}, + less: defaultLessFn, + }, + want: want{ + want: []int{}, + }, + }, + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(tt, test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(tt, test.args) + } + checkFunc := test.checkFunc + if test.checkFunc == nil { + checkFunc = defaultCheckFunc + } + + SortStableFunc(test.args.x, test.args.less) + if err := checkFunc(test.args.x, test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestRemoveDuplicates(t *testing.T) { + type args struct { + x []int + less func(left, right int) bool + } + type want struct { + want []int + } + type test struct { + name string + args args + want want + checkFunc func(want, []int) error + beforeFunc func(*testing.T, args) + afterFunc func(*testing.T, args) + } + defaultCheckFunc := func(w want, got []int) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + x:nil, + less:nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + beforeFunc: func(t *testing.T, args args) { + t.Helper() + }, + afterFunc: func(t *testing.T, args args) { + t.Helper() + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + x:nil, + less:nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + beforeFunc: func(t *testing.T, args args) { + t.Helper() + }, + afterFunc: func(t *testing.T, args args) { + t.Helper() + }, + } + }(), + */ + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) + if test.beforeFunc != nil { + test.beforeFunc(tt, test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(tt, test.args) + } + checkFunc := test.checkFunc + if test.checkFunc == nil { + checkFunc = defaultCheckFunc + } + + got := RemoveDuplicates(test.args.x, test.args.less) + if err := checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +// NOT IMPLEMENTED BELOW diff --git a/pkg/agent/core/ngt/service/ngt.go b/pkg/agent/core/ngt/service/ngt.go index 3b598ceb7e..1e1a75d291 100644 --- a/pkg/agent/core/ngt/service/ngt.go +++ b/pkg/agent/core/ngt/service/ngt.go @@ -27,7 +27,6 @@ import ( "path/filepath" "reflect" "runtime" - "slices" "sync/atomic" "time" @@ -40,6 +39,7 @@ import ( "github.com/vdaas/vald/internal/log" "github.com/vdaas/vald/internal/observability/trace" "github.com/vdaas/vald/internal/safety" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/strings" "github.com/vdaas/vald/internal/sync" "github.com/vdaas/vald/internal/sync/errgroup" diff --git a/pkg/agent/core/ngt/service/vqueue/queue.go b/pkg/agent/core/ngt/service/vqueue/queue.go index 821d77ce35..5d97511f6c 100644 --- a/pkg/agent/core/ngt/service/vqueue/queue.go +++ b/pkg/agent/core/ngt/service/vqueue/queue.go @@ -18,15 +18,14 @@ package vqueue import ( - "cmp" "context" "reflect" - "slices" "sync/atomic" "time" "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/log" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/sync" ) @@ -198,8 +197,9 @@ func (v *vqueue) RangePopInsert(ctx context.Context, now int64, f func(uuid stri } return true }) - slices.SortFunc(uii, func(left, right index) int { - return cmp.Compare(right.date, left.date) + slices.SortFunc(uii, func(left, right index) bool { + // sort by latest unix time order + return left.date > right.date }) for _, idx := range uii { if !f(idx.uuid, idx.vector, idx.date) { @@ -232,8 +232,9 @@ func (v *vqueue) RangePopDelete(ctx context.Context, now int64, f func(uuid stri } return true }) - slices.SortFunc(udi, func(left, right index) int { - return cmp.Compare(right.date, left.date) + slices.SortFunc(udi, func(left, right index) bool { + // sort by latest unix time order + return left.date > right.date }) for _, idx := range udi { if !f(idx.uuid) { diff --git a/pkg/discoverer/k8s/service/discover.go b/pkg/discoverer/k8s/service/discover.go index 9a2773c431..3a7d04ed6f 100644 --- a/pkg/discoverer/k8s/service/discover.go +++ b/pkg/discoverer/k8s/service/discover.go @@ -18,10 +18,8 @@ package service import ( - "cmp" "context" "reflect" - "slices" "sync/atomic" "time" @@ -36,6 +34,7 @@ import ( "github.com/vdaas/vald/internal/log" "github.com/vdaas/vald/internal/net" "github.com/vdaas/vald/internal/safety" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/sync" "github.com/vdaas/vald/internal/sync/errgroup" ) @@ -304,8 +303,8 @@ func (d *discoverer) Start(ctx context.Context) (<-chan error, error) { for nodeName := range podsByNode { for namespace := range podsByNode[nodeName] { for appName, p := range podsByNode[nodeName][namespace] { - slices.SortFunc(p, func(left, right *payload.Info_Pod) int { - return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage()) + slices.SortFunc(p, func(left, right *payload.Info_Pod) bool { + return left.GetMemory().GetUsage() < right.GetMemory().GetUsage() }) podsByNode[nodeName][namespace][appName] = p nn, ok := nodeByName[nodeName] @@ -331,8 +330,8 @@ func (d *discoverer) Start(ctx context.Context) (<-chan error, error) { nn, ok := nodeByName[nodeName] if ok && nn.GetPods() != nil && nn.GetPods().GetPods() != nil { p := nn.GetPods().Pods - slices.SortFunc(p, func(left, right *payload.Info_Pod) int { - return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage()) + slices.SortFunc(p, func(left, right *payload.Info_Pod) bool { + return left.GetMemory().GetUsage() < right.GetMemory().GetUsage() }) nodeByName[nodeName].GetPods().Pods = p } @@ -346,8 +345,8 @@ func (d *discoverer) Start(ctx context.Context) (<-chan error, error) { defer wg.Done() for namespace := range podsByNamespace { for appName, p := range podsByNamespace[namespace] { - slices.SortFunc(p, func(left, right *payload.Info_Pod) int { - return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage()) + slices.SortFunc(p, func(left, right *payload.Info_Pod) bool { + return left.GetMemory().GetUsage() < right.GetMemory().GetUsage() }) podsByNamespace[namespace][appName] = p } @@ -359,8 +358,8 @@ func (d *discoverer) Start(ctx context.Context) (<-chan error, error) { d.eg.Go(safety.RecoverFunc(func() error { defer wg.Done() for appName, p := range podsByName { - slices.SortFunc(p, func(left, right *payload.Info_Pod) int { - return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage()) + slices.SortFunc(p, func(left, right *payload.Info_Pod) bool { + return left.GetMemory().GetUsage() < right.GetMemory().GetUsage() }) podsByName[appName] = p } @@ -468,16 +467,16 @@ func (d *discoverer) GetNodes(req *payload.Discoverer_Request) (nodes *payload.I for i := range ps.Pods { ps.GetPods()[i].Node = nil } - slices.SortFunc(ps.Pods, func(left, right *payload.Info_Pod) int { - return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage()) + slices.SortFunc(ps.Pods, func(left, right *payload.Info_Pod) bool { + return left.GetMemory().GetUsage() < right.GetMemory().GetUsage() }) n.Pods = ps } } ns = append(ns, n) } - slices.SortFunc(ns, func(left, right *payload.Info_Node) int { - return cmp.Compare(left.GetMemory().GetUsage(), right.GetMemory().GetUsage()) + slices.SortFunc(ns, func(left, right *payload.Info_Node) bool { + return left.GetMemory().GetUsage() < right.GetMemory().GetUsage() }) nodes.Nodes = ns diff --git a/pkg/gateway/filter/usecase/vald.go b/pkg/gateway/filter/usecase/vald.go index 8535fe19a0..270d0ad90f 100644 --- a/pkg/gateway/filter/usecase/vald.go +++ b/pkg/gateway/filter/usecase/vald.go @@ -18,7 +18,6 @@ package usecase import ( "context" - "slices" "github.com/vdaas/vald/apis/grpc/v1/vald" "github.com/vdaas/vald/internal/client/v1/client/filter/egress" @@ -33,6 +32,7 @@ import ( "github.com/vdaas/vald/internal/safety" "github.com/vdaas/vald/internal/servers/server" "github.com/vdaas/vald/internal/servers/starter" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/sync/errgroup" "github.com/vdaas/vald/pkg/gateway/filter/config" handler "github.com/vdaas/vald/pkg/gateway/filter/handler/grpc" diff --git a/pkg/gateway/lb/handler/grpc/aggregation.go b/pkg/gateway/lb/handler/grpc/aggregation.go index 58f749eee0..acfba37e5e 100644 --- a/pkg/gateway/lb/handler/grpc/aggregation.go +++ b/pkg/gateway/lb/handler/grpc/aggregation.go @@ -18,7 +18,6 @@ import ( "fmt" "math" "math/big" - "slices" "sync/atomic" "time" @@ -32,6 +31,7 @@ import ( "github.com/vdaas/vald/internal/net/grpc/errdetails" "github.com/vdaas/vald/internal/net/grpc/status" "github.com/vdaas/vald/internal/observability/trace" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/sync" ) @@ -557,7 +557,7 @@ func newSlice(num, replica int) Aggregator { } } -func (*valdSliceAggr) Start(_ context.Context) {} +func (_ *valdSliceAggr) Start(_ context.Context) {} func (v *valdSliceAggr) Send(ctx context.Context, data *payload.Search_Response) { result := data.GetResults() @@ -580,8 +580,8 @@ func (v *valdSliceAggr) Send(ctx context.Context, data *payload.Search_Response) } func (v *valdSliceAggr) Result() (res *payload.Search_Response) { - removeDuplicates(v.result, func(l, r *DistPayload) int { - return l.distance.Cmp(r.distance) + slices.RemoveDuplicates(v.result, func(l, r *DistPayload) bool { + return l.distance.Cmp(r.distance) < 0 }) if len(v.result) > v.num { @@ -646,8 +646,8 @@ func (v *valdPoolSliceAggr) Send(ctx context.Context, data *payload.Search_Respo } func (v *valdPoolSliceAggr) Result() (res *payload.Search_Response) { - removeDuplicates(v.result, func(l, r *DistPayload) int { - return l.distance.Cmp(r.distance) + slices.RemoveDuplicates(v.result, func(l, r *DistPayload) bool { + return l.distance.Cmp(r.distance) < 0 }) if len(v.result) > v.num { @@ -662,11 +662,3 @@ func (v *valdPoolSliceAggr) Result() (res *payload.Search_Response) { poolDist.Put(v.result[:0]) return res } - -func removeDuplicates[S ~[]E, E comparable](x S, less func(left, right E) int) S { - if len(x) < 2 { - return x - } - slices.SortStableFunc(x, less) - return slices.Compact(x) -} diff --git a/pkg/gateway/lb/handler/grpc/handler.go b/pkg/gateway/lb/handler/grpc/handler.go index 27f5bf8c90..0442aeceb8 100644 --- a/pkg/gateway/lb/handler/grpc/handler.go +++ b/pkg/gateway/lb/handler/grpc/handler.go @@ -20,7 +20,6 @@ package grpc import ( "context" "fmt" - "slices" "strconv" "sync/atomic" "time" @@ -39,6 +38,7 @@ import ( "github.com/vdaas/vald/internal/net/grpc/status" "github.com/vdaas/vald/internal/observability/trace" "github.com/vdaas/vald/internal/safety" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/strings" "github.com/vdaas/vald/internal/sync" "github.com/vdaas/vald/internal/sync/errgroup" diff --git a/pkg/gateway/lb/handler/grpc/search_benchmark_test.go b/pkg/gateway/lb/handler/grpc/search_benchmark_test.go index 9302c2cdf0..3288e04d0a 100644 --- a/pkg/gateway/lb/handler/grpc/search_benchmark_test.go +++ b/pkg/gateway/lb/handler/grpc/search_benchmark_test.go @@ -14,18 +14,17 @@ package grpc import ( - "cmp" "context" "flag" "fmt" "runtime" - "slices" "sync/atomic" "testing" "time" "github.com/vdaas/vald/apis/grpc/v1/payload" "github.com/vdaas/vald/internal/rand" + "github.com/vdaas/vald/internal/slices" "github.com/vdaas/vald/internal/sync" "github.com/vdaas/vald/internal/sync/errgroup" "github.com/vdaas/vald/internal/test/data/strings" @@ -80,8 +79,8 @@ func newRandomResponse() (res *payload.Search_Response) { Distance: rand.Float32(), }) } - slices.SortFunc(res.Results, func(left, right *payload.Object_Distance) int { - return cmp.Compare(left.GetDistance(), right.GetDistance()) + slices.SortFunc(res.Results, func(left, right *payload.Object_Distance) bool { + return left.GetDistance() < right.GetDistance() }) return res }