Opened 7 years ago

Closed 15 months ago

#6668 closed Cleanup/optimization (fixed)

Optimise utils.text wrap

Reported by: SmileyChris Owned by: giuliettamasina
Component: Utilities Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In my work on #6667 I spent some time optimising the django.utils.text wrap method.

Here's the patch.

Attachments (2)

wrap_test.py (570 bytes) - added by SmileyChris 7 years ago.
Here's the test file I used (you'll have to copy my new method to new_wrap to run it yourself)
faster_wrap.diff (3.3 KB) - added by SmileyChris 7 years ago.
and here's the patch

Download all attachments as: .zip

Change History (20)

Changed 7 years ago by SmileyChris

Here's the test file I used (you'll have to copy my new method to new_wrap to run it yourself)

Changed 7 years ago by SmileyChris

and here's the patch

comment:1 Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to SmileyChris
  • Patch needs improvement unset
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Design decision needed

The test results:

Sentence
old: 1.43731307983
new: 0.553267002106

Paragraph
old: 4.15362501144
new: 1.33707404137

10 Paragraphs
old: 17.3589639664
new: 5.5629529953

(I'm not sure if this really needs to go through design decision, but I'll put it there anyway)

comment:2 Changed 7 years ago by ubernostrum

On this and #6668, is there a good reason to avoid use of Python's own built-in "textwrap" module?

comment:3 Changed 7 years ago by ubernostrum

Er, this and #6667 I mean.

comment:4 Changed 7 years ago by SmileyChris

It could potentially be implemented, but it messes with whitespace. My patch was intended to be identical to the current one (it's actually just a side-effect of me writing the mail wrap funciton).

The built-in one definitely isn't good enough for #6667.

comment:5 Changed 4 years ago by julien

  • Type set to Cleanup/optimization

comment:6 Changed 4 years ago by julien

  • Severity set to Normal

comment:7 Changed 4 years ago by Alex

  • Easy pickings unset
  • Triage Stage changed from Design decision needed to Accepted
  • UI/UX unset

Accepted, see #6667.

comment:8 Changed 2 years ago by aaugustin

  • Component changed from Core (Other) to Utilities

comment:9 Changed 2 years ago by aaugustin

  • Patch needs improvement set

The patch no longer applies, and the test must be rewritten as unittests.

comment:10 Changed 2 years ago by susan

I tested the patch that has the changes to text.py with the function wrap(). The original patch still works. The existing tests all pass.

comment:11 Changed 2 years ago by susan

  • Owner changed from SmileyChris to susan

comment:12 Changed 2 years ago by susan

See PR: https://github.com/django/django/pull/1339 SmileyChris's patch works against existing utils_tests suite.

comment:13 Changed 2 years ago by timo

As noted on the PR, it looks like this introduces an off by one error in the wordcount filter. Failing test is test_wordcount (defaultfilters.tests.DefaultFiltersTests)

comment:14 Changed 22 months ago by susan

  • Owner susan deleted
  • Status changed from assigned to new

comment:15 Changed 15 months ago by giuliettamasina

  • Owner set to giuliettamasina
  • Status changed from new to assigned

I've updated this patch to pass all tests, here's a new pull request:

https://github.com/django/django/pull/2668

The fix was to wrap the max_width = lines in min() to make sure it never exceeds the given wrap width.

comment:16 Changed 15 months ago by giuliettamasina

  • Patch needs improvement unset

comment:17 Changed 15 months ago by giuliettamasina

However, I have no idea why the wordwrap tests are in test_wordcount(). I added another commit to just move them to their own method.

comment:18 Changed 15 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In acb20016c0567876e7fc8144ecbe77905ee8388a:

Fixed #6668 -- Optimized utils.text wrap function

This fixes a failing test after applying an optimization of the
utils.text.wrap function by user SmileyChris.

Note: See TracTickets for help on using tickets.
Back to Top