Skip to content

Commit 5543c09

Browse files
authored
Merge pull request #85 from struct/perf_fixes_5_14_2022_v2
more perf improvements
2 parents 805e834 + c8183d3 commit 5543c09

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

include/conf.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
* then it has (ZONE_USER_SIZE / 128) = 32768 total
1616
* chunks available for it. The number of canaries is
1717
* calculated as (32768 / CANARY_COUNT_DIV) = 327.
18-
* When CANARY_COUNT_DIV = 100 we set aside %1 of user
19-
* chunks as canaries */
20-
#define CANARY_COUNT_DIV 100
18+
* When CANARY_COUNT_DIV = 7 we set aside < %1 of user
19+
* chunks as canaries because we right shift zone
20+
* chunk count by this value */
21+
#define CANARY_COUNT_DIV 7
2122

2223
/* If you're compiling for Android and want custom names
2324
* for internal mappings you can modify those here */

include/iso_alloc_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ typedef struct {
365365
uint16_t next_sz_index; /* What is the index of the next zone of this size */
366366
uint32_t alloc_count; /* Total number of lifetime allocations */
367367
uint32_t af_count; /* Increment/Decrement with each alloc/free operation */
368-
uint32_t chunk_count;
368+
uint32_t chunk_count; /* Total number of chunks in this zone */
369+
uint8_t chunk_size_pow2; /* Computed by _log2(chunk_size) */
369370
#if MEMORY_TAGGING
370371
bool tagged; /* Zone supports memory tagging */
371372
#endif

src/iso_alloc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ INTERNAL_HIDDEN void create_canary_chunks(iso_alloc_zone_t *zone) {
7575
const bitmap_index_t max_bitmap_idx = GET_MAX_BITMASK_INDEX(zone) - 1;
7676

7777
/* Roughly %1 of the chunks in this zone will become a canary */
78-
const uint64_t canary_count = (zone->chunk_count / CANARY_COUNT_DIV);
78+
const uint64_t canary_count = (zone->chunk_count >> CANARY_COUNT_DIV);
7979

8080
/* This function is only ever called during zone
8181
* initialization so we don't need to check the
@@ -683,11 +683,12 @@ INTERNAL_HIDDEN iso_alloc_zone_t *_iso_new_zone(size_t size, bool internal) {
683683
new_zone->is_full = false;
684684
new_zone->chunk_size = size;
685685

686-
size_t chunk_count = (ZONE_USER_SIZE / new_zone->chunk_size);
686+
new_zone->chunk_size_pow2 = _log2(new_zone->chunk_size);
687+
new_zone->chunk_count = (ZONE_USER_SIZE >> new_zone->chunk_size_pow2);
687688

688689
/* If a caller requests an allocation that is >=(ZONE_USER_SIZE/2)
689690
* then we need to allocate a minimum size bitmap */
690-
uint32_t bitmap_size = (chunk_count << BITS_PER_CHUNK_SHIFT) >> BITS_PER_BYTE_SHIFT;
691+
uint32_t bitmap_size = (new_zone->chunk_count << BITS_PER_CHUNK_SHIFT) >> BITS_PER_BYTE_SHIFT;
691692
new_zone->bitmap_size = (bitmap_size > sizeof(bitmap_index_t)) ? bitmap_size : sizeof(bitmap_index_t);
692693

693694
/* All of the following fields are immutable
@@ -716,7 +717,6 @@ INTERNAL_HIDDEN iso_alloc_zone_t *_iso_new_zone(size_t size, bool internal) {
716717
#endif
717718

718719
size_t total_size = ZONE_USER_SIZE + (_root->system_page_size << 1);
719-
new_zone->chunk_count = chunk_count;
720720

721721
#if MEMORY_TAGGING
722722
/* Each tag is 1 byte in size and the start address
@@ -1731,7 +1731,7 @@ INTERNAL_HIDDEN void iso_free_chunk_from_zone(iso_alloc_zone_t *zone, void *rest
17311731
p, zone->index, zone->chunk_size, (chunk_offset & (zone->chunk_size - 1)));
17321732
}
17331733

1734-
const size_t chunk_number = (chunk_offset / zone->chunk_size);
1734+
const size_t chunk_number = (chunk_offset >> zone->chunk_size_pow2);
17351735
const bit_slot_t bit_slot = (chunk_number << BITS_PER_CHUNK_SHIFT);
17361736
const bit_slot_t dwords_to_bit_slot = (bit_slot >> BITS_PER_QWORD_SHIFT);
17371737

@@ -2010,7 +2010,7 @@ INTERNAL_HIDDEN void _iso_free_internal_unlocked(void *p, bool permanent, iso_al
20102010
void *user_pages_start = UNMASK_USER_PTR(zone);
20112011
uint8_t *_mtp = (user_pages_start - _root->system_page_size - ROUND_UP_PAGE(zone->chunk_count * MEM_TAG_SIZE));
20122012
uint64_t chunk_offset = (uint64_t) (p - user_pages_start);
2013-
_mtp += (chunk_offset / zone->chunk_size);
2013+
_mtp += (chunk_offset >> zone->chunk_size_pow2);
20142014

20152015
/* Generate and write a new tag for this chunk */
20162016
uint8_t mem_tag = (uint8_t) rand_uint64();

0 commit comments

Comments
 (0)