⚡️ Speed up function _is_tmp_file by 149%
          #402
        
          
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
📄 149% (1.49x) speedup for
_is_tmp_fileinmarimo/_server/recents.py⏱️ Runtime :
2.28 milliseconds→915 microseconds(best of166runs)📝 Explanation and details
The optimization replaces the
any()generator expression with a direct call tostr.startswith()using a tuple argument. Instead of iterating through each folder in_IGNORED_FOLDERSand callingstartswith()individually, the optimized version passes the entire tuple tostartswith(), which can check all prefixes in a single operation.Key changes:
any()function and generator that required Python to iterate through each folder namestr.startswith()with a tuple argument is implemented in C and performs all prefix checks in one callstartswith()calls (one for each folder), there's now just 1Why it's faster:
The original code uses
any()with a generator that callsfilename.startswith()for each folder until a match is found. This involves Python bytecode interpretation overhead for each iteration. The optimized version leverages the fact thatstr.startswith()natively accepts a tuple of prefixes and performs all checks efficiently in C code.Performance characteristics:
/tmpsee ~180% speedup since the check completes immediately without iterationThe 149% overall speedup demonstrates that eliminating the Python-level iteration overhead provides substantial performance gains across all input patterns.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_4al8aq2a/tmprksu_t7p/test_concolic_coverage.py::test__is_tmp_fileTo edit these changes
git checkout codeflash/optimize-_is_tmp_file-mh5qbuttand push.