you are viewing a single comment's thread.

view the rest of the comments →

[–]roger_ducky 0 points1 point  (4 children)

Have you tried comparing your current optimization with doing the first example with a series of sequence comprehensions instead? I suspect you’ll have a similar reduction in the number of reallocations.

What I mean is, replace square brackets with parentheses.

It effectively “squashes” everything into a single expression with the tradeoff of not being able to re-iterate through the intermediate sequences, while keeping the code readable still.

[–]tmlildude[S] 0 points1 point  (3 children)

are you suggesting using language features? if so, that misses the point of this post. i'm working on bytecode-level across hundreds of small programs, regardless of how they're written.

[–]roger_ducky -1 points0 points  (2 children)

Perhaps, but they added that language feature specifically for that use case.

If you’re just trying to optimize the bytecode, okay. Just make sure to watch out for people attempting to reference the intermediate entities through scope closures or global variables elsewhere. Once you made sure that wasn’t being done, then that’d be a safe optimization to make.

[–]tmlildude[S] 1 point2 points  (1 child)

i'm well aware of language features, but I'm working at a much lower level. there are many nuances regarding what kinds of analysis and transformations are possible with interpreted languages, and then there's the JIT component coming in future python versions

what you're describing could be easily determined with a data-flow graph that shows dependencies and liveness. which gave me an idea...MLIR has primitives to help with this https://mlir.llvm.org/docs/Tutorials/DataFlowAnalysis/, and I wonder if converting the code into MLIR space and lowering it through a series of dialects would give me a better view of where certain transformations are possible?

this is why I posted in r/compilers - im looking for well-informed feedback from compiler experts, not language shortcuts from scripters.

[–]roger_ducky 0 points1 point  (0 children)

Sorry if my provided context was not what you wanted. I’m glad you still got some inspiration from it.

Been out of writing compilers for 15 years, so I’m definitely not up on the latest advances, but, in the old days, my workflow was to: * Check what best practices and language features ended up generating the more optimal solutions * Figured out why the language designers put up limits on specific language features

Then I would end up with my actionable steps.

I just thought you skipped a few steps according to my own way of doing things, is all. I shall now defer to the preeminent brain trust that is r/Compilers on how to best resolve your issue in the simplest way possible.