\documentclass{beamer}
\usetheme{Singapore}
\mode<presentation>
\setbeamercovered{dynamic}
\usepackage{CJKutf8}
\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}

\usepackage{listings}

\AtBeginSection[]
{
   \begin{frame}
       \frametitle{Outline}
       \tableofcontents[currentsection]
   \end{frame}
}

\begin{document}
\lstset{basicstyle=\tiny,language=Lisp,linewidth=\textwidth,frame=single}
\begin{CJK}{UTF8}{goth}

  \CJKtilde
  \CJKnospace


\title{teepeedee2: a fast webserver in Lisp}
\author{John Fremlin}
\institute{http://john.freml.in}
\date{2009 July 4}

\frame{\titlepage}

\section{Introduction}

\frame{\frametitle{Introduction}

This talk is about a web platform I made from scratch as a hobby,
called teepeedee2. It runs my blog at http://john.freml.in.

  \begin{itemize}
  \item Please ask questions!
  \item Sorry my Japanese language skills are terrible.
  \item But please interrupt and ask questions anyway!
  \item 疑問が湧いたら途中で、いつでも質問をして下さい。
  \item むしろ、質問してください。
  \item 質問は日本語でも結構です。(スタッフができる範囲でフォローします)
  \item 用中文提問也可以。
  \end{itemize}
}
\frame{\frametitle{teepeedee2}

Teepeedee2 is entirely written in Common Lisp. The only external C library
dependency is libc.

  \begin{itemize}
  \item Fast HTTP server designed for COMET.
  \item Fast HTTP client.
  \item Library for implementing networking protocols with
    continuation passing style transform.
  \item Fast HTML/XML output system.
  \item Domain Specific Language for implementing real-time live updated web applications.
  \item Blog system with real-time comments.
  \item An interactive real-time card game.
  \end{itemize}
}

\frame{\frametitle{Real-time interactive setup}
  \begin{itemize}
  \item WiFi ESSID teepeedee2
  \item Go to http://192.168.1.13:3000/chat
  \end{itemize}
}

\frame{\frametitle{Real-time interactive demo}
  \begin{itemize}
  \item Client requests page by HTTP.
  \item Makes a HTTP request with JavaScript (AJAX) subscribing to
    different channels - COMET. 
  \item If a channel is modified the server responds at once.
  \item Otherwise the server waits.
  \item When a channel is modified all pending connexions have their
    updates sent.
  \end{itemize}
}

\subsection{Benchmarks and testing}
\frame{\frametitle{HTTP server benchmarks}

\begin{itemize}
\item Measure request throughput for a small dynamic webpage - Hello NAME
\item One core only.
\item No logging.
\item schedtool -a 1 -e ab -n 100000 -c10 http://127.0.0.1...
\end{itemize}
}

\frame{\frametitle{Request throughput}

\includegraphics[height=8cm]{request-throughput}

}
\frame{\frametitle{HTTP client benchmarks}
  \begin{itemize}
  \item The HTTP client is not tuned.
  \item Lots of opportunities for improvement.
  \item But still quite fast - comparable to the httperf benchmarking tool.
  \end{itemize}
}

\frame{\frametitle{Client request throughput}
\includegraphics[height=8cm]{client-throughput}
}

\frame{\frametitle{HTTP server under DoS attacks}
  \begin{itemize}
  \item slowloris in default mode does not affect tpd2.
  \item Should be able to scale to 10k connexions without trouble.
  \item How much more?
  \end{itemize}
}

\section{Architecture}

\frame{\frametitle{How does it get fast?}
  \begin{itemize}
  \item epoll system call (Linux only).
  \item Consistent use of (unsigned-byte 8) arrays ``bytestrings'' not
    encoding to and from Unicode strings.
  \item Uses cl-irregsexp for fast text processing.
  \item sendbuf - cords of bytestrings.
  \item Timeout time complexities are $O(1)$ for insert, delete and $O(m)$ for expiry
    where $m$ is the number of timeouts to expire - i.e. best possible
    complexity.
  \item Not much consing (i.e. little garbage generated).
  \end{itemize}
}

\section{Internals}

\subsection{General library}

\frame{\frametitle{my-defun and my}
  \begin{itemize}
  \item defstruct creates a lot of long-winded accessors
  \item In C++, instance slots are already in scope in class methods.
  \item Let's do the same for Lisp.
  \item No runtime overhead for ``my''.
  \item My first real macro!
  \end{itemize}
}
\begin{frame}[fragile]
\frametitle{my-defun and my example}

\lstinputlisting{my.lisp}

\end{frame}

% \frame{\frametitle{defmyclass and its}

%   \begin{itemize}
%   \item defclass syntax $\neq$ defstruct syntax.
%   \item $\therefore$ defmyclass
%   \item The convenience of ``my'' without being in my-defun? ``its''
%   \end{itemize}
% }

\frame{\frametitle{defun-consistent}
  \begin{itemize}
  \item Precomputes the function if the arguments are known to be
    constant at compile-time.
  \item Uses eval if possible.
  \item Otherwise falls back to load-time-value.
  \item Cannot use compiler-macros because the output of compiler
    macros is not compiler macroexpanded on SBCL.
  \end{itemize}
}

\begin{frame}[fragile]
\frametitle{defun-consistent example}

\lstinputlisting{defun-consistent.lisp}
\end{frame}

\subsection{Network IO}

\frame{\frametitle{sendbuf}

  \begin{itemize}
  \item A list of byte-vectors.
  \item Saves copying.
  \item with-sendbuf macrology.
  \item Big optimisation.
  \end{itemize}
}

\frame{\frametitle{defprotocol}

  \begin{itemize}
  \item Uses cl-cont to transform to call/cc continuation passing style.
  \item Second debug mode that works on normal Lisp streams.
  \item Would be transparent but need a lot of without-call/cc to stop
    cl-cont blowing up the compiler.
  \end{itemize}
}

\subsection{HTML, CSS and JavaScript}

\frame{\frametitle{XML/HTML output}

  \begin{itemize}
  \item Spell-checked attributes.
  \item Parent/child relationships enforced at compile time.
  \item As much as possible precomputed at compile-time, remainder
    built up in a sendbuf.
  \item output-object-to-ml
  \item DTD definitions.
  \end{itemize}
}

\frame{\frametitle{superquote}
\begin{itemize}
\item Sometimes `(backtick ,comma) syntax cannot be used because it is
implemented at read-time.
\item Replacement that is expanded as a macro.
\end{itemize}
}

\frame{\frametitle{CSS output}
  \begin{itemize}
  \item Spell checked CSS attributes.
  \item Shortcuts for some common extensions.
  \item Allows use of variables in CSS via superquote.
  \end{itemize}
}

\frame{\frametitle{JavaScript output}
  \begin{itemize}
  \item Parenscript translates a Lisp-like syntax to pretty JavaScript.
  \item Pre-compute at compile time if possible.
  \end{itemize}
}

\subsection{Web applications}

\begin{frame}[fragile]
\frametitle{defpage}
Define a web-page and link it to a site.

\lstinputlisting{defpage.lisp}
\end{frame}

\begin{frame}[fragile]
\frametitle{html-action-form}

A higher level construct allowing both the input form and its handler
to be specified in the same place.

\lstinputlisting{html-action-form.lisp}
\end{frame}

\frame{\frametitle{Channels}

Browser sessions can subscribe to channels. If the channel is updated,
then all the browsers are updated at once.

A strength of tpd2.

\begin{itemize}
\item Inherit from the simple-channel class.
\item Specialise a method for simple-channel-body-ml.
\item Include the channel on a page with output-object-to-ml.
\end{itemize}
}

\begin{frame}[fragile]
\frametitle{channel example}

\lstinputlisting{channel.lisp}

\end{frame}

\subsection{Games}

\frame{\frametitle{Truc demo}

  \begin{itemize}
  \item Highest card wins the hand. (7 is strongest, then 8, A,
    K, Q, J, 10, 9.)
  \item Win two hands and you win the pot.
  \item Increase stake or fold.
  \item Simple AI with precomputed start cards. Deciding when to raise or fold is hard!
  \end{itemize}
}
\frame{\frametitle{defrules}

  Define the rules of a game.
  
  \begin{itemize}
  \item Once again uses cl-cont to transform into CPS.
  \item Console mode for testing.
  \item Several general AIs that can play any game.
  \item Semi-automatically generated web interface.
  \end{itemize}
}

\section{Conclusion}

\frame{\frametitle{Where next?}
  
  A unique web platform looking for an application.

  \pause

  This hobby project needs a direction!

  \pause

  Questions and suggestions please.

}
\end{CJK}
\end{document}

