GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(export): guess MIME types when no MIME policy requires content inspection by haatveit · Pull Request #444 · unioslo/tsd-file-api

Conversation

@haatveit

@haatveit haatveit commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

This change checks whether the tenant's export policy has MIME type restrictions, and if not, uses mimetypes guessing instead of content inspection via magic. For compressed files I'm for now just falling back to magic because mimetypes returns the inner content instead.

In my local testing on SSD, inspecting with magic via asyncio.to_thread() is 250x slower than guessing. On network storage I'd imagine the gap to be even wider.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@haatveit
perf(export): guess MIME types when no MIME policy requires content i…
e2741e6
…nspection
@amn

amn commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Keep in mind we have max_workers=1 by default -- calls to to_thread may effectively form a queue:

tsd-file-api/tsdfileapi/api.py

Lines 192 to 194 in 2ce334b

executor = ThreadPoolExecutor(
max_workers=options.max_workers
) # Our own executor, set as default further down, allows control over maximum amount of worker threads the application may use -- through e.g. `asyncio.to_thread` and implicitly through `aiofiles`

I had a good reason (so I thought at the time, at least) to use 1 as default, but can't tell now why, pretty sure it's in a commit message should you want to git blame it.

Point being, the 250x slowdown may be due to the queue processing -- not the cost of to_thread itself (assuming free thread in the pool).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haatveit

haatveit commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Either I had a really good run last week or I must've misread my test data, because 250x slower than filename guessing is generally what I get from direct calls to libmagic.

When going via asyncio.to_thread the further overhead makes it average to ~300x slower than mimetypes filename guesses on my machine, and there's not really any gain to be had from increasing workers because magic.from_file places a lock that completely serializes execution:

https://github.com/ahupp/python-magic/blob/f7b3f1396bfa46d2d7d1a722b689ed424513c83a/magic/__init__.py#L150-L158

https://github.com/ahupp/python-magic/blob/f7b3f1396bfa46d2d7d1a722b689ed424513c83a/magic/__init__.py#L201-L218

We'd need to set up per-thread Magic class instances to get around that, but my testing just mirrored what the API is doing. Tried that now and with 8 workers and thread local instances it looks like we could get 2x throughput compared to what we're currently doing.

But pay in mind these numbers are all from my Mac's SSD, there'll be a wider gap between "looking at filenames" and actually reading files for network storage, and not doing file I/O is always going to be faster. :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haatveit haatveit mentioned this pull request Nov 23, 2026
Deserialize python-magic from_magic execution #446
Open
@haatveit

haatveit commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

I had a good reason (so I thought at the time, at least) to use 1 as default, but can't tell now why, pretty sure it's in a commit message should you want to git blame it.

edfbd99 cites memory use if I read it correctly. I guess we need to actually test if there are any side effects from using additional workers but assume it would be fine.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@haatveit @amn
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.