|
1 | | -# fix allocations of this array comprehension: |
| 1 | +# Fix allocations of this array comprehension: |
2 | 2 | # y = [panel.control_point[2] for panel in body_aero.panels] |
| 3 | +# This mwe shows that the line above is allocating a lot, |
| 4 | +# and provides the function test_new() that shows a better way of implementing. |
3 | 5 |
|
4 | 6 | using VortexStepMethod, PreallocationTools |
5 | 7 |
|
@@ -31,17 +33,30 @@ body_aero::BodyAerodynamics = BodyAerodynamics([wing]) |
31 | 33 | y = [panel.control_point[2] for panel in body_aero.panels] |
32 | 34 | n = @allocated y = [panel.control_point[2] for panel in body_aero.panels] |
33 | 35 |
|
34 | | -function test(body_aero, gamma_i) |
| 36 | +function test_old(body_aero, gamma_i) |
| 37 | + y = [panel.control_point[2] for panel in body_aero.panels] |
| 38 | + y |
| 39 | +end |
| 40 | + |
| 41 | +function test_new(body_aero, gamma_i) |
35 | 42 | y = body_aero.y |
36 | 43 | for (i, panel) in pairs(body_aero.panels) |
37 | 44 | y[i] = panel.control_point[2] |
38 | 45 | end |
39 | | - # y = [panel.control_point[2] for panel in body_aero.panels] |
40 | 46 | y |
41 | 47 | end |
42 | 48 |
|
| 49 | + |
43 | 50 | gamma_i=zeros(length(body_aero.panels)) |
44 | 51 |
|
45 | | -test(body_aero, gamma_i) |
46 | | -@allocated test(body_aero, gamma_i) |
| 52 | +# make sure the two test functions get compiled |
| 53 | +test_old(body_aero, gamma_i) |
| 54 | +test_new(body_aero, gamma_i) |
| 55 | + |
| 56 | +println("Old function, using an array comprehension:") |
| 57 | +n = @allocated test_old(body_aero, gamma_i) |
| 58 | +println("Allocations: ", n) |
47 | 59 |
|
| 60 | +println("New function, using a pre-allocated array and a for loop:") |
| 61 | +m = @allocated test_new(body_aero, gamma_i) |
| 62 | +println("Allocations: ", m) |
0 commit comments