⚡️ Speed up function embedding_from_b64 by 8%
          #1
        
          
      
  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.
  
    
  
    
📄 8% (0.08x) speedup for
embedding_from_b64inmodules/textual_inversion/image_embedding.py⏱️ Runtime :
1.08 milliseconds→995 microseconds(best of146runs)📝 Explanation and details
The optimization adds an explicit UTF-8 decoding step (
d.decode('utf-8')) before passing the data tojson.loads(). Whilejson.loads()can accept both bytes and strings in Python 3.6+, passing a decoded string is more efficient than letting the JSON parser handle the bytes internally.Key changes:
Why this improves performance:
The JSON parser has optimized code paths for string inputs versus bytes inputs. When given bytes,
json.loads()must first decode them internally, adding overhead. By pre-decoding to UTF-8, we use the more direct string parsing path, reducing the per-call overhead by ~3-4 microseconds.Performance characteristics from tests:
The optimization is most effective for smaller payloads where the decoding overhead represents a larger fraction of total processing time.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-embedding_from_b64-mh9tmh9band push.