Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swag init generates documentation too slowly #1919

Open
luoxianjie opened this issue Oct 29, 2024 · 0 comments
Open

swag init generates documentation too slowly #1919

luoxianjie opened this issue Oct 29, 2024 · 0 comments

Comments

@luoxianjie
Copy link

Is your feature request related to a problem? Please describe.
As our projects become larger and larger, swag init will be very slow to generate documents, usually taking more than half an hour, which is very painful.

Describe the solution you'd like
Looking at the source code, we found that it takes a lot of time to search for dependency packages, because all dependencies will be searched repeatedly. It is not as good as both B and C depending on A. When building B and C, A will be searched repeatedly. We add the FindPackage method. After add the cache, I found that the speed increased by nearly 10 times. The code is as follows

conf := loader.Config{
  ParserMode:  goparser.ParseComments,
  Cwd:         cwd,
  FindPackage: FindPackage,
}
var packageCache = make(map[string]*build.Package)
var cacheMutex sync.RWMutex
var packageLocks sync.Map

func FindPackage(ctxt *build.Context, importPath, fromDir string, mode build.ImportMode) (*build.Package, error) {
	cacheMutex.RLock()
	pkg, found := packageCache[importPath]
	cacheMutex.RUnlock()

	if found {
		return pkg, nil
	}

	lockInterface, _ := packageLocks.LoadOrStore(importPath, &sync.Mutex{})
	lock := lockInterface.(*sync.Mutex)

	lock.Lock()
	defer lock.Unlock() 

	cacheMutex.RLock()
	pkg, found = packageCache[importPath]
	cacheMutex.RUnlock()

	if found {
		return pkg, nil
	}

	pkg, err := ctxt.Import(importPath, fromDir, mode)
	if err != nil {
		return nil, err
	}

	cacheMutex.Lock()
	packageCache[importPath] = pkg
	cacheMutex.Unlock()

	return pkg, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant