Code Monkey home page Code Monkey logo

Comments (2)

memeplex avatar memeplex commented on June 10, 2024

Here is a quick python script that finds repeated chunks and according to a efficiency-of-replacement metric decides whether to factorize them into a function or not. It reduces the size of the script to 5000 lines. Of course, there is a risk of breaking things since the script understands nothing about semantics. The compressed script works fine for me, but my intention was merely to show the amount of compression that can be achieved without losing functionality.

from collections import defaultdict

in_path = "/tmp/rclone"
out_path = "/tmp/rclone-small"

with open(in_path) as f:
    script = f.read()

line_locs = defaultdict(list)
for loc, line in enumerate(script.splitlines(keepends=True)):
    line_locs[line].append(loc)

chunks = []
prev_locs = []
for line, locs in line_locs.items():
    if locs != [loc + 1 for loc in prev_locs]:
        chunk_lines = []
        chunks.append((chunk_lines, len(locs)))
    chunk_lines.append(line)
    prev_locs = locs

chunk_id = 0
for chunk_lines, chunk_reps in chunks:
    chunk_size = len(chunk_lines)
    ratio = (chunk_size + chunk_reps) / (chunk_size * chunk_reps)
    if ratio < 0.5:
        chunk_text = ''.join(chunk_lines)
        chunk_fun = f"chunk_{chunk_id}"
        script = script.replace(chunk_text, f"    {chunk_fun}\n")
        script += f"\n{chunk_fun}() {{\n{chunk_text}}}\n"
        chunk_id += 1

with open(out_path, "w") as f:
    f.write(script)

from rclone.

memeplex avatar memeplex commented on June 10, 2024

Closing this in favor of #7000.

This may be the solution: #7000 (comment)

from rclone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.