Skip to content

Commit

Permalink
Set inhibit-modification-hooks to t (#54)
Browse files Browse the repository at this point in the history
* Set `inhibit-modification-hooks` to `t`

This results in a 40% speedup without noticeable user impact. This is surprising because
`jsonian--huge-edit` already wrapped the call in `combine-change-calls`, setting
`before-change-functions` and `after-change-functions` to `nil`.

* Add backup to .gitignore
  • Loading branch information
iwahbe authored Sep 9, 2023
1 parent 75ecae8 commit 318ce1b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
lint.log
bin
test-assets/large-json-file.json
*.backup
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ finally moves point to the end of the file and exits.

| Package | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `fundamental-mode` | 1.444 ± 0.174 | 1.301 | 1.734 | 1.00 ± 0.12 |
| `prog-mode` | 1.442 ± 0.039 | 1.402 | 1.488 | 1.00 |
| `jsonian-mode` | 2.296 ± 0.013 | 2.289 | 2.332 | 1.59 ± 0.04 |
| `json-mode` | 3.775 ± 0.033 | 3.762 | 3.867 | 2.62 ± 0.07 |
| `javascript-mode` | 13.599 ± 0.288 | 13.341 | 14.145 | 9.43 ± 0.32 |
| `fundamental-mode` | 1.357 ± 0.007 | 1.346 | 1.372 | 1.00 |
| `prog-mode` | 1.431 ± 0.009 | 1.419 | 1.444 | 1.05 ± 0.01 |
| `jsonian-mode` | 2.315 ± 0.021 | 2.284 | 2.347 | 1.71 ± 0.02 |
| `json-mode` | 3.846 ± 0.062 | 3.781 | 3.992 | 2.83 ± 0.05 |
| `javascript-mode` | 13.638 ± 0.099 | 13.439 | 13.816 | 10.05 ± 0.09 |

We can use this benchmark to derive how long different parts of the proces take.

Expand All @@ -248,10 +248,10 @@ remove all whitespace. The formatted files are largely identical.

| Package | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `jsonian-format-region` | 1.709 ± 0.091 | 1.633 | 1.877 | 1.12 ± 0.06 |
| `jsonian-format-region (minimize)` | 1.524 ± 0.010 | 1.516 | 1.549 | 1.00 |
| `json-pretty-print-buffer` | 4.582 ± 0.006 | 4.576 | 4.593 | 3.01 ± 0.02 |
| `json-pretty-print-buffer (minimize)` | 4.440 ± 0.114 | 4.384 | 4.753 | 2.91 ± 0.08 |
| `jsonian-format-region` | 1.015 ± 0.011 | 1.000 | 1.034 | 1.18 ± 0.02 |
| `jsonian-format-region (minimize)` | 0.860 ± 0.007 | 0.845 | 0.869 | 1.00 |
| `json-pretty-print-buffer` | 4.655 ± 0.005 | 4.650 | 4.666 | 5.42 ± 0.04 |
| `json-pretty-print-buffer (minimize)` | 4.466 ± 0.020 | 4.437 | 4.502 | 5.20 ± 0.05 |

We see that the built-in `json-pretty-print-buffer` takes significantly longer then
`jsonian-format-region`, regardless of whether we are pretty printing or minimizing.
Expand Down
10 changes: 5 additions & 5 deletions bench/font-lock.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `fundamental-mode` | 1.444 ± 0.174 | 1.301 | 1.734 | 1.00 ± 0.12 |
| `prog-mode` | 1.442 ± 0.039 | 1.402 | 1.488 | 1.00 |
| `jsonian-mode` | 2.296 ± 0.013 | 2.289 | 2.332 | 1.59 ± 0.04 |
| `json-mode` | 3.775 ± 0.033 | 3.762 | 3.867 | 2.62 ± 0.07 |
| `javascript-mode` | 13.599 ± 0.288 | 13.341 | 14.145 | 9.43 ± 0.32 |
| `fundamental-mode` | 1.357 ± 0.007 | 1.346 | 1.372 | 1.00 |
| `prog-mode` | 1.431 ± 0.009 | 1.419 | 1.444 | 1.05 ± 0.01 |
| `jsonian-mode` | 2.315 ± 0.021 | 2.284 | 2.347 | 1.71 ± 0.02 |
| `json-mode` | 3.846 ± 0.062 | 3.781 | 3.992 | 2.83 ± 0.05 |
| `javascript-mode` | 13.638 ± 0.099 | 13.439 | 13.816 | 10.05 ± 0.09 |
8 changes: 4 additions & 4 deletions bench/format.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `jsonian-format-region` | 1.709 ± 0.091 | 1.633 | 1.877 | 1.12 ± 0.06 |
| `jsonian-format-region (minimize)` | 1.524 ± 0.010 | 1.516 | 1.549 | 1.00 |
| `json-pretty-print-buffer` | 4.582 ± 0.006 | 4.576 | 4.593 | 3.01 ± 0.02 |
| `json-pretty-print-buffer (minimize)` | 4.440 ± 0.114 | 4.384 | 4.753 | 2.91 ± 0.08 |
| `jsonian-format-region` | 1.015 ± 0.011 | 1.000 | 1.034 | 1.18 ± 0.02 |
| `jsonian-format-region (minimize)` | 0.860 ± 0.007 | 0.845 | 0.869 | 1.00 |
| `json-pretty-print-buffer` | 4.655 ± 0.005 | 4.650 | 4.666 | 5.42 ± 0.04 |
| `json-pretty-print-buffer (minimize)` | 4.466 ± 0.020 | 4.437 | 4.502 | 5.20 ± 0.05 |
26 changes: 13 additions & 13 deletions bench/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ finally moves point to the end of the file and exits.

| Package | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `fundamental-mode` | 1.444 ± 0.174 | 1.301 | 1.734 | 1.00 ± 0.12 |
| `prog-mode` | 1.442 ± 0.039 | 1.402 | 1.488 | 1.00 |
| `jsonian-mode` | 2.296 ± 0.013 | 2.289 | 2.332 | 1.59 ± 0.04 |
| `json-mode` | 3.775 ± 0.033 | 3.762 | 3.867 | 2.62 ± 0.07 |
| `javascript-mode` | 13.599 ± 0.288 | 13.341 | 14.145 | 9.43 ± 0.32 |
| `fundamental-mode` | 1.357 ± 0.007 | 1.346 | 1.372 | 1.00 |
| `prog-mode` | 1.431 ± 0.009 | 1.419 | 1.444 | 1.05 ± 0.01 |
| `jsonian-mode` | 2.315 ± 0.021 | 2.284 | 2.347 | 1.71 ± 0.02 |
| `json-mode` | 3.846 ± 0.062 | 3.781 | 3.992 | 2.83 ± 0.05 |
| `javascript-mode` | 13.638 ± 0.099 | 13.439 | 13.816 | 10.05 ± 0.09 |

We can use this benchmark to derive how long different parts of the proces take.

- Fundamental mode is the lower limit. This is the time Emacs spends processing the
buffer, parsing sexps, etc.

- We see that `prog-mode` doesn\'t do much more then `fundamental-mode`, which makes
sense.
- `prog-mode` doesn\'t do much more then `fundamental-mode`, which makes sense, since it
takes about the same amount of time.

- Applying JSON formatting take at most `jsonian-mode` - `prog-mode`.

Expand All @@ -38,13 +38,13 @@ remove all whitespace. The formatted files are largely identical.

| Package | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `jsonian-format-region` | 1.709 ± 0.091 | 1.633 | 1.877 | 1.12 ± 0.06 |
| `jsonian-format-region (minimize)` | 1.524 ± 0.010 | 1.516 | 1.549 | 1.00 |
| `json-pretty-print-buffer` | 4.582 ± 0.006 | 4.576 | 4.593 | 3.01 ± 0.02 |
| `json-pretty-print-buffer (minimize)` | 4.440 ± 0.114 | 4.384 | 4.753 | 2.91 ± 0.08 |
| `jsonian-format-region` | 1.015 ± 0.011 | 1.000 | 1.034 | 1.18 ± 0.02 |
| `jsonian-format-region (minimize)` | 0.860 ± 0.007 | 0.845 | 0.869 | 1.00 |
| `json-pretty-print-buffer` | 4.655 ± 0.005 | 4.650 | 4.666 | 5.42 ± 0.04 |
| `json-pretty-print-buffer (minimize)` | 4.466 ± 0.020 | 4.437 | 4.502 | 5.20 ± 0.05 |

We see that the built-in `json-pretty-print-buffer` takes significantly longer then our
implementation.
We see that the built-in `json-pretty-print-buffer` takes significantly longer then
`jsonian-format-region`, regardless of whether we are pretty printing or minimizing.

Notes:

Expand Down
8 changes: 6 additions & 2 deletions jsonian.el
Original file line number Diff line number Diff line change
Expand Up @@ -1939,12 +1939,16 @@ If MINIMIZE is non-nil, minimize the region instead of expanding it."
(interactive "*r\nP")
(let ((current-point (point-marker)))
(jsonian--huge-edit start end
(let ((end (progn (goto-char end) (point-marker))))
;; Both `inhibit-modification-hooks' and `undo-inhibit-record-point' must be inside
;; `jsonian--huge-edit' to allow `jsonian--huge-edit' to handle changes
;; appropriately.
(let ((inhibit-modification-hooks t)
(undo-inhibit-record-point t)
(end (progn (goto-char end) (point-marker))))
(goto-char start)
(jsonian--snap-to-token)
(let* ((indent (jsonian--indentation-spaces))
(indent-level (jsonian--get-indent-level indent))
(undo-inhibit-record-point t)
(next-token (make-marker))
;; Don't allocate a new string each time you add indentation.
;;
Expand Down

0 comments on commit 318ce1b

Please sign in to comment.