summaryrefslogtreecommitdiff
path: root/source/tools
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2015-03-20 17:02:35 +0000
committerRobin Watts <robin.watts@artifex.com>2015-03-24 19:49:58 +0000
commitf533104d6e66b3fc6d3b63b98ec7fe4fb175b366 (patch)
tree9b26b57a66dcb5124c568a9826311d41292a6056 /source/tools
parente0f638b398b2362f5843ea0c1907f678cfa8e278 (diff)
downloadmupdf-f533104d6e66b3fc6d3b63b98ec7fe4fb175b366.tar.xz
Rework handling of PDF names for speed and memory.
Currently, every PDF name is allocated in a pdf_obj structure, and comparisons are done using strcmp. Given that we can predict most of the PDF names we'll use in a given file, this seems wasteful. The pdf_obj type is opaque outside the pdf-object.c file, so we can abuse it slightly without anyone outside knowing. We collect a sorted list of names used in PDF (resources/pdf/names.txt), and we add a utility (namedump) that preprocesses this into 2 header files. The first (include/mupdf/pdf/pdf-names-table.h, included as part of include/mupdf/pdf/object.h), defines a set of "PDF_NAME_xxxx" entries. These are pdf_obj *'s that callers can use to mean "A PDF object that means literal name 'xxxx'" The second (source/pdf/pdf-name-impl.h) is a C array of names. We therefore update the code so that rather than passing "xxxx" to functions (such as pdf_dict_gets(...)) we now pass PDF_NAME_xxxx (to pdf_dict_get(...)). This is a fairly natural (if widespread) change. The pdf_dict_getp (and sibling) functions that take a path (e.g. "foo/bar/baz") are therefore supplemented with equivalents that take a list (pdf_dict_getl(... , PDF_NAME_foo, PDF_NAME_bar, PDF_NAME_baz, NULL)). The actual implementation of this relies on the fact that small pointer values are never valid values. For a given pdf_obj *p, if NULL < (intptr_t)p < PDF_NAME__LIMIT then p is a literal entry in the name table. This enables us to do fast pointer compares and to skip expensive strcmps. Also, bring "null", "true" and "false" into the same style as PDF names. Rather than using full pdf_obj structures for null/true/false, use special pointer values just above the PDF_NAME_ table. This saves memory and makes comparisons easier.
Diffstat (limited to 'source/tools')
-rw-r--r--source/tools/pdfclean.c38
1 files changed, 19 insertions, 19 deletions
diff --git a/source/tools/pdfclean.c b/source/tools/pdfclean.c
index 07ca7e5a..cbb14f16 100644
--- a/source/tools/pdfclean.c
+++ b/source/tools/pdfclean.c
@@ -59,7 +59,7 @@ static void retainpage(fz_context *ctx, pdf_document *doc, pdf_obj *parent, pdf_
pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, page-1);
pdf_obj *pageobj = pdf_resolve_indirect(ctx, pageref);
- pdf_dict_puts(ctx, pageobj, "Parent", parent);
+ pdf_dict_put(ctx, pageobj, PDF_NAME_Parent, parent);
/* Store page object in new kids array */
pdf_array_push(ctx, kids, pageref);
@@ -76,13 +76,13 @@ static void retainpages(fz_context *ctx, globals *glo, int argc, char **argv)
/* Keep only pages/type and (reduced) dest entries to avoid
* references to unretained pages */
- oldroot = pdf_dict_gets(ctx, pdf_trailer(ctx, doc), "Root");
- pages = pdf_dict_gets(ctx, oldroot, "Pages");
- olddests = pdf_load_name_tree(ctx, doc, "Dests");
+ oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Root);
+ pages = pdf_dict_get(ctx, oldroot, PDF_NAME_Pages);
+ olddests = pdf_load_name_tree(ctx, doc, PDF_NAME_Dests);
root = pdf_new_dict(ctx, doc, 2);
- pdf_dict_puts(ctx, root, "Type", pdf_dict_gets(ctx, oldroot, "Type"));
- pdf_dict_puts(ctx, root, "Pages", pdf_dict_gets(ctx, oldroot, "Pages"));
+ pdf_dict_put(ctx, root, PDF_NAME_Type, pdf_dict_get(ctx, oldroot, PDF_NAME_Type));
+ pdf_dict_put(ctx, root, PDF_NAME_Pages, pdf_dict_get(ctx, oldroot, PDF_NAME_Pages));
pdf_update_object(ctx, doc, pdf_to_num(ctx, oldroot), root);
@@ -138,9 +138,9 @@ static void retainpages(fz_context *ctx, globals *glo, int argc, char **argv)
/* Update page count and kids array */
countobj = pdf_new_int(ctx, doc, pdf_array_len(ctx, kids));
- pdf_dict_puts(ctx, pages, "Count", countobj);
+ pdf_dict_put(ctx, pages, PDF_NAME_Count, countobj);
pdf_drop_obj(ctx, countobj);
- pdf_dict_puts(ctx, pages, "Kids", kids);
+ pdf_dict_put(ctx, pages, PDF_NAME_Kids, kids);
pdf_drop_obj(ctx, kids);
/* Also preserve the (partial) Dests name tree */
@@ -156,10 +156,10 @@ static void retainpages(fz_context *ctx, globals *glo, int argc, char **argv)
{
pdf_obj *key = pdf_dict_get_key(ctx, olddests, i);
pdf_obj *val = pdf_dict_get_val(ctx, olddests, i);
- pdf_obj *dest = pdf_dict_gets(ctx, val, "D");
+ pdf_obj *dest = pdf_dict_get(ctx, val, PDF_NAME_D);
dest = pdf_array_get(ctx, dest ? dest : val, 0);
- if (pdf_array_contains(ctx, pdf_dict_gets(ctx, pages, "Kids"), dest))
+ if (pdf_array_contains(ctx, pdf_dict_get(ctx, pages, PDF_NAME_Kids), dest))
{
pdf_obj *key_str = pdf_new_string(ctx, doc, pdf_to_name(ctx, key), strlen(pdf_to_name(ctx, key)));
pdf_array_push(ctx, names_list, key_str);
@@ -168,10 +168,10 @@ static void retainpages(fz_context *ctx, globals *glo, int argc, char **argv)
}
}
- root = pdf_dict_gets(ctx, pdf_trailer(ctx, doc), "Root");
- pdf_dict_puts(ctx, dests, "Names", names_list);
- pdf_dict_puts(ctx, names, "Dests", dests);
- pdf_dict_puts(ctx, root, "Names", names);
+ root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Root);
+ pdf_dict_put(ctx, dests, PDF_NAME_Names, names_list);
+ pdf_dict_put(ctx, names, PDF_NAME_Dests, dests);
+ pdf_dict_put(ctx, root, PDF_NAME_Names, names);
pdf_drop_obj(ctx, names);
pdf_drop_obj(ctx, dests);
@@ -190,7 +190,7 @@ static void retainpages(fz_context *ctx, globals *glo, int argc, char **argv)
pdf_obj *pageref = pdf_lookup_page_obj(ctx, doc, i);
pdf_obj *pageobj = pdf_resolve_indirect(ctx, pageref);
- pdf_obj *annots = pdf_dict_gets(ctx, pageobj, "Annots");
+ pdf_obj *annots = pdf_dict_get(ctx, pageobj, PDF_NAME_Annots);
int len = pdf_array_len(ctx, annots);
int j;
@@ -200,14 +200,14 @@ static void retainpages(fz_context *ctx, globals *glo, int argc, char **argv)
pdf_obj *o = pdf_array_get(ctx, annots, j);
pdf_obj *p;
- if (strcmp(pdf_to_name(ctx, pdf_dict_gets(ctx, o, "Subtype")), "Link"))
+ if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME_Subtype), PDF_NAME_Link))
continue;
- p = pdf_dict_gets(ctx, o, "A");
- if (strcmp(pdf_to_name(ctx, pdf_dict_gets(ctx, p, "S")), "GoTo"))
+ p = pdf_dict_get(ctx, o, PDF_NAME_A);
+ if (!pdf_name_eq(ctx, pdf_dict_get(ctx, p, PDF_NAME_S), PDF_NAME_GoTo))
continue;
- if (string_in_names_list(ctx, pdf_dict_gets(ctx, p, "D"), names_list))
+ if (string_in_names_list(ctx, pdf_dict_get(ctx, p, PDF_NAME_D), names_list))
continue;
/* FIXME: Should probably look at Next too */