⚡️ Speed up method S3DataSource.get_bucket_cors by 74%
          #279
        
          
      
  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.
  
    
  
    
📄 74% (0.74x) speedup for
S3DataSource.get_bucket_corsinbackend/python/app/sources/external/s3/s3.py⏱️ Runtime :
1.13 milliseconds→648 microseconds(best of88runs)📝 Explanation and details
The key optimization is client connection reuse through a persistent S3 client object.
What changed:
_s3_client_objinstance variable and_get_persistent_s3_client()methodsession.client('s3')context manager for each request, the optimized version creates one persistent client using__aenter__()and reuses it across all subsequent callsWhy this improves performance:
The original code created and tore down a new aioboto3 S3 client connection for every single
get_bucket_corscall. Eachasync with session.client('s3')involves:The line profiler shows the impact: in the original code,
async with session.client('s3') as s3_client:took 24.1% of total execution time. In the optimized version, this overhead is eliminated after the first call.Performance gains:
Best for: Workloads making multiple S3 operations on the same S3DataSource instance. The optimization shines in scenarios like the concurrent tests (100-200 requests), where connection reuse provides cumulative savings. Single-call scenarios see minimal benefit since the first call still pays the client creation cost.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-S3DataSource.get_bucket_cors-mhczwdcrand push.