Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Modern Optimization (was: Beazley's Problem) Date: 26 Sep 2024 12:51:13 GMT Organization: Stefan Ram Lines: 52 Expires: 1 Jul 2025 11:59:58 GMT Message-ID: References: <87tte941ko.fsf@nightsong.com> <87plow4v4p.fsf@nightsong.com> <0709b4b8b0bbf2a32d53649d1a6fbefbcd44a68a.camel@tilde.green> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de gVEjUUA4jt9uTbwrbofZHwVRVoPfFdysO5CdIDPr3CTuCw Cancel-Lock: sha1:4hhSsxGrl8NrlXRO/sZgrzVzvWo= sha256:qjnScNwl8PQhdq08Xi7kGRaQXBNDuBOZNnezb2FySr8= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Bytes: 3149 ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted: >totally implement anything in an imperative or functional style. In functional programming, you don't redefine names. So, |let i := 7 is still kosher with functional programming, while |let i := 7 |let i := 8 is a no-go. Why am I bringing this up? If you redefine a name in a Python module (since around 2022), like, |i = 7 .. . . |i = 8 , you're putting the kibosh on a certain optimization for name lookup and your program's going to drag. This means that sprinkling in a little functional programming mojo can make your Python programs zip along! This was laid out by Kevin Modzelewski in a talk back in 2022. He dropped these nuggets for Python programs (for CPython, I take it) that don't cramp modern optimizations: - Don't reassign global variables. - All objects of a class should have the same attributes (names, not values; i.e., "obj.dict.keys()" shouldn't be different between objects of the same class). - Set the same attributes in the same order for all objects of a class. - Use slots. - Don't change attributes of classes of objects. - Don't bother trying to optimize attribute lookup for method calls outside of loops anymore. (Don't try to "cache" methods in variables.) The optimizer will take care of this today better than you could ever do. What else puts the brakes on a program is using module "getattr" methods and tracing or profiling.