From 6b0195a615e121c59db6711fcb4544fa9eb5c46a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr>
Date: Fri, 15 Mar 2019 11:16:42 +0100
Subject: [PATCH] [Lib/Markdown] optimisation of empty and nil

---
 src/libraries/utils/markdown.ml  | 23 +++++++++++++++++------
 src/libraries/utils/markdown.mli |  1 +
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/libraries/utils/markdown.ml b/src/libraries/utils/markdown.ml
index 2a5d1ec1656..61a484b6a14 100644
--- a/src/libraries/utils/markdown.ml
+++ b/src/libraries/utils/markdown.ml
@@ -65,11 +65,13 @@ let local ctxt job data =
 (* --- Combinators                                                        --- *)
 (* -------------------------------------------------------------------------- *)
 
+let nil _fmt = ()
+let empty= nil
 let space fmt = Format.pp_print_space fmt ()
 let newline fmt = Format.pp_print_newline fmt ()
 
 let merge sep ds fmt =
-  match ds with
+  match List.filter (fun d -> d != nil) ds with
   | [] -> ()
   | d::ds -> d fmt ; List.iter (fun d -> sep fmt ; d fmt) ds
 
@@ -78,9 +80,20 @@ let glue ?sep ds fmt =
   | None -> List.iter (fun d -> d fmt) ds
   | Some s -> merge s ds fmt
 
-let (<@>) a b fmt = a fmt ; b fmt
-let (<+>) a b fmt = a fmt ; space fmt ; b fmt
-let (</>) a b fmt = a fmt ; newline fmt ; b fmt
+let (<@>) a b =
+  if a == empty then b else
+  if b == empty then a else
+    fun fmt -> a fmt ; b fmt
+
+let (<+>) a b =
+  if a == empty then b else
+  if b == empty then a else
+    fun fmt -> a fmt ; space fmt ; b fmt
+
+let (</>) a b =
+  if a == empty then b else
+  if b == empty then a else
+    fun fmt -> a fmt ; newline fmt ; b fmt
 
 let fmt_text k fmt = Format.fprintf fmt "@[<h 0>%t@]" k
 let fmt_block k fmt = Format.fprintf fmt "@[<v 0>%t@]" k
@@ -151,8 +164,6 @@ let href ?title (h : href) fmt =
 (* --- Blocks                                                             --- *)
 (* -------------------------------------------------------------------------- *)
 
-let empty _fmt = ()
-
 let aname anchor fmt =
   Format.fprintf fmt "<a name=\"%s\"></a>@\n" anchor
 
diff --git a/src/libraries/utils/markdown.mli b/src/libraries/utils/markdown.mli
index 31fb5e003f6..34b955919fc 100644
--- a/src/libraries/utils/markdown.mli
+++ b/src/libraries/utils/markdown.mli
@@ -42,6 +42,7 @@ val (</>) : block -> block -> block (** Infix operator for [concat] *)
 
 (** {2 Text Constructors} *)
 
+val nil : text (** Empty *)
 val raw : string -> text (** inlined markdown format *)
 val rm : string -> text (** roman (normal) style *)
 val it : string -> text (** italic style *)
-- 
GitLab