upb: simplify upb_Map_Insert() down to upb_Map_Set() where possible

PiperOrigin-RevId: 592107014
pull/15124/head
Eric Salo 11 months ago committed by Copybara-Service
parent f284522c4b
commit 1587fefee7
  1. 10
      upb/message/accessors.c
  2. 8
      upb/message/accessors.h
  3. 3
      upb/message/copy.c
  4. 10
      upb/message/internal/map.h
  5. 8
      upb/message/internal/map_entry.h
  6. 6
      upb/message/internal/map_sorter.h
  7. 11
      upb/message/promote.c

@ -21,11 +21,9 @@
// Must be last.
#include "upb/port/def.inc"
upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* f,
upb_Message* map_entry_message,
upb_Arena* arena) {
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table,
const upb_MiniTableField* f,
upb_Message* map_entry_message, upb_Arena* arena) {
// TODO: use a variant of upb_MiniTable_GetSubMessageTable() here.
const upb_MiniTable* map_entry_mini_table = upb_MiniTableSub_Message(
mini_table->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)]);
@ -43,7 +41,7 @@ upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
upb_Message_GetField(map_entry_message, map_entry_key_field, default_val);
upb_MessageValue map_entry_value = upb_Message_GetField(
map_entry_message, map_entry_value_field, default_val);
return upb_Map_Insert(map, map_entry_key, map_entry_value, arena);
return upb_Map_Set(map, map_entry_key, map_entry_value, arena);
}
bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2,

@ -449,11 +449,9 @@ UPB_API_INLINE upb_Map* upb_Message_GetOrCreateMutableMap(
}
// Updates a map entry given an entry message.
upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message,
upb_Arena* arena);
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message, upb_Arena* arena);
// Compares two messages by serializing them and calling memcmp().
bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2,

@ -105,8 +105,7 @@ upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
if (!upb_Clone_MessageValue(&val, value_field_type, value_sub, arena)) {
return NULL;
}
if (upb_Map_Insert(cloned_map, key, val, arena) ==
kUpb_MapInsertStatus_OutOfMemory) {
if (!upb_Map_Set(cloned_map, key, val, arena)) {
return NULL;
}
}

@ -7,9 +7,13 @@
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_H_
#define UPB_MESSAGE_INTERNAL_MAP_H_
#include <stddef.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
@ -147,4 +151,4 @@ upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
#include "upb/port/undef.inc"
#endif /* UPB_COLLECTIONS_INTERNAL_MAP_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MAP_H_ */

@ -5,8 +5,10 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_
#define UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_
#include <stdint.h>
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
@ -40,4 +42,4 @@ typedef struct {
upb_MapEntryData data;
} upb_MapEntry;
#endif // UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#endif // UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_

@ -7,8 +7,8 @@
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
#define UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
#include <stdlib.h>
@ -86,4 +86,4 @@ bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
#include "upb/port/undef.inc"
#endif /* UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ */

@ -15,7 +15,6 @@
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/message.h"
@ -347,13 +346,9 @@ upb_UnknownToMessage_Status upb_MiniTable_PromoteUnknownToMap(
upb_Map* map = upb_Message_GetOrCreateMutableMap(msg, map_entry_mini_table,
field, arena);
upb_Message* map_entry_message = ret.message;
upb_MapInsertStatus insert_status = upb_Message_InsertMapEntry(
map, mini_table, field, map_entry_message, arena);
if (insert_status == kUpb_MapInsertStatus_OutOfMemory) {
return kUpb_UnknownToMessage_OutOfMemory;
}
UPB_ASSUME(insert_status == kUpb_MapInsertStatus_Inserted ||
insert_status == kUpb_MapInsertStatus_Replaced);
bool insert_success = upb_Message_SetMapEntry(map, mini_table, field,
map_entry_message, arena);
if (!insert_success) return kUpb_UnknownToMessage_OutOfMemory;
upb_Message_DeleteUnknown(msg, unknown.ptr, unknown.len);
}
return kUpb_UnknownToMessage_Ok;

Loading…
Cancel
Save