Skip to content

Commit 921fb70

Browse files
committed
perf(endpoint): optimize ProviderSpecific to use map for O(1) access
1 parent cd37cc9 commit 921fb70

25 files changed

+356
-628
lines changed

apis/v1alpha1/dnsendpoint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type DNSEndpointSpec struct {
5252
Endpoints []*Endpoint `json:"endpoints"`
5353
}
5454

55+
// ProviderSpecificProperty holds the name and value of a configuration which is specific to individual DNS providers
5556
type ProviderSpecificProperty struct {
5657
Name string `json:"name,omitempty"`
5758
Value string `json:"value,omitempty"`

endpoint/endpoint.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,54 @@ func (t Targets) IsLess(o Targets) bool {
206206
return false
207207
}
208208

209-
// ProviderSpecificProperty holds the name and value of a configuration which is specific to individual DNS providers
210-
type ProviderSpecificProperty struct {
211-
Name string `json:"name,omitempty"`
212-
Value string `json:"value,omitempty"`
209+
// ProviderSpecific holds configuration which is specific to individual DNS providers
210+
type ProviderSpecific map[string]string
211+
212+
func (ps ProviderSpecific) Set(key, value string) {
213+
ps[key] = value
213214
}
214215

215-
// ProviderSpecific holds configuration which is specific to individual DNS providers
216-
type ProviderSpecific []ProviderSpecificProperty
216+
func (ps ProviderSpecific) Get(key string) (string, bool) {
217+
value, ok := ps[key]
218+
return value, ok
219+
}
220+
221+
func (ps ProviderSpecific) Delete(key string) {
222+
delete(ps, key)
223+
}
224+
225+
// String returns a stable, backwards-compatible string form.
226+
// For compatibility with older branches where ProviderSpecific was
227+
// []ProviderSpecificProperty, we render:
228+
// - empty or nil as "[]"
229+
// - non-empty as slice-like entries sorted by key, e.g. "[{k1 v1} {k2 v2}]"
230+
// This preserves previous log expectations and keeps output deterministic.
231+
func (ps ProviderSpecific) String() string {
232+
if len(ps) == 0 {
233+
return "[]"
234+
}
235+
// Collect and sort keys for stable output.
236+
keys := make([]string, 0, len(ps))
237+
for k := range ps {
238+
keys = append(keys, k)
239+
}
240+
sort.Strings(keys)
241+
// Build entries like "{key value}" preserving stable order.
242+
b := strings.Builder{}
243+
b.WriteByte('[')
244+
for i, k := range keys {
245+
if i > 0 {
246+
b.WriteByte(' ')
247+
}
248+
b.WriteByte('{')
249+
b.WriteString(k)
250+
b.WriteByte(' ')
251+
b.WriteString(ps[k])
252+
b.WriteByte('}')
253+
}
254+
b.WriteByte(']')
255+
return b.String()
256+
}
217257

218258
// EndpointKey is the type of a map key for separating endpoints or targets.
219259
type EndpointKey struct {
@@ -224,7 +264,6 @@ type EndpointKey struct {
224264
}
225265

226266
// Endpoint is a high-level way of a connection between a service and an IP
227-
// +kubebuilder:object:generate=true
228267
type Endpoint struct {
229268
// The hostname of the DNS record
230269
DNSName string `json:"dnsName,omitempty"`
@@ -293,37 +332,26 @@ func (e *Endpoint) WithProviderSpecific(key, value string) *Endpoint {
293332

294333
// GetProviderSpecificProperty returns the value of a ProviderSpecificProperty if the property exists.
295334
func (e *Endpoint) GetProviderSpecificProperty(key string) (string, bool) {
296-
for _, providerSpecific := range e.ProviderSpecific {
297-
if providerSpecific.Name == key {
298-
return providerSpecific.Value, true
299-
}
335+
if e.ProviderSpecific == nil {
336+
return "", false
300337
}
301-
return "", false
338+
return e.ProviderSpecific.Get(key)
302339
}
303340

304341
// SetProviderSpecificProperty sets the value of a ProviderSpecificProperty.
305342
func (e *Endpoint) SetProviderSpecificProperty(key string, value string) {
306-
for i, providerSpecific := range e.ProviderSpecific {
307-
if providerSpecific.Name == key {
308-
e.ProviderSpecific[i] = ProviderSpecificProperty{
309-
Name: key,
310-
Value: value,
311-
}
312-
return
313-
}
343+
if e.ProviderSpecific == nil {
344+
e.ProviderSpecific = make(ProviderSpecific)
314345
}
315-
316-
e.ProviderSpecific = append(e.ProviderSpecific, ProviderSpecificProperty{Name: key, Value: value})
346+
e.ProviderSpecific.Set(key, value)
317347
}
318348

319349
// DeleteProviderSpecificProperty deletes any ProviderSpecificProperty of the specified name.
320350
func (e *Endpoint) DeleteProviderSpecificProperty(key string) {
321-
for i, providerSpecific := range e.ProviderSpecific {
322-
if providerSpecific.Name == key {
323-
e.ProviderSpecific = append(e.ProviderSpecific[:i], e.ProviderSpecific[i+1:]...)
324-
return
325-
}
351+
if e.ProviderSpecific == nil {
352+
return
326353
}
354+
e.ProviderSpecific.Delete(key)
327355
}
328356

329357
// WithLabel adds or updates a label for the Endpoint.

0 commit comments

Comments
 (0)