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

View File

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