Commit a3d4ee5
committed
[Xamarin.Android.Build.Tasks] no temp files in GenerateJavaStubs
Context: #2505
This is based on #2505, but I incorporated the java.interop changes so
`GenerateJavaStubs` uses no temp files at all.
In many places throughout the Xamarin.Android build, we have a pattern
of:
- Generate a temp file.
- Use `MonoAndroidHelper.CopyIfChanged` to put the file in the
destination. This reads both files, doing a hash comparison before
deciding to write or not.
- Delete the temp file.
Sometimes the temp file is actually in `%TEMP%`, but other times we
append `.new` to the destination file. The case of `.new` can collide
if two builds are running at once (example caused by a known VS for
Mac issue).
Thinking about this, in cases with small files, we can make a simple
optimization:
- Generate the file in-memory.
- Use a new `MonoAndroidHelper.CopyStreamIfChanged` method`.
This has several benefits:
- We never write a file to disk when there are no changes.
- We don't have to *remember* to delete the file.
- The code, in general, is slightly simpler.
The only place we likely shouldn't use this new pattern, would be if
the file was huge.
~~ Changes ~~
I added new APIs for:
- `Files.HasStreamChanged` - to compare a `Stream` in memory
- `Files.HasBytesChanged` - to compare a `byte[]` in memory
- `Files.CopyIfStreamChanged`
- `Files.CopyIfStringChanged` - some cases we have a `string`
- `Files.CopyIfBytesChanged` - this supports `string`
- `MonoAndroidHelper.CopyIfStreamChanged`
- `MonoAndroidHelper.CopyIfStringChanged`
- `MonoAndroidHelper.CopyIfBytesChanged`
I changed the following MSBuild tasks, mostly to test out the new
behavior:
- `GenerateResourceDesigner` was using a `.new` file.
- `GenerateJavaStubs` was using temp files in many places. I was able
to fix up all of these.
- `ManifestDocument` now has an overload for `Save` that takes a
`Stream`
- `Generator` now uses `CopyIfStreamChanged` a new
`GetDestinationPath` method from java.interop. It reuses a single
`MemoryStream`, and I moved the `GenerateJavaSource` method into
`CreateJavaSources` for simplicity.
I made other general refactoring in `GenerateJavaStubs`:
- Since we don't have to worry about deleting a `temp_map_file`, we
can return earlier if `Generator.CreateJavaSources` fails.
- A `GetResource<T>` method, cleans up the places reading from
`EmbeddedResource` files. I also changed it up to properly `Dispose`
things.
- A `save` anonymous method/delegate should just be a `SaveResource`
*regular* method.
- A few places were calling `Path.GetFullPath` unecessarily. Since
this method accesses the file system, we should skip it unless the
full path is actually needed.
- Avoid using `StringWriter` and `string.Format`.
- Use capacity and `StringComparer.Ordinal` when creating
dictionaries: https://www.dotnetperls.com/dictionary-stringcomparer
- Preallocate `MemoryStream` with `java_types.Length * 32`
I also added some tests for the new APIs in `MonoAndroidHelper`.
~~ Results ~~
I did three test runs, because I was getting varying times for
`GenerateJavaStubs`. This is the Xamarin.Forms integration project in
this repo:
Before (Clean Build):
1. 1433 ms GenerateJavaStubs 1 calls
2. 1594 ms GenerateJavaStubs 1 calls
3. 1353 ms GenerateJavaStubs 1 calls
After (Clean Build):
1. 1201 ms GenerateJavaStubs 1 calls
2. 1137 ms GenerateJavaStubs 1 calls
3. 1136 ms GenerateJavaStubs 1 calls
Before (Incremental):
1. 1184 ms GenerateJavaStubs 1 calls
2. 1181 ms GenerateJavaStubs 1 calls
3. 1172 ms GenerateJavaStubs 1 calls
After (Incremental):
1. 1035 ms GenerateJavaStubs 1 calls
2. 1049 ms GenerateJavaStubs 1 calls
3. 1036 ms GenerateJavaStubs 1 calls
`GenerateJavaStubs` is now about 250ms faster on initial build and
150ms faster on incremental builds.
I could not see a difference in `GenerateResourceDesigner`, likely
since it wrote only a single temp file.
Next steps would be to make changes in other MSBuild tasks as well.1 parent b0aaaf2 commit a3d4ee5
File tree
8 files changed
+373
-144
lines changed- src/Xamarin.Android.Build.Tasks
- Generator
- Tasks
- Tests/Xamarin.Android.Build.Tests
- Utilities
- Utilities
8 files changed
+373
-144
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
39 | 24 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
48 | 34 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
56 | 58 | | |
| 59 | + | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
Lines changed: 91 additions & 98 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
80 | | - | |
| 79 | + | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
| |||
99 | 98 | | |
100 | 99 | | |
101 | 100 | | |
102 | | - | |
103 | | - | |
104 | 101 | | |
105 | 102 | | |
106 | 103 | | |
| |||
111 | 108 | | |
112 | 109 | | |
113 | 110 | | |
114 | | - | |
| 111 | + | |
| 112 | + | |
115 | 113 | | |
116 | | - | |
| 114 | + | |
117 | 115 | | |
118 | 116 | | |
119 | 117 | | |
| |||
130 | 128 | | |
131 | 129 | | |
132 | 130 | | |
133 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
| 136 | + | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
| 139 | + | |
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
145 | | - | |
| 144 | + | |
| 145 | + | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
178 | 196 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | 197 | | |
202 | | - | |
| 198 | + | |
| 199 | + | |
203 | 200 | | |
204 | 201 | | |
205 | 202 | | |
| |||
219 | 216 | | |
220 | 217 | | |
221 | 218 | | |
222 | | - | |
223 | | - | |
| 219 | + | |
| 220 | + | |
224 | 221 | | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
229 | 225 | | |
230 | 226 | | |
231 | 227 | | |
232 | | - | |
| 228 | + | |
233 | 229 | | |
234 | 230 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
241 | 234 | | |
242 | 235 | | |
243 | 236 | | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | 237 | | |
255 | 238 | | |
256 | 239 | | |
| |||
262 | 245 | | |
263 | 246 | | |
264 | 247 | | |
265 | | - | |
| 248 | + | |
266 | 249 | | |
267 | | - | |
| 250 | + | |
268 | 251 | | |
269 | 252 | | |
270 | 253 | | |
271 | 254 | | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
276 | 270 | | |
277 | 271 | | |
278 | 272 | | |
| |||
287 | 281 | | |
288 | 282 | | |
289 | 283 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
295 | 288 | | |
296 | 289 | | |
297 | 290 | | |
Lines changed: 2 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
239 | 239 | | |
240 | 240 | | |
241 | 241 | | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
| 242 | + | |
| 243 | + | |
248 | 244 | | |
249 | 245 | | |
250 | 246 | | |
| |||
0 commit comments