Robust change detection: replaced string concat with struct-based compare in hasModelMappingsChanged; removed boolTo01.

•  Performance: pre-allocate map and regex slice capacities in UpdateMappings.
   •  Verified with amp module tests (all passing)
This commit is contained in:
altamash
2025-12-23 18:52:28 +05:30
parent 5dcf7cb846
commit 0c0aae1eac
2 changed files with 13 additions and 18 deletions

View File

@@ -279,21 +279,23 @@ func (m *AmpModule) hasModelMappingsChanged(old *config.AmpCode, new *config.Amp
return true
}
// Build map for efficient comparison
oldMap := make(map[string]string, len(old.ModelMappings))
// Build map for efficient and robust comparison
type mappingInfo struct {
to string
regex bool
}
oldMap := make(map[string]mappingInfo, len(old.ModelMappings))
for _, mapping := range old.ModelMappings {
from := strings.TrimSpace(mapping.From)
to := strings.TrimSpace(mapping.To)
key := from
val := to + "|regex=" + boolTo01(mapping.Regex)
oldMap[key] = val
oldMap[strings.TrimSpace(mapping.From)] = mappingInfo{
to: strings.TrimSpace(mapping.To),
regex: mapping.Regex,
}
}
for _, mapping := range new.ModelMappings {
from := strings.TrimSpace(mapping.From)
to := strings.TrimSpace(mapping.To)
val := to + "|regex=" + boolTo01(mapping.Regex)
if oldVal, exists := oldMap[from]; !exists || oldVal != val {
if oldVal, exists := oldMap[from]; !exists || oldVal.to != to || oldVal.regex != mapping.Regex {
return true
}
}
@@ -301,13 +303,6 @@ func (m *AmpModule) hasModelMappingsChanged(old *config.AmpCode, new *config.Amp
return false
}
func boolTo01(b bool) string {
if b {
return "1"
}
return "0"
}
// hasAPIKeyChanged compares old and new API keys.
func (m *AmpModule) hasAPIKeyChanged(old *config.AmpCode, new *config.AmpCode) bool {
oldKey := ""

View File

@@ -91,8 +91,8 @@ func (m *DefaultModelMapper) UpdateMappings(mappings []config.AmpModelMapping) {
defer m.mu.Unlock()
// Clear and rebuild mappings
m.mappings = make(map[string]string)
m.regexps = m.regexps[:0]
m.mappings = make(map[string]string, len(mappings))
m.regexps = make([]regexMapping, 0, len(mappings))
for _, mapping := range mappings {
from := strings.TrimSpace(mapping.From)