Skip to content

Commit a5a9f34

Browse files
refactor: do not specify g_direct_{hash, equal} for performance
g_hash_table_new/g_hash_table_new_full: ``` * Hash values returned by @hash_func are used to determine where keys * are stored within the #GHashTable data structure. The g_direct_hash(), * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash() * functions are provided for some common types of keys. * If @hash_func is %NULL, g_direct_hash() is used. * * @key_equal_func is used when looking up keys in the #GHashTable. * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal() * and g_str_equal() functions are provided for the most common types * of keys. If @key_equal_func is %NULL, keys are compared directly in * a similar fashion to g_direct_equal(), but without the overhead of * a function call. @key_equal_func is called with the key from the hash table * as its first parameter, and the user-provided key to check against as * its second. ``` 1. `If @hash_func is %NULL, g_direct_hash() is used.` so passing this argument to the function is unnecessary. 2. `If @key_equal_func is %NULL, keys are compared directly in a similar fashion to g_direct_equal(), but without the overhead of a function call.` so passing g_direct_equal instead of NULL will make the lookups slower. mechanical change using the following commands: ```sh sed -i '' 's/g_direct_hash/NULL/g' ./hw/**/*.c ./util/**/*.c ./target/**/*.c ./accel/**/*.c ./system/**/*.c ./migration/**/*.c sed -i '' 's/g_direct_equal/NULL/g' ./hw/**/*.c ./util/**/*.c ./target/**/*.c ./accel/**/*.c ./system/**/*.c ./migration/**/*.c ```
1 parent ca50795 commit a5a9f34

File tree

15 files changed

+22
-24
lines changed

15 files changed

+22
-24
lines changed

accel/kvm/kvm-all.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ static void kvm_irqchip_create(KVMState *s)
24582458

24592459
kvm_init_irq_routing(s);
24602460

2461-
s->gsimap = g_hash_table_new(g_direct_hash, g_direct_equal);
2461+
s->gsimap = g_hash_table_new(NULL, NULL);
24622462
}
24632463

24642464
/* Find number of supported CPUs using the recommended

hw/arm/apple-silicon/dart.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,7 @@ static void apple_dart_reset(DeviceState *dev)
653653
if (mapper->tlb) {
654654
g_hash_table_destroy(mapper->tlb);
655655
}
656-
mapper->tlb = g_hash_table_new_full(g_direct_hash, g_direct_equal,
657-
NULL, g_free);
656+
mapper->tlb = g_hash_table_new_full(NULL, NULL, NULL, g_free);
658657
break;
659658
}
660659
default:

hw/i386/kvm/xen_gnttab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static struct xengntdev_handle *xen_be_gnttab_open(void)
498498
{
499499
struct xengntdev_handle *xgt = g_new0(struct xengntdev_handle, 1);
500500

501-
xgt->active_maps = g_hash_table_new(g_direct_hash, g_direct_equal);
501+
xgt->active_maps = g_hash_table_new(NULL, NULL);
502502
return xgt;
503503
}
504504

hw/i386/kvm/xenstore_impl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ XenstoreImplState *xs_impl_create(unsigned int dom_id)
13741374
GList *perms;
13751375

13761376
s->watches = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1377-
s->transactions = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1377+
s->transactions = g_hash_table_new_full(NULL, NULL,
13781378
NULL, xs_tx_free);
13791379

13801380
perms = g_list_append(NULL, xs_perm_as_string(XS_PERM_NONE, 0));

hw/misc/apple-silicon/a7iop/rtkit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,7 @@ void apple_rtkit_init(AppleRTKit *s, void *opaque, const char *role,
441441
apple_rtkit_handle_messages_bh);
442442

443443
s->opaque = opaque ? opaque : s;
444-
s->endpoints =
445-
g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
444+
s->endpoints = g_hash_table_new_full(NULL, NULL, NULL, g_free);
446445
s->ops = ops;
447446
QTAILQ_INIT(&s->rollcall);
448447
qemu_mutex_init(&s->lock);

hw/ppc/vof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ void vof_init(Vof *vof, uint64_t top_addr, Error **errp)
10031003
{
10041004
vof_cleanup(vof);
10051005

1006-
vof->of_instances = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1006+
vof->of_instances = g_hash_table_new_full(NULL, NULL,
10071007
NULL, vof_instance_free);
10081008
vof->claimed = g_array_new(false, false, sizeof(OfClaimed));
10091009

hw/riscv/riscv-iommu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2583,7 +2583,7 @@ static void riscv_iommu_realize(DeviceState *dev, Error **errp)
25832583
if (s->cap & RISCV_IOMMU_CAP_HPM) {
25842584
s->hpm_timer =
25852585
timer_new_ns(QEMU_CLOCK_VIRTUAL, riscv_iommu_hpm_timer_cb, s);
2586-
s->hpm_event_ctr_map = g_hash_table_new(g_direct_hash, g_direct_equal);
2586+
s->hpm_event_ctr_map = g_hash_table_new(NULL, NULL);
25872587
}
25882588
}
25892589

migration/postcopy-ram.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static GHashTable *blocktime_init_tid_to_vcpu_hash(void)
269269
* be 0, which means NULL. Then when lookup we can never know whether
270270
* it's 0 or "not found". Hence use an indirection for CPU index.
271271
*/
272-
GHashTable *table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
272+
GHashTable *table = g_hash_table_new_full(NULL, NULL,
273273
NULL, g_free);
274274
CPUState *cpu;
275275

@@ -311,8 +311,8 @@ static struct PostcopyBlocktimeContext *blocktime_context_new(void)
311311
*
312312
* The value will be a list of BlocktimeVCPUEntry entries.
313313
*/
314-
ctx->vcpu_addr_hash = g_hash_table_new_full(g_direct_hash,
315-
g_direct_equal,
314+
ctx->vcpu_addr_hash = g_hash_table_new_full(NULL,
315+
NULL,
316316
NULL,
317317
blocktime_vcpu_list_free);
318318

migration/rdma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ static int qemu_rdma_source_init(RDMAContext *rdma, bool pin_all, Error **errp)
23092309
qemu_rdma_init_ram_blocks(rdma);
23102310

23112311
/* Build the hash that maps from offset to RAMBlock */
2312-
rdma->blockmap = g_hash_table_new(g_direct_hash, g_direct_equal);
2312+
rdma->blockmap = g_hash_table_new(NULL, NULL);
23132313
for (int i = 0; i < rdma->local_ram_blocks.nb_blocks; i++) {
23142314
g_hash_table_insert(rdma->blockmap,
23152315
(void *)(uintptr_t)rdma->local_ram_blocks.block[i].offset,

system/memory.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ static void flatviews_init(void)
10431043
return;
10441044
}
10451045

1046-
flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1046+
flat_views = g_hash_table_new_full(NULL, NULL, NULL,
10471047
(GDestroyNotify) flatview_unref);
10481048
if (!empty_view) {
10491049
empty_view = generate_memory_topology(NULL);
@@ -3563,7 +3563,7 @@ static void mtree_info_flatview(bool dispatch_tree, bool owner)
35633563
AddressSpace *as;
35643564
FlatView *view;
35653565
GArray *fv_address_spaces;
3566-
GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal);
3566+
GHashTable *views = g_hash_table_new(NULL, NULL);
35673567
AccelClass *ac = ACCEL_GET_CLASS(current_accel());
35683568

35693569
if (ac->has_memory) {
@@ -3639,7 +3639,7 @@ static void mtree_info_as(bool dispatch_tree, bool owner, bool disabled)
36393639
MemoryRegionListHead ml_head;
36403640
MemoryRegionList *ml, *ml2;
36413641
AddressSpace *as;
3642-
GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal);
3642+
GHashTable *views = g_hash_table_new(NULL, NULL);
36433643
GSList *as_same_root_mr_list;
36443644
struct AddressSpaceInfo asi = {
36453645
.ml_head = &ml_head,

0 commit comments

Comments
 (0)