⚡️ Speed up function encoded_tokens_len by 70% in PR #231 (remove-tiktoken)
          #235
        
          
      
  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.
  
    
  
    
⚡️ This pull request contains optimizations for PR #231
If you approve this dependent PR, these changes will be merged into the original PR branch
remove-tiktoken.📄 70% (0.70x) speedup for
encoded_tokens_lenincodeflash/code_utils/code_utils.py⏱️ Runtime :
40.1 microseconds→23.6 microseconds(best of237runs)⚡️ This change will improve the performance of the following benchmarks:
📝 Explanation and details
Here is an optimized version of your code. The bottleneck is minimal as the computation is a single multiplication and a cast to int, which is already fast. However, a very minor optimization can be done by avoiding the
int()call for many cases by using integer division directly.You can also remove the
__future__import, asannotationsis default since Python 3.7.Here is an optimized version.
This avoids floating point multiplication and conversion overhead, and gives the same result as
int(len(s)*0.25)for non-negative integerlen(s).✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-pr231-2025-05-21T01.49.04and push.