diff options
author | Robin Watts <robin.watts@artifex.com> | 2015-03-20 17:02:35 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2015-03-24 19:49:58 +0000 |
commit | f533104d6e66b3fc6d3b63b98ec7fe4fb175b366 (patch) | |
tree | 9b26b57a66dcb5124c568a9826311d41292a6056 /source/pdf/pdf-op-filter.c | |
parent | e0f638b398b2362f5843ea0c1907f678cfa8e278 (diff) | |
download | mupdf-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/pdf/pdf-op-filter.c')
-rw-r--r-- | source/pdf/pdf-op-filter.c | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/source/pdf/pdf-op-filter.c b/source/pdf/pdf-op-filter.c index c030f468..24644a37 100644 --- a/source/pdf/pdf-op-filter.c +++ b/source/pdf/pdf-op-filter.c @@ -51,22 +51,22 @@ typedef struct pdf_filter_processor_s } pdf_filter_processor; static void -copy_resource(fz_context *ctx, pdf_filter_processor *p, const char *key, const char *name) +copy_resource(fz_context *ctx, pdf_filter_processor *p, pdf_obj *key, const char *name) { pdf_obj *res, *obj; if (!name || name[0] == 0) return; - res = pdf_dict_gets(ctx, p->old_rdb, key); + res = pdf_dict_get(ctx, p->old_rdb, key); obj = pdf_dict_gets(ctx, res, name); if (obj) { - res = pdf_dict_gets(ctx, p->new_rdb, key); + res = pdf_dict_get(ctx, p->new_rdb, key); if (!res) { res = pdf_new_dict(ctx, p->doc, 1); - pdf_dict_puts_drop(ctx, p->new_rdb, key, res); + pdf_dict_put_drop(ctx, p->new_rdb, key, res); } pdf_dict_putp(ctx, res, name, obj); } @@ -402,7 +402,7 @@ pdf_filter_gs_begin(fz_context *ctx, pdf_processor *proc, const char *name, pdf_ filter_flush(ctx, p, FLUSH_ALL); if (p->chain->op_gs_begin) p->chain->op_gs_begin(ctx, p->chain, name, extgstate); - copy_resource(ctx, p, "ExtGState", name); + copy_resource(ctx, p, PDF_NAME_ExtGState, name); } static void @@ -725,7 +725,7 @@ pdf_filter_Tf(fz_context *ctx, pdf_processor *proc, const char *name, pdf_font_d filter_flush(ctx, p, 0); if (p->chain->op_Tf) p->chain->op_Tf(ctx, p->chain, name, font, size); - copy_resource(ctx, p, "Font", name); + copy_resource(ctx, p, PDF_NAME_Font, name); } static void @@ -851,7 +851,7 @@ pdf_filter_CS(fz_context *ctx, pdf_processor *proc, const char *name, fz_colorsp filter_gstate *gstate = gstate_to_update(ctx, p); fz_strlcpy(gstate->CS.name, name, sizeof gstate->CS.name); gstate->CS.cs = cs; - copy_resource(ctx, p, "ColorSpace", name); + copy_resource(ctx, p, PDF_NAME_ColorSpace, name); } static void @@ -861,7 +861,7 @@ pdf_filter_cs(fz_context *ctx, pdf_processor *proc, const char *name, fz_colorsp filter_gstate *gstate = gstate_to_update(ctx, p); fz_strlcpy(gstate->cs.name, name, sizeof gstate->cs.name); gstate->cs.cs = cs; - copy_resource(ctx, p, "ColorSpace", name); + copy_resource(ctx, p, PDF_NAME_ColorSpace, name); } static void @@ -876,7 +876,7 @@ pdf_filter_SC_pattern(fz_context *ctx, pdf_processor *proc, const char *name, pd gstate->SC.n = n; for (i = 0; i < n; ++i) gstate->SC.c[i] = color[i]; - copy_resource(ctx, p, "Pattern", name); + copy_resource(ctx, p, PDF_NAME_Pattern, name); } static void @@ -891,7 +891,7 @@ pdf_filter_sc_pattern(fz_context *ctx, pdf_processor *proc, const char *name, pd gstate->sc.n = n; for (i = 0; i < n; ++i) gstate->sc.c[i] = color[i]; - copy_resource(ctx, p, "Pattern", name); + copy_resource(ctx, p, PDF_NAME_Pattern, name); } static void @@ -903,7 +903,7 @@ pdf_filter_SC_shade(fz_context *ctx, pdf_processor *proc, const char *name, fz_s gstate->SC.pat = NULL; gstate->SC.shd = shade; gstate->SC.n = 0; - copy_resource(ctx, p, "Pattern", name); + copy_resource(ctx, p, PDF_NAME_Pattern, name); } static void @@ -915,7 +915,7 @@ pdf_filter_sc_shade(fz_context *ctx, pdf_processor *proc, const char *name, fz_s gstate->sc.pat = NULL; gstate->sc.shd = shade; gstate->sc.n = 0; - copy_resource(ctx, p, "Pattern", name); + copy_resource(ctx, p, PDF_NAME_Pattern, name); } static void @@ -1012,7 +1012,7 @@ pdf_filter_sh(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade * filter_flush(ctx, p, FLUSH_ALL); if (p->chain->op_sh) p->chain->op_sh(ctx, p->chain, name, shade); - copy_resource(ctx, p, "Shading", name); + copy_resource(ctx, p, PDF_NAME_Shading, name); } static void @@ -1022,7 +1022,7 @@ pdf_filter_Do_image(fz_context *ctx, pdf_processor *proc, const char *name, fz_i filter_flush(ctx, p, FLUSH_ALL); if (p->chain->op_Do_image) p->chain->op_Do_image(ctx, p->chain, name, image); - copy_resource(ctx, p, "XObject", name); + copy_resource(ctx, p, PDF_NAME_XObject, name); } static void @@ -1032,7 +1032,7 @@ pdf_filter_Do_form(fz_context *ctx, pdf_processor *proc, const char *name, pdf_x filter_flush(ctx, p, FLUSH_ALL); if (p->chain->op_Do_form) p->chain->op_Do_form(ctx, p->chain, name, xobj, page_resources); - copy_resource(ctx, p, "XObject", name); + copy_resource(ctx, p, PDF_NAME_XObject, name); } /* marked content */ |