This should be the start of a discussion on how to combine market makers with an order book. The most simple solution would be to just have them completely separated.
On chain market maker calculations that includes an order book are most likely infeasible. However - maybe a "valid state" can be defined and checked easily.
(something like: the no current market maker price is allowed to be higher than the highes matching open order) It than would be necessary for the user to full fill an order AND trade with the market maker to result in a valid state (can be done atomic with a transactions that full fills a list of trades). The calculations required can be done off chain.
However - for this validity check a sorted list of the orderbook would be necessary. Even this would stop trades having a fixed gas prices. So very busy markets would become more expensive and could even run out of the block gas limit and therefore create a dead lock.