Skip to content

Commit

Permalink
tweak uloc
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Apr 30, 2024
1 parent 4782495 commit 594e3a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 86 deletions.
19 changes: 7 additions & 12 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var tabularShortFormatBodyNoComplexity = "%-22s %11d %11d %10d %11d %9d\n"
var tabularShortFormatFileNoComplexity = "%s %11d %10d %11d %9d\n"
var longNameTruncate = 22

var tabularUlocFormatBody = "Total Unique Source Lines of Code (ULOC) %38d\n"

var tabularWideBreak = "─────────────────────────────────────────────────────────────────────────────────────────────────────────────\n"
var tabularWideBreakCi = "-------------------------------------------------------------------------------------------------------------\n"
var tabularWideFormatHead = "%-33s %9s %9s %8s %9s %8s %10s %16s\n"
Expand Down Expand Up @@ -673,18 +675,6 @@ create table t (
return str.String()
}

// var tabularWideFormatBody = "%-33s %9d %9d %8d %9d %8d %10d %16.2f\n"
var tabularUlocFormatBody = "Total Unique Source Lines of Code (ULOC) %38d\n"

func ulocDisplay(input int) string {
var str strings.Builder
str.WriteString(getTabularShortBreak())
str.WriteString(fmt.Sprintf(tabularUlocFormatBody, input))
str.WriteString(getTabularShortBreak())

return str.String()
}

func fileSummarize(input chan *FileJob) string {
if FormatMulti != "" {
return fileSummarizeMulti(input)
Expand Down Expand Up @@ -1077,6 +1067,11 @@ func fileSummarizeShort(input chan *FileJob) string {
}
str.WriteString(getTabularShortBreak())

if UlocMode {
str.WriteString(fmt.Sprintf(tabularUlocFormatBody, len(ulocGlobalCount)))
str.WriteString(getTabularShortBreak())
}

if !Cocomo {
if SLOCCountFormat {
calculateCocomoSLOCCount(sumCode, &str)
Expand Down
5 changes: 5 additions & 0 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ func printLanguages() {
}
}

// global variables to deal with ULOC calculations
var ulocMutex = sync.Mutex{}
var ulocGlobalCount = map[string]struct{}{}
var ulocLanguageCount = map[string]map[string]struct{}{}

// Process is the main entry point of the command line it sets everything up and starts running
func Process() {
if Languages {
Expand Down
88 changes: 14 additions & 74 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,6 @@ func fileProcessorWorker(input chan *FileJob, output chan *FileJob) {
var fileCount int64
var gcEnabled int64
var wg sync.WaitGroup
var ulocMutex = sync.Mutex{}
uloc := map[string]struct{}{}

for i := 0; i < FileProcessJobWorkers; i++ {
wg.Add(1)
Expand Down Expand Up @@ -682,13 +680,6 @@ func fileProcessorWorker(input chan *FileJob, output chan *FileJob) {
if processFile(job) {
output <- job
}
if UlocMode {
ulocMutex.Lock()
for _, l := range strings.Split(string(content), "\n") {
uloc[l] = struct{}{}
}
ulocMutex.Unlock()
}
} else {
if Verbose {
printWarn(fmt.Sprintf("error reading: %s %s", job.Location, err))
Expand All @@ -703,78 +694,13 @@ func fileProcessorWorker(input chan *FileJob, output chan *FileJob) {
go func() {
wg.Wait()
close(output)
fmt.Println(ulocDisplay(len(uloc)))

if Debug {
printDebug(fmt.Sprintf("milliseconds reading files into memory: %d", makeTimestampMilli()-startTime))
}
}()
}

// Processes files using ULOC - Unique lines of code
func fileProcessorWorkerUloc(input chan *FileJob, output chan *FileJob) {
var startTime int64
var fileCount int64
var gcEnabled int64
var wg sync.WaitGroup

var ulocMutex = sync.Mutex{}
uloc := map[string]struct{}{}

for i := 0; i < FileProcessJobWorkers; i++ {
wg.Add(1)
go func() {
reader := NewFileReader()

for job := range input {
atomic.CompareAndSwapInt64(&startTime, 0, makeTimestampMilli())

loc := job.Location
if job.Symlocation != "" {
loc = job.Symlocation
}

fileStartTime := makeTimestampNano()
content, err := reader.ReadFile(loc, int(job.Bytes))
atomic.AddInt64(&fileCount, 1)

if atomic.LoadInt64(&gcEnabled) == 0 && atomic.LoadInt64(&fileCount) >= int64(GcFileCount) {
debug.SetGCPercent(gcPercent)
atomic.AddInt64(&gcEnabled, 1)
if Verbose {
printWarn("read file limit exceeded GC re-enabled")
}
}

if Trace {
printTrace(fmt.Sprintf("nanoseconds read into memory: %s: %d", job.Location, makeTimestampNano()-fileStartTime))
}

if err == nil {
ulocMutex.Lock()
for _, l := range strings.Split(string(content), "\n") {
uloc[l] = struct{}{}
}
ulocMutex.Unlock()
} else {
if Verbose {
printWarn(fmt.Sprintf("error reading: %s %s", job.Location, err))
}
}
}

wg.Done()
}()
}

wg.Wait()
if Debug {
printDebug(fmt.Sprintf("milliseconds reading files into memory: %d", makeTimestampMilli()-startTime))
}

fmt.Println(ulocDisplay(len(uloc)))
}

// Process a single file
// File must have been read to job.Content already
func processFile(job *FileJob) bool {
Expand Down Expand Up @@ -823,6 +749,20 @@ func processFile(job *FileJob) bool {

CountStats(job)

if UlocMode {
ulocMutex.Lock()
for _, l := range strings.Split(string(job.Content), "\n") {
ulocGlobalCount[l] = struct{}{}

_, ok := ulocLanguageCount[job.Language]
if !ok {
ulocLanguageCount[job.Language] = map[string]struct{}{}
}
ulocLanguageCount[job.Language][l] = struct{}{}
}
ulocMutex.Unlock()
}

if Duplicates {
duplicates.mux.Lock()
jobHash := job.Hash.Sum(nil)
Expand Down

0 comments on commit 594e3a7

Please sign in to comment.