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

Faster imports #23

Closed
wants to merge 9 commits into from
Closed

Faster imports #23

wants to merge 9 commits into from

Conversation

ryanpdx
Copy link
Member

@ryanpdx ryanpdx commented Mar 31, 2024

Tested with python -X importtime run.py gps -b can0 on C3 card at max CPU frequency

Import time before changes: 5.9s
Import time after changes: 4.1s

Changes:

  • Replaced natsort with a custom function (removed 300ms)
  • Replaced loguru with build-in logging module (removed 260ms)
  • Set minimum python-can to 4.3.0 (release fixed internal pkg_resource slowdown, removed 1s)

Could remove 1.5s more if removed or split out theFlask app, but that could make the projects harder to modify for new students.

@ryanpdx ryanpdx added the enhancement New feature or request label Mar 31, 2024
@ryanpdx ryanpdx requested a review from ThirteenFish March 31, 2024 08:06
Copy link
Contributor

@ThirteenFish ThirteenFish left a comment

Choose a reason for hiding this comment

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

This is a really awesome improvement, figuring out what was going on with the pkg_resource stuff is really great.

Comment on lines -66 to +74
ignore = "E402,C901,C0103,E203,R0912,R0915,R901,R901,R0914,C0413,C0206,R1716,W1514,R0902,R0913,W0707,W0102,W0107,R0903"
# W1201: Use % formatting in logging functions
# W1203: Use %s formatting in logging functions
ignore = "E402,C901,C0103,E203,R0912,R0915,R901,R901,R0914,C0413,C0206,R1716,W1514,R0902,R0913,W0707,W0102,W0107,R0903,W1201,W1203"
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of ignoring W1203 in pylama, for deliberate style choices (and looking at the pylint issues this was a pretty controversial lint) it's a good idea to disable it in pylint directly using a new section:

[tool.pylint.logging]
disable="logging-fstring-interpolation"

Pylama will pick up this config anyway, and this way I can run pylint directly too without the noise.

On the other hand, W1201 seems like something worth fixing directly. That's warning about using concatenation instead of an fstring, the three instances can be upgraded to fstrings.

@@ -32,6 +33,9 @@
except ImportError:
__version__ = "0.0.0" # package is not installed

logger = logging.getLogger(__file__)
LOGGER_FORMAT = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)s] %(message)s"
Copy link
Contributor

Choose a reason for hiding this comment

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

This format string gets pretty close to the old loguru one: %(asctime)s | %(levelname)-9s | %(name)s:%(funcName)s:%(lineno)s - %(message)s.

Importantly this includes name (the logger name) and funcName. Just using filename lead to some confusion because it doesn't show the path to the filename. The logs were getting spammed from base.py and it took a hot second to figure out where base.py was.

@@ -32,6 +33,9 @@
except ImportError:
__version__ = "0.0.0" # package is not installed

logger = logging.getLogger(__file__)
LOGGER_FORMAT = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)s] %(message)s"

Copy link
Contributor

Choose a reason for hiding this comment

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

On the topic of base.py, it comes from canopen. It's logging a bunch of stuff about PDO creation(?) to loglevel INFO that we don't need to see. Either here or near a canopen import it'd be nice to set it to a more reasonable logging.getLogger("canopen").setLevel(logging.ERROR).


logger_tmp_file_setup(level)
logging.basicConfig(format=LOGGER_FORMAT, level=level, handlers=handlers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Setting the format argument will automatically set the formatter for all Handlers passed to handlers (it's the only thing that the format arg does). You can save a few lines above by removing the formatter bits.

if args.log:
logger.add(SysLogHandler(address="/dev/log"), level=level, backtrace=True)
syslog_handler = SysLogHandler()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there still a reason to have a dedicated SysLogHandler? The two running environments I'm aware of are dev in which case I always want stdio, and on the cards where they're being run as systemd units and stdio is redirected to the journal anyway.

else:
logger.add(sys.stdout, level=level, backtrace=True)
stream_handler = logging.StreamHandler()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if using stdout previously was deliberate, but if it was StreamHandler defaults to stderr.


logger_tmp_file_setup(level)
logging.basicConfig(format=LOGGER_FORMAT, level=level, handlers=handlers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Configuring logging here, during the application start, means that not all log entries may be handled by the appropriate handlers. In the C3 app for example there's a line about the RTC that occurs before olaf_setup(). If basicConfig() were moved up into the global scope then it would reasonably configure the loggers before any of our code gets run.

The only catch is that it needs to be configured here because the list of handlers depends on the result of argparse, but does it really need to? If SysLogHandler was dropped then that would free this up to be moved.

@ryanpdx ryanpdx closed this Aug 11, 2024
@ryanpdx ryanpdx deleted the faster-import branch August 11, 2024 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants