Skip to content

Commit

Permalink
Document core auto-generation example in "Loading Slynk faster".
Browse files Browse the repository at this point in the history
* doc/sly.texi (Loading Slynk faster): Add Emacs core generation function.
  • Loading branch information
Ambrevar committed Nov 17, 2020
1 parent fd0d004 commit a7eb959
Showing 1 changed file with 56 additions and 13 deletions.
69 changes: 56 additions & 13 deletions doc/sly.texi
Original file line number Diff line number Diff line change
Expand Up @@ -2900,24 +2900,67 @@ Lisp (SBCL) is presented. Similar setups should also work for other
Lisp implementations.

For SBCL, we recommend that you create a custom core file with socket
support and @acronym{POSIX} bindings included because those modules
take the most time to load. To create such a core, execute the
following steps:

@example
shell$ sbcl
* (mapc 'require '(sb-bsd-sockets sb-posix sb-introspect sb-cltl2 asdf))
* (save-lisp-and-die "sbcl.core-for-sly")
@end example

After that, add something like this to your @file{~/.emacs} or
support and @acronym{POSIX} bindings included because those modules take
the most time to load. To create such a core, include the following in
your @file{~/.emacs} or
@file{~/.emacs.d/init.el} (@pxref{Emacs Init File}):

@lisp
(setq sly-lisp-implementations '((sbcl ("sbcl" "--core"
"sbcl.core-for-sly"))))
(defvar my/sly-sbcl-core-extra-packages '(:serapeum))
(defun my/sly-dump-sbcl-core (&rest extra-package-keywords)
"Dump an SBCL core optimized for SLY.
EXTRA-PACKAGE-KEYWORDS can be included in the core.
The core is stored in `user-emacs-directory'/sly.
Return path to the generated core, or nil if it failed to generate.
If the core already exists, it's only regenerated if the SBCL version has
changed, of when called interactively."
(interactive my/sly-sbcl-core-extra-packages)
(cl-flet ((read-bytes (path)
(with-temp-buffer
(insert-file-contents-literally path)
(buffer-substring-no-properties (point-min) (point-max))))
(call-process-to-string (command &rest args)
(with-temp-buffer
(apply #'call-process command nil t nil args)
(buffer-string))))
(let* ((sbcl-core-dir (expand-file-name "sly/" user-emacs-directory))
(sbcl-core (expand-file-name "sbcl.core-for-sly" sbcl-core-dir))
(sbcl-core-version (expand-file-name "sbcl.version" sbcl-core-dir))
(current-sbcl-version (cadr (split-string
(call-process-to-string "sbcl" "--version"))))
(lisp-output-buffer (get-buffer-create " *Lisp dump log*")))
(when (or (called-interactively-p 'any)
(not (file-exists-p sbcl-core-version))
(not (version= (car (split-string (read-bytes sbcl-core-version)))
current-sbcl-version)))
(make-directory sbcl-core-dir :parents)
(with-current-buffer lisp-output-buffer
(erase-buffer))
(apply #'call-process
"sbcl" nil (list lisp-output-buffer t) nil
"--eval" "(mapc 'require '(sb-bsd-sockets sb-posix sb-introspect sb-cltl2 asdf))"
(append
(mapcan (lambda (package-keyword)
(list "--eval" (format "(require %S)" package-keyword)))
extra-package-keywords)
(list
"--eval" (format "(save-lisp-and-die %S)" sbcl-core)))))
(if (not (file-exists-p sbcl-core))
(switch-to-buffer-other-window lisp-output-buffer)
(with-temp-file sbcl-core-version
(insert current-sbcl-version))
sbcl-core))))
(setq sly-lisp-implementations
'((sbcl (lambda ()
(let ((core (my/sly-dump-sbcl-core :alexandria)))
`(("sbcl" ,@(when core `("--core" ,core)))))))))
@end lisp

As mentioned in the @code{my/sly-dump-sbcl-core} docstring, the core is
generated on demand and on SBCL version change.

For maximum startup speed you can include the Slynk server directly in
a core file. The disadvantage of this approach is that the setup is a
bit more involved and that you need to create a new core file when you
Expand Down

3 comments on commit a7eb959

@joaotavora
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must say this is getting rather big for an example in the manual. Examples in the manual are supposed to be readable pieces of code that illustrate a principle that a newcomer can follow along. That is what the current example is. It shows the principle of dumping an image.

In the manual, or in the README, we can add a link to some extra "SLY utils" package or contrib that has this kind of sophistication.

@Ambrevar
Copy link
Contributor Author

@Ambrevar Ambrevar commented on a7eb959 Nov 17, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joaotavora
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise let's add a link as you suggested. Maybe I'll make a Common
Lisp library then.

There's a section about third party contribs in the README.

Please sign in to comment.