Skip to content

Commit

Permalink
fix: lower protobuf hash order of magnitude for PHP (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Dec 8, 2020
1 parent 332a413 commit 197a644
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ public Map<Integer, Field> getFieldsWithNumbers() {
// All the "magic numbers" come from the proto3 spec:
// https://developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers
private int getFieldNumber(String fieldName) {
int fieldNumber = (fieldName.hashCode() << 3) >>> 3;
// TODO: Use 2^29 instead of 2^28 once the following is fixed:
// https://github.com/protocolbuffers/protobuf/issues/8114
int fieldNumber = (fieldName.hashCode() << 4) >>> 4;
if (fieldNumber == 0 || (fieldNumber >= 19000 && fieldNumber <= 19999)) {
fieldNumber = 20000 + ((fieldNumber % 19000) + 1) * 536314;
fieldNumber = 20000 + ((fieldNumber % 19000) + 1) * 268147;
}
return fieldNumber;
}

private int incrementFieldNumber(int fieldNumber) {
int incrementedFieldNumber = fieldNumber + 1;
if ((fieldNumber >= 19000 && fieldNumber <= 19999) || fieldNumber >= (1 << 29)) {
if ((fieldNumber >= 19000 && fieldNumber <= 19999) || fieldNumber >= (1 << 28)) {
incrementedFieldNumber = 20000;
}
return incrementedFieldNumber;
Expand Down
Loading

0 comments on commit 197a644

Please sign in to comment.