There are many places in the django codebase where we know the thing we
are marking as safe to be a normal string. In such cases it makes sense to
use `SafeString` instead of `mark_safe`.
To play devils advocate, you could definitely argue that this is an
unnecessary micro-optimisation. Following a brief search for `mark_safe`,
there are some situations where we have something like `mark_safe(X)` and
where evaluating `X` will take sufficiently long that any savings made
marking the string as safe would be rendered insignificant.
Having said that, there are other places where we end up calling
`mark_safe` a very large number of times. In such situations a small
saving in time will add up to a larger saving. There are also places where
we even have `mark_safe("some string literal")`.
Furthermore, since this change is literally just replacing one word with
an equally clear word, it would have no effect on complexity or
readability, and so for a slight performance boost, why not?
--
Ticket URL: <https://code.djangoproject.com/ticket/32568>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Tim McCurrach
--
Ticket URL: <https://code.djangoproject.com/ticket/32568#comment:1>
Old description:
> `mark_safe` takes roughly twice as long as simply creating a `SafeString`
> - using pyperf:
> {{{
> mark_safe: Mean +- std dev: 296 ns +- 12 ns
> SafeString: Mean +- std dev: 158 ns +- 7 ns
> }}}
>
> There are many places in the django codebase where we know the thing we
> are marking as safe to be a normal string. In such cases it makes sense
> to use `SafeString` instead of `mark_safe`.
>
> To play devils advocate, you could definitely argue that this is an
> unnecessary micro-optimisation. Following a brief search for `mark_safe`,
> there are some situations where we have something like `mark_safe(X)` and
> where evaluating `X` will take sufficiently long that any savings made
> marking the string as safe would be rendered insignificant.
>
> Having said that, there are other places where we end up calling
> `mark_safe` a very large number of times. In such situations a small
> saving in time will add up to a larger saving. There are also places
> where we even have `mark_safe("some string literal")`.
>
> Furthermore, since this change is literally just replacing one word with
> an equally clear word, it would have no effect on complexity or
> readability, and so for a slight performance boost, why not?
New description:
`mark_safe` takes roughly twice as long as simply creating a `SafeString`
- using pyperf:
{{{
mark_safe: Mean +- std dev: 296 ns +- 12 ns
SafeString: Mean +- std dev: 158 ns +- 7 ns
}}}
There are many places in the django codebase where we know the thing we
are marking as safe to be a normal (not marked as safe) string. In such
cases it makes sense to use `SafeString` instead of `mark_safe`.
To play devils advocate, you could definitely argue that this is an
unnecessary micro-optimisation. Following a brief search for `mark_safe`,
there are some situations where we have something like `mark_safe(X)` and
where evaluating `X` will take sufficiently long that any savings made
marking the string as safe would be rendered insignificant.
Having said that, there are other places where we end up calling
`mark_safe` a very large number of times. In such situations a small
saving in time will add up to a larger saving. There are also places where
we even have `mark_safe("some string literal")`.
Furthermore, since this change is literally just replacing one word with
an equally clear word, it would have no effect on complexity or
readability, and so for a slight performance boost, why not?
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32568#comment:2>
* version: 3.1 => dev
--
Ticket URL: <https://code.djangoproject.com/ticket/32568#comment:3>
* component: Uncategorized => Template system
* stage: Unreviewed => Accepted
Comment:
Hey Tim — I'm going to provisionally accept this, since you assigned it to
yourself, and I assume there's a PR on the way?
It seems reasonable enough, but I'd like to have a look at it before
saying Yes absolutely.
Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/32568#comment:4>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"cda81b79f212e0666782393c52ad19c2790c9446" cda81b79]:
{{{
#!CommitTicketReference repository=""
revision="cda81b79f212e0666782393c52ad19c2790c9446"
Refs #32568 -- Optimized escape() by using SafeString instead of
mark_safe().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32568#comment:5>