Skip to content

Commit

Permalink
Add getEntryCache() and setEntryCache()
Browse files Browse the repository at this point in the history
  • Loading branch information
sengleung committed Dec 20, 2023
1 parent 9eb5fef commit d521820
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 2 additions & 8 deletions pkg/yang/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,18 +559,12 @@ func ToEntry(n Node) (e *Entry) {
}
}
ms := RootNode(n).Modules

// Protect the entryCache map from concurrent access.
ms.entryCacheMu.RLock()
e = ms.entryCache[n]
ms.entryCacheMu.RUnlock()
e = ms.getEntryCache(n)
if e != nil {
return e
}
defer func() {
ms.entryCacheMu.Lock()
ms.entryCache[n] = e
ms.entryCacheMu.Unlock()
ms.setEntryCache(n, e)
}()

// Copy in the extensions from our Node, if any.
Expand Down
15 changes: 14 additions & 1 deletion pkg/yang/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type Modules struct {
typeDict *typeDictionary // Cache for type definitions.
entryCacheMu sync.RWMutex // entryCacheMu protects the entryCache map.
// entryCache is used to prevent unnecessary recursion into previously
// converted nodes.
// converted nodes. To access the map, use the get/set/ClearEntryCache()
// thread-safe functions.
entryCache map[Node]*Entry
// mergedSubmodule is used to prevent re-parsing a submodule that has already
// been merged into a particular entity when circular dependencies are being
Expand Down Expand Up @@ -444,6 +445,18 @@ func (ms *Modules) include(m *Module) error {
return nil
}

func (ms *Modules) getEntryCache(n Node) *Entry {
ms.entryCacheMu.RLock()
defer ms.entryCacheMu.RUnlock()
return ms.entryCache[n]
}

func (ms *Modules) setEntryCache(n Node, e *Entry) {
ms.entryCacheMu.Lock()
defer ms.entryCacheMu.Unlock()
ms.entryCache[n] = e
}

// ClearEntryCache clears the entryCache containing previously converted nodes
// used by the ToEntry function.
func (ms *Modules) ClearEntryCache() {
Expand Down

0 comments on commit d521820

Please sign in to comment.