MCPcopy Create free account
hub / github.com/bufbuild/buf / newFiles

Function newFiles

private/bufpkg/bufprotosource/files.go:28–77  ·  view source on GitHub ↗
(
	ctx context.Context,
	inputFiles []F,
	resolver protodesc.Resolver,
)

Source from the content-addressed store, hash-verified

26const defaultChunkSizeThreshold = 8
27
28func newFiles[F InputFile](
29 ctx context.Context,
30 inputFiles []F,
31 resolver protodesc.Resolver,
32) ([]File, error) {
33 indexedInputFiles := xslices.ToIndexed(inputFiles)
34 if len(indexedInputFiles) == 0 {
35 return nil, nil
36 }
37
38 // Why were we chunking this? We could just send each individual call to thread.Parallelize
39 // and let thread.Parallelize deal with what to do.
40
41 chunkSize := len(indexedInputFiles) / thread.Parallelism()
42 if chunkSize < defaultChunkSizeThreshold {
43 files := make([]File, 0, len(indexedInputFiles))
44 for _, indexedInputFile := range indexedInputFiles {
45 file, err := newFile(indexedInputFile.Value, resolver)
46 if err != nil {
47 return nil, err
48 }
49 files = append(files, file)
50 }
51 return files, nil
52 }
53 chunks := xslices.ToChunks(indexedInputFiles, chunkSize)
54 indexedFiles := make([]xslices.Indexed[File], 0, len(indexedInputFiles))
55 jobs := make([]func(context.Context) error, len(chunks))
56 var lock sync.Mutex
57 for i, indexedInputFileChunk := range chunks {
58 jobs[i] = func(ctx context.Context) error {
59 iIndexedFiles := make([]xslices.Indexed[File], 0, len(indexedInputFileChunk))
60 for _, indexedInputFile := range indexedInputFileChunk {
61 file, err := newFile(indexedInputFile.Value, resolver)
62 if err != nil {
63 return err
64 }
65 iIndexedFiles = append(iIndexedFiles, xslices.Indexed[File]{Value: file, Index: indexedInputFile.Index})
66 }
67 lock.Lock()
68 indexedFiles = append(indexedFiles, iIndexedFiles...)
69 lock.Unlock()
70 return nil
71 }
72 }
73 if err := thread.Parallelize(ctx, jobs); err != nil {
74 return nil, err
75 }
76 return xslices.IndexedToSortedValues(indexedFiles), nil
77}

Callers 1

NewFilesFunction · 0.85

Calls 5

ParallelismFunction · 0.92
ParallelizeFunction · 0.92
newFileFunction · 0.70
LockMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…