Skip to content

Commit 864735d

Browse files
committed
perf(endpoint): optimize ProviderSpecific to use map for O(1) access
1 parent f583111 commit 864735d

24 files changed

+356
-591
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
@@ -207,14 +207,54 @@ func (t Targets) IsLess(o Targets) bool {
207207
return false
208208
}
209209

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

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

219259
// EndpointKey is the type of a map key for separating endpoints or targets.
220260
type EndpointKey struct {
@@ -225,7 +265,6 @@ type EndpointKey struct {
225265
}
226266

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

295334
// GetProviderSpecificProperty returns the value of a ProviderSpecificProperty if the property exists.
296335
func (e *Endpoint) GetProviderSpecificProperty(key string) (string, bool) {
297-
for _, providerSpecific := range e.ProviderSpecific {
298-
if providerSpecific.Name == key {
299-
return providerSpecific.Value, true
300-
}
336+
if e.ProviderSpecific == nil {
337+
return "", false
301338
}
302-
return "", false
339+
return e.ProviderSpecific.Get(key)
303340
}
304341

305342
// SetProviderSpecificProperty sets the value of a ProviderSpecificProperty.
306343
func (e *Endpoint) SetProviderSpecificProperty(key string, value string) {
307-
for i, providerSpecific := range e.ProviderSpecific {
308-
if providerSpecific.Name == key {
309-
e.ProviderSpecific[i] = ProviderSpecificProperty{
310-
Name: key,
311-
Value: value,
312-
}
313-
return
314-
}
344+
if e.ProviderSpecific == nil {
345+
e.ProviderSpecific = make(ProviderSpecific)
315346
}
316-
317-
e.ProviderSpecific = append(e.ProviderSpecific, ProviderSpecificProperty{Name: key, Value: value})
347+
e.ProviderSpecific.Set(key, value)
318348
}
319349

320350
// DeleteProviderSpecificProperty deletes any ProviderSpecificProperty of the specified name.
321351
func (e *Endpoint) DeleteProviderSpecificProperty(key string) {
322-
for i, providerSpecific := range e.ProviderSpecific {
323-
if providerSpecific.Name == key {
324-
e.ProviderSpecific = append(e.ProviderSpecific[:i], e.ProviderSpecific[i+1:]...)
325-
return
326-
}
352+
if e.ProviderSpecific == nil {
353+
return
327354
}
355+
e.ProviderSpecific.Delete(key)
328356
}
329357

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

0 commit comments

Comments
 (0)