-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allocation improvements #32
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not opposed to the PR or its intended changes, but there are other concerns mixed in with it and the changes to generateBaseCellNeighbors.c
are simply incorrect.
@@ -13,6 +13,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
#include <string.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any code in here using string.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memset
for all of these comments.
src/apps/filters/hexRange.c
Outdated
@@ -30,13 +30,15 @@ | |||
|
|||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. printf
is in stdio
, I don't see why string
was included.
src/apps/filters/kRing.c
Outdated
@@ -25,22 +25,22 @@ | |||
|
|||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this.
@@ -237,18 +226,19 @@ void generate() { | |||
|
|||
printf("const int baseCellNeighbors[NUM_BASE_CELLS][7] = {\n"); | |||
for (int i = 0; i < NUM_BASE_CELLS; i++) { | |||
char* neighborStrings[7] = {0, 0, 0, 0, 0, 0, 0}; | |||
printf("{"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please undo the changes you made here. This approach will print garbage around the INVALID_BASE_CELL
printout, instead of only printing that message when reaching that condition, so it will not generate the base cell neighbors code correctly, anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I'm very confused. The difference between the old code and this is the removal of the generateBaseCellOutput
, which simply formatted strings that are printed here. Instead, I used identical code here to write the digits/macro directly to stdout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dfellis Could you paste the output you are seeing or expecting? I ran this branch and got the same output as before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mistake on the reading of his changes. I did not run his branch.
src/h3lib/lib/algos.c
Outdated
@@ -22,6 +22,7 @@ | |||
#include <math.h> | |||
#include <stdbool.h> | |||
#include <stdlib.h> | |||
#include <string.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.
src/h3lib/lib/algos.c
Outdated
@@ -90,9 +91,8 @@ int H3_EXPORT(maxKringSize)(int k) { | |||
*/ | |||
void H3_EXPORT(kRing)(H3Index origin, int k, H3Index* out) { | |||
int maxIdx = H3_EXPORT(maxKringSize)(k); | |||
int* distances = calloc(maxIdx, sizeof(int)); | |||
int distances[maxIdx]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was a little concerned this does not initialize distances
. It might be that distances
cannot be used before initializing. Not sure if you want to add a memset here for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Was almost certain I followed them all with a memset
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
numRemainingHexes = compactableCount; | ||
} | ||
// Final cleanup | ||
free(remainingHexes); | ||
free(hashSetArray); | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this line is no longer reached in tests, perhaps because of the change around line 276. Could you change to preserve 100% coverage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Will push fix for that now.
@@ -237,18 +226,19 @@ void generate() { | |||
|
|||
printf("const int baseCellNeighbors[NUM_BASE_CELLS][7] = {\n"); | |||
for (int i = 0; i < NUM_BASE_CELLS; i++) { | |||
char* neighborStrings[7] = {0, 0, 0, 0, 0, 0, 0}; | |||
printf("{"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dfellis Could you paste the output you are seeing or expecting? I ran this branch and got the same output as before.
src/h3lib/lib/algos.c
Outdated
@@ -741,7 +742,8 @@ void H3_EXPORT(polyfill)(const GeoPolygon* geoPolygon, int res, H3Index* out) { | |||
// This first part is identical to the maxPolyfillSize above. | |||
|
|||
// Get the bounding boxes for the polygon and any holes | |||
BBox* bboxes = calloc(sizeof(BBox), geoPolygon->numHoles + 1); | |||
BBox bboxes[geoPolygon->numHoles + 1]; | |||
memset(bboxes, 0, sizeof(BBox)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a mistake. Let me fix now.
src/h3lib/lib/algos.c
Outdated
@@ -741,7 +742,8 @@ void H3_EXPORT(polyfill)(const GeoPolygon* geoPolygon, int res, H3Index* out) { | |||
// This first part is identical to the maxPolyfillSize above. | |||
|
|||
// Get the bounding boxes for the polygon and any holes | |||
BBox* bboxes = calloc(sizeof(BBox), geoPolygon->numHoles + 1); | |||
BBox bboxes[geoPolygon->numHoles + 1]; | |||
memset(bboxes, 0, sizeof(bboxes)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Think I addressed all the comments. |
I tested this on MSVC (Visual Studio 2017) and it seems it does not support variable length arrays. I would prefer the repo is limited to the subset of C99/C11 that MSVC supports to maximize how broadly it can be used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One note, probably ignorable, otherwise LGTM.
currentCoord = currentLoop->first; | ||
while (currentCoord != NULL) { | ||
bool skip = true; | ||
for (LinkedGeoPolygon *currentPolygon = polygon, *nextPolygon; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I believe our style in this project is LinkedGeoPolygon* currentPolygon
, can we use that here? Or would that require LinkedGeoPolygon* currentPolygon, LinkedGeoPolygon* nextPolygon
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the pointer alignment? Ya it's a problem. clang-format
isn't even fixing it. The only alternative is LinkedGeoPolygon* nextPolygon;
followed by for (LinkedGeoPolygon* currentPolygon = ...)
. I once answered a question about this on StackOverflow here: https://stackoverflow.com/a/16490876/1930331.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*Requested change is discussing what features of C we want to require.
@@ -0,0 +1,21 @@ | |||
if(__check_alloca) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include the license header in all source files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK.
@@ -0,0 +1,44 @@ | |||
#ifndef STACKALLOC_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also include a @file
/@brief
comment describing the purpose of this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea
neighborStrings[0], neighborStrings[1], neighborStrings[2], | ||
neighborStrings[3], neighborStrings[4], neighborStrings[5], | ||
neighborStrings[6], i, | ||
printf("}, /* base cell %d%s */\n", i, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the formatting of the comments changes, could you rerun generateBaseCellNeighbors and update baseCells.c?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing.
I successfully built and tested this on MSVC :) - #34 will make that automated. |
Luckily even MSVC has the alloca function. It's a bit ugly but this should definitely allow some form of VLA on all popular platforms. |
reentrant_gmtime(&tmStruct, &rawTime); | ||
const char licenseFormat[] = | ||
"/*\n" | ||
" * Copyright 2016-%d Uber Technologies, Inc.\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a lot of work just to automatically format the date. I don't think the parts of the baseCells.c file unrelated to the baseCellNeighbors/baseCellNeighbor60CCWRots should be generated. (Those two were the only parts of the file that were generated from this - maybe those two could be split off to their own, generated file.)
Could you revert this change, and we can revisit it in another issue or PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Allocation improvements
No description provided.