GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Simplify stdlib code by using `itertools.batched()` · Issue #126317 · python/cpython

Simplify stdlib code by using itertools.batched() #126317

New issue
Copy link
New issue
Copy link
Closed
Closed
Simplify stdlib code by using itertools.batched()#126317
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@lgeiger
lgeiger
opened on Nov 1, 2024
Issue body actions

#98364 introduced itertools.batched() but since it's pretty new it's still rarely used inside the standard library.

pickle.py includes a custom version multiple times which all could be replaced by itertools.batched() for improved readability.
This probably has a negligible performance impact (at the very least it won't be slower) but should make the code easier to understand.

Let me know if you're interested in a contribution for this or whether you'd rather not have the extra churn for a minor change like this.

Linked PRs

  • gh-126317: Simplify stdlib code by using itertools.batched() #126323

Activity

  1. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 2, 2024
  2. picnixz
    added this to Pickle and copy issues 🥒on Nov 2, 2024
  3. picnixz commented on Nov 2, 2024

    @picnixz
    picnixz
    on Nov 2, 2024
    Member
    More actions

    I'm not sure we want to refactor pickle.py, especially not the pure Python implementation. But I'll let this decision fall to @serhiy-storchaka.

    Reacted by Lukas Geiger, sobolevn and Lee Dong Wook
  4. dongwooklee96
    added a commit that references this issue on Nov 2, 2024

    pythongh-126317: Simplify stdlib code by using itertools.batched()

    11eb1ee
  5. serhiy-storchaka commented on Nov 2, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Nov 2, 2024
    Member
    More actions

    There is no great need in such change, but it makes the code slightly smaller, so I have no objections. Please show the results of microbenchmarks for pickling large list, dict and set. Even a small speedup would be an additional argument for this change. A slowdown would be a sign that there is something wrong with itertools.batched().

  6. dongwooklee96 commented on Nov 2, 2024

    @dongwooklee96
    dongwooklee96
    on Nov 2, 2024
    Contributor
    More actions

    bench.py

    import pickle
    import pyperf
    
    large_list = list(range(10**6))  # 1,000,000 list
    large_dict = {str(i): i for i in range(10**6)}  # 1,000,000 dict
    large_set = set(range(10**6))  # 1,000,000 set
    
    def pickle_save(data, filename):
        with open(filename, "wb") as f:
            pickle.dump(data, f)
    
    def pickle_load(filename):
        with open(filename, "rb") as f:
            return pickle.load(f)
    
    def run_benchmarks():
        runner = pyperf.Runner()
    
        # Pickle dump test
        runner.bench_func("pickle_save_large_list", pickle_save, large_list, "large_list.pkl")
        runner.bench_func("pickle_save_large_dict", pickle_save, large_dict, "large_dict.pkl")
        runner.bench_func("pickle_save_large_set", pickle_save, large_set, "large_set.pkl")
    
        # Pickle load test
        runner.bench_func("pickle_load_large_list", pickle_load, "large_list.pkl")
        runner.bench_func("pickle_load_large_dict", pickle_load, "large_dict.pkl")
        runner.bench_func("pickle_load_large_set", pickle_load, "large_set.pkl")
    
    if __name__ == "__main__":
        run_benchmarks()

    result

    Benchmark hidden because not significant (6): pickle_save_large_list, pickle_save_large_dict, pickle_save_large_set, pickle_load_large_list, pickle_load_large_dict, pickle_load_large_set
    

    The result of running the benchmark using pyperf.

    I'm sorry, my results are a little different from what I've done before, so I'm guessing there's a difference in configure. If you get a different result, I'd appreciate it if you could let me know.

  7. dongwooklee96
    added 2 commits that reference this issue on Nov 2, 2024

    pythongh-126317: Simplify stdlib code by using itertools.batched()

    4fa459b

    pythongh-126317: Fix to avoid calling the len function twice

    21d7b01
  8. serhiy-storchaka
    added a commit that references this issue on Nov 2, 2024

    gh-126317: Simplify pickle code by using itertools.batched() (GH-126323)

    bd4be5e
  9. serhiy-storchaka commented on Nov 2, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Nov 2, 2024
    Member
    More actions

    Thank you for your contribution @dongwooklee96.

    Reacted by Lee Dong Wook and Terry Jan Reedy
  10. serhiy-storchaka
    closed this as completedon Nov 2, 2024
  11. github-project-automation
    moved this to Done in Pickle and copy issues 🥒on Nov 2, 2024
  12. picnixz
    added a commit that references this issue on Dec 8, 2024

    pythongh-126317: Simplify pickle code by using itertools.batched() (p…

    6c5227d
  13. ebonnal
    added a commit that references this issue on Jan 12, 2025

    pythongh-126317: Simplify pickle code by using itertools.batched() (p…

    96db8d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Pickle and copy issues 🥒
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.