History

avcodec/tiff_data: Avoid relocations for TiffGeoTagNameType

Instead store all the strings in one continugous string (with internal \0) and use offsets to access the actual substrings. This replaces the pointers to the strings and therefore avoids relocations (and on x64, it actually shrinks TiffGeoTagNameType by reusing padding to store the offset field). This saves 720B of .data.rel.ro and 1080B of .rela.dyn (containing the relocation records) here while increasing .rodata by 384B.

Andreas Rheinhardt committed 2 years ago

Commit 282812d1 parent ad6347f3 files · +92 −70Browse files

Unified · Split

3 files
  1. libavcodec/tiff.c +16 −12
  2. libavcodec/tiff.h +0 −5
  3. libavcodec/tiff_data.h +76 −53
libavcodec/tiff.c+16−12
@@ -139,27 +139,31 @@ static void free_geotags(TiffContext *const s) · 138 unmodified lines
139139 s->geotag_count = 0;
140140 }
141141
142−#define RET_GEOKEY(TYPE, array, element)\
142+static const char *get_geokey_name(int key)
143+{
144+#define RET_GEOKEY_STR(TYPE, array)\
143145 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
144146 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\
145− return tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
147+ return tiff_##array##_name_type_string + tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].offset;
146148
147−static const char *get_geokey_name(int key)
148−{
149− RET_GEOKEY(VERT, vert, name);
150− RET_GEOKEY(PROJ, proj, name);
151− RET_GEOKEY(GEOG, geog, name);
152− RET_GEOKEY(CONF, conf, name);
149+ RET_GEOKEY_STR(VERT, vert);
150+ RET_GEOKEY_STR(PROJ, proj);
151+ RET_GEOKEY_STR(GEOG, geog);
152+ RET_GEOKEY_STR(CONF, conf);
153153
154154 return NULL;
155155 }
156156
157157 static int get_geokey_type(int key)
158158 {
159− RET_GEOKEY(VERT, vert, type);
160− RET_GEOKEY(PROJ, proj, type);
161− RET_GEOKEY(GEOG, geog, type);
162− RET_GEOKEY(CONF, conf, type);
159+#define RET_GEOKEY_TYPE(TYPE, array)\
160+ if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
161+ key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\
162+ return tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].type;
163+ RET_GEOKEY_TYPE(VERT, vert);
164+ RET_GEOKEY_TYPE(PROJ, proj);
165+ RET_GEOKEY_TYPE(GEOG, geog);
166+ RET_GEOKEY_TYPE(CONF, conf);
163167
164168 return AVERROR_INVALIDDATA;
165169 }
libavcodec/tiff.h+0−5
@@ -222,9 +222,4 @@ typedef struct TiffGeoTagKeyName { · 221 unmodified lines
222222 const char *const name;
223223 } TiffGeoTagKeyName;
224224
225−typedef struct TiffGeoTagNameType {
226− const char *const name;
227− const enum TiffGeoTagType type;
228−} TiffGeoTagNameType;
229−
230225 #endif /* AVCODEC_TIFF_H */
libavcodec/tiff_data.h+76−53
@@ -32,66 +32,89 @@ · 31 unmodified lines
3232
3333 #include "tiff.h"
3434
35+typedef struct TiffGeoTagNameType {
36+ enum TiffGeoTagType type;
37+ unsigned offset;
38+} TiffGeoTagNameType;
39+
3540 #define TIFF_CONF_KEY_ID_OFFSET 1024
36−static const TiffGeoTagNameType tiff_conf_name_type_map[] = {
37− {"GTModelTypeGeoKey", GEOTIFF_SHORT },
38− {"GTRasterTypeGeoKey", GEOTIFF_SHORT },
39− {"GTCitationGeoKey", GEOTIFF_STRING}
40−};
41+#define CONF_NAME_TYPE_MAP(KEY) \
42+ KEY(GTModelTypeGeoKey, SHORT ) \
43+ KEY(GTRasterTypeGeoKey, SHORT ) \
44+ KEY(GTCitationGeoKey, STRING) \
4145
4246 #define TIFF_GEOG_KEY_ID_OFFSET 2048
43−static const TiffGeoTagNameType tiff_geog_name_type_map[] = {
44− {"GeographicTypeGeoKey", GEOTIFF_SHORT },
45− {"GeogCitationGeoKey", GEOTIFF_STRING},
46− {"GeogGeodeticDatumGeoKey", GEOTIFF_SHORT },
47− {"GeogPrimeMeridianGeoKey", GEOTIFF_SHORT },
48− {"GeogLinearUnitsGeoKey", GEOTIFF_SHORT },
49− {"GeogLinearUnitSizeGeoKey", GEOTIFF_DOUBLE},
50− {"GeogAngularUnitsGeoKey", GEOTIFF_SHORT },
51− {"GeogAngularUnitSizeGeoKey", GEOTIFF_DOUBLE},
52− {"GeogEllipsoidGeoKey", GEOTIFF_SHORT },
53− {"GeogSemiMajorAxisGeoKey", GEOTIFF_DOUBLE},
54− {"GeogSemiMinorAxisGeoKey", GEOTIFF_DOUBLE},
55− {"GeogInvFlatteningGeoKey", GEOTIFF_DOUBLE},
56− {"GeogAzimuthUnitsGeoKey", GEOTIFF_SHORT },
57− {"GeogPrimeMeridianLongGeoKey", GEOTIFF_DOUBLE}
58−};
47+#define GEOG_NAME_TYPE_MAP(KEY) \
48+ KEY(GeographicTypeGeoKey, SHORT ) \
49+ KEY(GeogCitationGeoKey, STRING) \
50+ KEY(GeogGeodeticDatumGeoKey, SHORT ) \
51+ KEY(GeogPrimeMeridianGeoKey, SHORT ) \
52+ KEY(GeogLinearUnitsGeoKey, SHORT ) \
53+ KEY(GeogLinearUnitSizeGeoKey, DOUBLE) \
54+ KEY(GeogAngularUnitsGeoKey, SHORT ) \
55+ KEY(GeogAngularUnitSizeGeoKey, DOUBLE) \
56+ KEY(GeogEllipsoidGeoKey, SHORT ) \
57+ KEY(GeogSemiMajorAxisGeoKey, DOUBLE) \
58+ KEY(GeogSemiMinorAxisGeoKey, DOUBLE) \
59+ KEY(GeogInvFlatteningGeoKey, DOUBLE) \
60+ KEY(GeogAzimuthUnitsGeoKey, SHORT ) \
61+ KEY(GeogPrimeMeridianLongGeoKey, DOUBLE) \
5962
6063 #define TIFF_PROJ_KEY_ID_OFFSET 3072
61−static const TiffGeoTagNameType tiff_proj_name_type_map[] = {
62− {"ProjectedCSTypeGeoKey", GEOTIFF_SHORT },
63− {"PCSCitationGeoKey", GEOTIFF_STRING},
64− {"ProjectionGeoKey", GEOTIFF_SHORT },
65− {"ProjCoordTransGeoKey", GEOTIFF_SHORT },
66− {"ProjLinearUnitsGeoKey", GEOTIFF_SHORT },
67− {"ProjLinearUnitSizeGeoKey", GEOTIFF_DOUBLE},
68− {"ProjStdParallel1GeoKey", GEOTIFF_DOUBLE},
69− {"ProjStdParallel2GeoKey", GEOTIFF_DOUBLE},
70− {"ProjNatOriginLongGeoKey", GEOTIFF_DOUBLE},
71− {"ProjNatOriginLatGeoKey", GEOTIFF_DOUBLE},
72− {"ProjFalseEastingGeoKey", GEOTIFF_DOUBLE},
73− {"ProjFalseNorthingGeoKey", GEOTIFF_DOUBLE},
74− {"ProjFalseOriginLongGeoKey", GEOTIFF_DOUBLE},
75− {"ProjFalseOriginLatGeoKey", GEOTIFF_DOUBLE},
76− {"ProjFalseOriginEastingGeoKey", GEOTIFF_DOUBLE},
77− {"ProjFalseOriginNorthingGeoKey", GEOTIFF_DOUBLE},
78− {"ProjCenterLongGeoKey", GEOTIFF_DOUBLE},
79− {"ProjCenterLatGeoKey", GEOTIFF_DOUBLE},
80− {"ProjCenterEastingGeoKey", GEOTIFF_DOUBLE},
81− {"ProjCenterNorthingGeoKey", GEOTIFF_DOUBLE},
82− {"ProjScaleAtNatOriginGeoKey", GEOTIFF_DOUBLE},
83− {"ProjScaleAtCenterGeoKey", GEOTIFF_DOUBLE},
84− {"ProjAzimuthAngleGeoKey", GEOTIFF_DOUBLE},
85− {"ProjStraightVertPoleLongGeoKey", GEOTIFF_DOUBLE}
86−};
64+#define PROJ_NAME_TYPE_MAP(KEY) \
65+ KEY(ProjectedCSTypeGeoKey, SHORT ) \
66+ KEY(PCSCitationGeoKey, STRING) \
67+ KEY(ProjectionGeoKey, SHORT ) \
68+ KEY(ProjCoordTransGeoKey, SHORT ) \
69+ KEY(ProjLinearUnitsGeoKey, SHORT ) \
70+ KEY(ProjLinearUnitSizeGeoKey, DOUBLE) \
71+ KEY(ProjStdParallel1GeoKey, DOUBLE) \
72+ KEY(ProjStdParallel2GeoKey, DOUBLE) \
73+ KEY(ProjNatOriginLongGeoKey, DOUBLE) \
74+ KEY(ProjNatOriginLatGeoKey, DOUBLE) \
75+ KEY(ProjFalseEastingGeoKey, DOUBLE) \
76+ KEY(ProjFalseNorthingGeoKey, DOUBLE) \
77+ KEY(ProjFalseOriginLongGeoKey, DOUBLE) \
78+ KEY(ProjFalseOriginLatGeoKey, DOUBLE) \
79+ KEY(ProjFalseOriginEastingGeoKey, DOUBLE) \
80+ KEY(ProjFalseOriginNorthingGeoKey, DOUBLE) \
81+ KEY(ProjCenterLongGeoKey, DOUBLE) \
82+ KEY(ProjCenterLatGeoKey, DOUBLE) \
83+ KEY(ProjCenterEastingGeoKey, DOUBLE) \
84+ KEY(ProjCenterNorthingGeoKey, DOUBLE) \
85+ KEY(ProjScaleAtNatOriginGeoKey, DOUBLE) \
86+ KEY(ProjScaleAtCenterGeoKey, DOUBLE) \
87+ KEY(ProjAzimuthAngleGeoKey, DOUBLE) \
88+ KEY(ProjStraightVertPoleLongGeoKey, DOUBLE) \
8789
8890 #define TIFF_VERT_KEY_ID_OFFSET 4096
89−static const TiffGeoTagNameType tiff_vert_name_type_map[] = {
90− {"VerticalCSTypeGeoKey", GEOTIFF_SHORT },
91− {"VerticalCitationGeoKey", GEOTIFF_STRING},
92− {"VerticalDatumGeoKey", GEOTIFF_SHORT },
93− {"VerticalUnitsGeoKey", GEOTIFF_SHORT }
94−};
91+#define VERT_NAME_TYPE_MAP(KEY) \
92+ KEY(VerticalCSTypeGeoKey, SHORT ) \
93+ KEY(VerticalCitationGeoKey, STRING) \
94+ KEY(VerticalDatumGeoKey, SHORT ) \
95+ KEY(VerticalUnitsGeoKey, SHORT ) \
96+
97+#define ADD_OFFSET(NAME, TYPE) \
98+ NAME ## _OFFSET, \
99+ NAME ## _END = NAME ## _OFFSET + sizeof(#NAME) - 1, \
100+
101+#define STRING(NAME, TYPE) #NAME "\0"
102+
103+#define ENTRY(NAME, TYPE) { .type = GEOTIFF_ ## TYPE, .offset = NAME ## _OFFSET },
104+#define NAME_TYPE_MAP(NAME, name) \
105+ enum { \
106+ NAME ## _NAME_TYPE_MAP(ADD_OFFSET) \
107+ }; \
108+ static const TiffGeoTagNameType tiff_ ## name ## _name_type_map[] = { \
109+ NAME ## _NAME_TYPE_MAP(ENTRY) \
110+ }; \
111+ static const char *const tiff_ ## name ## _name_type_string = \
112+ NAME ## _NAME_TYPE_MAP(STRING)
113+
114+NAME_TYPE_MAP(CONF, conf);
115+NAME_TYPE_MAP(GEOG, geog);
116+NAME_TYPE_MAP(PROJ, proj);
117+NAME_TYPE_MAP(VERT, vert);
95118
96119 #define TIFF_GEO_KEY_UNDEFINED 0
97120 #define TIFF_GEO_KEY_USER_DEFINED 32767

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t