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

Refactor CountStats state machine #247

Merged
merged 1 commit into from Mar 29, 2021

Conversation

dbaggerman
Copy link
Collaborator

Since I've been back in the scc code, I got tempted to make an(other) attempt to break up the complexity in the CountStats state machine.

This changes from states being an enum, with all the state tracked in CountStats and passed in/out of the different methods to introduce a State interface with different states having their stateXYZ function moved to being attached to the relevant struct. This means we can drop the switch statement in the core loop and instead let the go runtime do the dispatch. It also lets relevant state be tracked in the State struct instead of being passed in and out of CountStats.

As a bonus, the different 'flavours' of slComment vs slCommentCode, etc are no longer required.

It seems to produce the same result in the examples I've thrown at it, other than python docstrings - issue #246 doesn't exist in this implementation (this mismatch is how I found that issue in the first place). It could probably use a bit more hammering just to make sure though - I haven't tested permutations of optional flags for example.

Performance is roughly on par with the current implementation, only being marginally slower:

Benchmark #1: ./scc-master ../../torvalds/linux
  Time (mean ± σ):     688.1 ms ±  11.9 ms    [User: 8.613 s, System: 0.970 s]
  Range (min … max):   663.2 ms … 712.3 ms    50 runs

Benchmark #2: ./scc-refactor ../../torvalds/linux
  Time (mean ± σ):     695.7 ms ±  10.4 ms    [User: 8.567 s, System: 1.034 s]
  Range (min … max):   675.1 ms … 720.2 ms    50 runs

Summary
  './scc-master ../../torvalds/linux' ran
    1.01 ± 0.02 times faster than './scc-refactor ../../torvalds/linux'

@boyter
Copy link
Owner

boyter commented Mar 28, 2021

Yeah the state machine stuff is something I have been toying around with as well. I want too start pulling out common languages into their own versions which should allow for further performance optimisations. For example, counting text files should be very fast as they only need to check for new lines, and if the line is all whitespace to produce the correct counts.

Something ill get back into at some point.

@dbaggerman dbaggerman merged commit e8712d0 into boyter:master Mar 29, 2021
boyter added a commit that referenced this pull request Dec 14, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants