Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#2179 closed Enhancement (fixed)

memory allocation in benc could be more efficient

Reported by: charles Owned by: charles
Priority: Normal Milestone: 1.71
Component: libtransmission Version: 1.70
Severity: Normal Keywords:
Cc:

Description (last modified by charles)

When creating a dictionary or list in bencode.c, we preallocate room for 8 children. Testing real-world bencoded data indicates that we could change 8 to 4 -- using less memory -- without having to realloc very often.

To figure out the "best" number to use in practice, I added a counter to track how many dictionaries and lists are created, and how often we had to realloc() them. Then I tested out different bin sizes when loading a huge number of .torrent files.

bin size 2. had to realloc 187913 times out of 570483  (32.9)
bin size 3. had to realloc 133254 times out of 562893  (23.7)
bin size 4. had to realloc 67725 times out of 564444  (12.0)
bin size 5. had to realloc 55063 times out of 564173  (9.8)
bin size 6. had to realloc 37850 times out of 566511  (6.7)
bin size 7. had to realloc 31966 times out of 561274  (5.7)
bin size 8. had to realloc 26887 times out of 566704  (4.7)

We save about 10% when raising the bin size from 2 to 3, and again from 3 to 4. After that we only get 1-2%, so 4 appears to be the best cutoff.

Change History (3)

comment:1 Changed 12 years ago by charles

  • Status changed from new to assigned

preallocation changed from 8 to 4 in r8646.

comment:2 Changed 12 years ago by charles

  • Resolution set to fixed
  • Status changed from assigned to closed

comment:3 Changed 12 years ago by charles

  • Description modified (diff)
Note: See TracTickets for help on using tickets.