"""Direct isolated validator replacement; real complete caller, no proxy calls."""
from pathlib import Path
import gc,hashlib,importlib.util,json,platform,statistics,struct,sys,sysconfig,time
root=Path.cwd();folder=root/'.local/pack-ragged-next-native'
sys.path.insert(0,str(root/'src'))
import numpy as np
from unstd.pack import binpack,ragged
installed=ragged._native

def load(name,path):
 spec=importlib.util.spec_from_file_location(name,path);module=importlib.util.module_from_spec(spec);sys.modules[name]=module;spec.loader.exec_module(module);return module
p=load('_direct_consumer_profile',root/'bench/profiles/pack_consumers.py')
r=load('_direct_ragged_profile',root/'bench/profiles/ragged.py')
capture=p._read_capture(root/'.local/pack-consumer-capture.json')
baseline=p._compile_consumers(capture,binpack)['baseline']
modules={'installed':ragged};backends={'installed':installed}
suffix=sysconfig.get_config_var('EXT_SUFFIX')
for name in ('reference','max'):
 source=folder/('direct-'+name)/'src/unstd/pack'
 backend=load('_direct_'+name+'._pack_native',source/('_pack_native'+suffix))
 module=load('_direct_'+name+'.ragged',source/'ragged.py')
 module._native=backend;modules[name]=module;backends[name]=backend
# Validate every lane and tail before any timing; endian and unaligned prefixes.
quality_checks=0
for order in '<>=':
 endian=sys.byteorder if order=='=' else 'little' if order=='<' else 'big'
 for n in (0,1,3,4,7,8,15,16,17,31,32,33,63,64,65,257):
  code='B' if n<=255 else 'H';prefix=struct.pack('=3'+code,0,0,n)
  values=struct.pack(order+str(n)+'f',*[float(i) for i in range(n)])
  args=(prefix+values,3,1,'f',order,code,'=',True,True)
  assert backends['max'].validate(*args)==backends['reference'].validate(*args)==n;quality_checks+=1
  for i in range(n):
   for bits in (0x7f800000,0xff800000,0x7f800001,0x7fc00001):
    data=bytearray(values);data[i*4:i*4+4]=bits.to_bytes(4,endian)
    try:backends['max'].validate(prefix+data,3,1,'f',order,code,'=',True,True)
    except ValueError:quality_checks+=1
    else:raise AssertionError((order,n,i,bits))

candidates={}
for name,module in modules.items():
 decoder=module.RaggedDecoder('<f4',columns=128,index_dtype='<i8',finite=True,allow_empty=False)
 candidates[name]=r._candidate(capture,baseline,decoder)

def timed(fn,n):
 started=time.perf_counter_ns()
 for _ in range(n):fn()
 return (time.perf_counter_ns()-started)/n

def calibrate(fn,target=25_000_000):
 n=1
 while True:
  elapsed=timed(fn,n)
  if elapsed*n>target/8 or n>=100000:break
  n*=2
 return max(1,min(100000,int(target/max(1,elapsed))))

results={}
gc.disable()
try:
 for rows in (1,32,128,512,8192,65536):
  fixtures=[]
  for slot in range(4):
   tokens=struct.pack('<128f',*[((i%31)-15+slot/4)/32 for i in range(128)])*rows
   fixtures.append(p.Fixture(rows,128,struct.pack('<2q',0,rows)+tokens,tokens,capture['identity']))
  functions={}
  for name,module in {'baseline':baseline,**candidates}.items():
   counter=[0]
   def run(module=module,counter=counter):
    fixture=fixtures[counter[0]%4];counter[0]+=1
    return module._remote_index(fixture.body,len(fixture.tokens))
   functions[name]=run
   value=run();r._check(value,fixtures[0],baseline.TokenIndex,shared=name!='baseline')
  counts={name:calibrate(fn) for name,fn in functions.items()}
  samples={name:[] for name in functions}
  for cycle in range(9):
   names=list(functions)
   if cycle%2:names.reverse()
   for name in names:samples[name].append(timed(functions[name],counts[name]))
  medians={name:statistics.median(vals) for name,vals in samples.items()}
  result={'payload_bytes':len(fixtures[0].tokens),'pool_size':4,'iterations':counts,'median_ns':medians,'samples_ns':samples,'installed_over_max':medians['installed']/medians['max'],'same_flags_reference_over_max':medians['reference']/medians['max'],'baseline_over_max':medians['baseline']/medians['max'],'conservative_reference_over_max':min(samples['reference'])/max(samples['max'])}
  results[str(rows)]=result
  print(rows,{name:round(value/1000,3) for name,value in medians.items()},'installed/max',round(result['installed_over_max'],3),'sameflags/max',round(result['same_flags_reference_over_max'],3),flush=True)
finally:gc.enable()
sha=lambda path:hashlib.sha256(path.read_bytes()).hexdigest()
files=[Path(__file__),root/'src/unstd/pack/ragged.py',Path(installed.__file__),root/'bench/profiles/ragged.py',root/'bench/profiles/pack_consumers.py']
for name in ('reference','max'):
 files.extend([folder/('direct-'+name)/'adapter.c',Path(backends[name].__file__),folder/('direct-'+name)/'src/unstd/pack/ragged.py'])
payload={'python':sys.version,'platform':platform.platform(),'numpy_version':np.__version__,'measurement':'complete actual captured decoder/result class;9alternating25ms batches;four distinct immutable input objects/content; outputallocation andrefcountcleanup included; no validationcache; no Python validatorproxy','quality_checks':quality_checks,'hashes':{str(path.relative_to(root)):sha(path) for path in files},'results':results}
(folder/'direct-max-results.json').write_text(json.dumps(payload,indent=2)+'\n')
