Skip to content

Added caching - #136

Merged
ambv merged 3 commits into
psf:masterfrom
ojii:cache-timestamps-and-size
Apr 19, 2018
Merged

ambv merged 3 commits into
psf:masterfrom
ojii:cache-timestamps-and-size

Conversation

@ojii

@ojii ojii commented Apr 17, 2018

Copy link
Copy Markdown
Contributor

This implements what was discussed in #109.

With this change, Black will cache already formatted files using their file size and modification timestamp. The cache is per-user and will always be used unless Black is used with --diff or with code provided via standard input.

Cache files are located at:

  • Windows: C:\\Users\<username>\AppData\Local\black\black\Cache\<version>\cache.pkl
  • macOS: /Users/<username>/Library/Caches/black/<version>/cache.pkl
  • Linux: /home/<username>/.cache/black/<version>/cache.pkl

This is the first time that I've used pipenv and I'm not sure that the changes to the lockfile are correct. Please let me know how I can fix this if necessary.

ojii added 2 commits April 17, 2018 15:06
Black will cache already formatted files using their file size and
modification timestamp. The cache is per-user and will always be used
unless Black is used with --diff or with code provided via standard
input.
@coveralls

coveralls commented Apr 17, 2018 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 234

  • 246 of 256 (96.09%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+4.4%) to 94.685%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tests/test_black.py 141 142 99.3%
black.py 105 114 92.11%
Totals Coverage Status
Change from base Build 222: 4.4%
Covered Lines: 1728
Relevant Lines: 1825

💛 - Coveralls

@ojii

ojii commented Apr 17, 2018

Copy link
Copy Markdown
Contributor Author

I'm not sure why 3.7-dev is failing. Running locally, both latest commit on 3.7 branch (7899153c1f9e6808c7729200f62ab2a70cfa9427) and v3.7.0b3 work for me.

Also, please apologize for the messy second commit. I didn't realize flake8 was used and only ran mypy on black.py, not the tests.

@ambv ambv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very nice! I love the improved code coverage.

On a general level, I'd like us to avoid using the word "cache" in messages and docs. A "cache" suggests that we're storing the output of formatting for future reuse. I gave you suggestions what I have in mind. Note that I'm fine using the name "cache" in the source since this is how the implementation sees it.

There's a few other things, see comments.

Thanks again for working on it, this is a nice architectural improvement.

Comment thread black.py Outdated
__version__ = "18.4a2"
DEFAULT_LINE_LENGTH = 88
CACHE_DIR = Path(user_cache_dir("black", version=__version__))
CACHE_FILE = CACHE_DIR / "cache.pkl"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. lib2to3 uses ".pickle", let's use that for the file extension, too.
  2. You might have noticed, black.py is grouping related global variables close to where they're used. With this approach, it would be nicer if you put CACHE_DIR and CACHE_FILE immediately before read_cache().

Comment thread black.py Outdated
write_back = WriteBack.DIFF
else:
write_back = WriteBack.YES

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: In black.py we avoid vertical whitespace.

Comment thread black.py
ctx.exit(return_code)


def run_single_file_mode(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the amount of context that you need to pass, I think run_single_file_mode() and run_multi_file_mode() are unnecessary. You aren't calling them otherwise. I'm not sure why you extracted those functions out?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flake8 was complaining about main getting too complicated, which is why I split it into those functions. I did not plan to do so but that was the easiest way I could think of to reduce the complexity in a single function.

Comment thread black.py Outdated
else:
report.done(src, task.result())

cache: Union[Cache, None]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Just use cache: Cache = {} here, then you don't need the else clause below.
  2. Instead of checking for if cache is not None and formatted: in 334, state explicitly: if write_back != WriteBack.DIFF and formatted:. That removes the indirection that otherwise makes people ask "why would cache ever be None?".

(Same applies to the single-file case.)

Comment thread black.py
cancelled = []
formatted = []

if sources:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't really need that indent for the entire block. The only thing you need to wrap in if sources: is lines 310-313.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to indent everything here, but why do the extra work if there's nothing to do? If sources is empty, none of those lines need to run.

Comment thread black.py Outdated
def filter_cached(
cache: Cache, sources: Iterable[Path]
) -> Tuple[List[Path], List[Path]]:
"""Splits a list of paths to format into a tuple of a list of paths that may still

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 257 and use the imperative form ("Split a list of paths into two.\n\nThe first list contains paths of files that modified on disk. The other list contains paths to non-modified files."")

Comment thread black.py Outdated
if src not in cache:
todo.append(src)
else:
if cache[src] != get_cache_info(src):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can wind the if on 2512 into this one:

if cache.get(src) != get_cache_info(src):

Comment thread black.py
return todo, done


def write_cache(cache: Cache, sources: List[Path]) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache is a performance measure. If it fails to read or write, this should be transparent to the user. This function needs to be wrapped in try:except OSError: to make write failures invisible.

Comment thread README.md Outdated
master, this is also an option.


## Caching

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Ignoring non-modified files"

Comment thread README.md Outdated

## Caching

Black caches already formatted files, unless the `--diff` flag is used or

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In the README, we always use italics and a capital letter for the name of the project. So, *Black*.
  2. "Black remembers files it already formatted".

@ambv

ambv commented Apr 17, 2018

Copy link
Copy Markdown
Collaborator

I'll look at that 3.7-dev failure later.

@ambv

ambv commented Apr 19, 2018

Copy link
Copy Markdown
Collaborator

You didn't annotate one function and I have some nits about the remaining changes but I can fix this forward faster. I'll merge as is for now.

Thanks a lot! ✨ 🍰 ✨

@ambv
ambv merged commit 639b62d into psf:master Apr 19, 2018
@ojii
ojii deleted the cache-timestamps-and-size branch April 19, 2018 01:57
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.

3 participants